如何从串口读取 [英] How to read from Serial Port

查看:90
本文介绍了如何从串口读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们好,

请帮助我解决此问题.我想从COM7端口读取.我尝试了各种手段来获取价值.我期望串行端口是

"0A 0D AE E6 D6 6F D7 0F 0B 9B BF E2 41 A3 3A CF 67 CB D7 EE 6F 58 8B DB 7D 79 BE 6C 69 FE F3 B0 8D DD"

但我得到以下值:

"x10 x13 x10 x13 x63 x63 x10 x13 x63 x63 x63 x111 x10 x13 x63 x63 x63 x63 x111 x63 x15"

我使用的代码是:

 字符串 Rxstring = " ;
字符 []结果=  字符 [  1 ];
      for ( int  len =  0 ; len <  result.Length;)
        len + = serialPort1.Read(result,len,result.Length-len);复制代码
      foreach ( char  c  in 结果中的字符)
        RxString + = 字符串 .Format(" ,Convert.ToUInt16(c).ToString())+  "; 


我正在使用设备(arduino uno)将1字节的数据写入COM端口.我更改了波特率,但数据未更改.是转换(Convert.ToUint16)的问题.热销将其转换为十六进制值.

请帮帮我.

从下面的非解决方案复制的其他信息(在解决方案1之后)
谢谢我的朋友,正如您提到的,我编辑了我的代码,问题仍然存在.现在我得到的值是:

"0A 0D 3F 3F 3F 6F 3F 0F 1B 3F 3F 3F 41F 3F 3A 3F 67 3F 3F 3F 6F 58 0B 3F 7D 79 3F 6C 69 3F 3F 3F 3F 3F 3F"

为什么我大多数时候都得到"3F",并且它的ASCII等效值是?"? ?.某些数据完全匹配.但是...

我正在使用设备(arduino uno)将1字节的数据写入COM端口.我已经更改了波特率,但是接收到的数据没有更改.是转换问题(Convert.ToUint16)还是c#无法识别Visual Studio编码中的字符或问题.正确估价; "x {0}"应为"{0:X2}"并删除ToString.


那是一些非常奇怪的代码,可能需要更多帮助.


谢谢我的朋友,我按你提到的那样编辑了代码,再次出现了问题.现在我得到的值是:

"0A 0D 3F 3F 3F 6F 3F 0F 1B 3F 3F 3F 41F 3F 3A 3F 67 3F 3F 3F 6F 58 0B 3F 7D 79 3F 6C 69 3F 3F 3F 3F 3F 3F"

为什么我大多数时候都得到"3F",并且它的ASCII等效值是?"? ?.某些数据完全匹配.但是...

我正在使用设备(arduino uno)将1字节的数据写入COM端口.我已经更改了波特率,但是接收到的数据没有更改.是转换问题(Convert.ToUint16)还是c#无法识别字符或Visual Studio编码问题.


Hai guys,

Please help me to sort out this issue. I wan to read from COM7 port. I tried every means to grab the values. What i am expecting from serial port is

"0A 0D AE E6 D6 6F D7 0F 0B 9B BF E2 41 A3 3A CF 67 CB D7 EE 6F 58 8B DB 7D 79 BE 6C 69 FE F3 B0 8D DD"

But i am getting the following value:

"x10 x13 x10 x13 x63 x63 x10 x13 x63 x63 x63 x111 x10 x13 x63 x63 x63 x111 x63 x15"

The code i am using is :

String Rxstring="";
char[] result = new char[1];
     for (int len = 0; len < result.Length; )
        len += serialPort1.Read(result, len, result.Length - len);
     foreach (char c in result)
        RxString += String.Format("x{0}", Convert.ToUInt16(c).ToString()) + "    ";


I am writing 1 byte of data to COM port using a device(arduino uno). i changed the baud rate but no change in the data. is It the problem of conversion(Convert.ToUint16). Hot to convert it into HEX value.

Please help me guys.

additional information copied from non solution below (it came after solution 1)
Thanks my friend, I edited my code as you mentioned, again the problem exist. Now i am getting values as:

"0A 0D 3F 3F 3F 6F 3F 0F 1B 3F 3F 3F 41 3F 3A 3F 67 3F 3F 3F 6F 58 0B 3F 7D 79 3F 6C 69 3F 3F 3F 3F 3F"

Why i am getting "3F" most times and its ASCII equivalent is "?" ?.Some data matches exactly.But...

I am writing 1 byte of data to COM port using a device(arduino uno). I have changed the baud rate but no change in the data received . is It the problem of conversion(Convert.ToUint16) or does c# cannot recognize the character or problem with visual studio encoding.

解决方案

You''re simply not formatting the value correctly; "x{0}" should be "{0:X2}" and remove the ToString.


And that''s some seriously strange code that probably needs a lot more help.


Thanks my friend, I edited my code as you mentioned, again the problem exist. Now i am getting values as:

"0A 0D 3F 3F 3F 6F 3F 0F 1B 3F 3F 3F 41 3F 3A 3F 67 3F 3F 3F 6F 58 0B 3F 7D 79 3F 6C 69 3F 3F 3F 3F 3F"

Why i am getting "3F" most times and its ASCII equivalent is "?" ?.Some data matches exactly.But...

I am writing 1 byte of data to COM port using a device(arduino uno). I have changed the baud rate but no change in the data received . is It the problem of conversion(Convert.ToUint16) or does c# cannot recognize the character or problem with visual studio encoding.


这篇关于如何从串口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆