串口接收到的字节不超过127
我有一个程序可以将流字节发送到另一台电脑。 值范围从 0 到 255。我像这样设置我的串行端口
sp.BaudRate = 115200;
sp.PortName = "COM53";
sp.DataBits = 8;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Parity = System.IO.Ports.Parity.None;
sp.ReadTimeout = 0;
sp.Open();
sp.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(sp_ DataReceived);
,然后我有这个
void sp_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
string Mystring = sp.ReadExisting();
byte testbyte = 254;
// Gather all the bytes until 102 is reached
foreach (byte c in Mystring)
{
if(pixelcount<102)
pixel[pixelcount] = c;
pixelcount++;
if (c 126)
Console.WriteLine("big number {0}", c);// biggest number ever printed is 127
}
//got all the bytes, now draw them
if (pixelcount == 102)
{
Console.WriteLine("testbyte = {0}", testbyte);
oldx = 0;
pixelcount = 0;
pictureBox_rawData.Invalidate();
}
}
我的问题是“c”永远不会超过 127。 我在这里缺少什么? 我已经测试了所有编码,但无法解决这个问题。请帮忙。
谢谢 整数91h
I have a program that sends a stream bytes to eother pc.
The values range from 0 to 255. I set up my serialport like this
sp.BaudRate = 115200;
sp.PortName = "COM53";
sp.DataBits = 8;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Parity = System.IO.Ports.Parity.None;
sp.ReadTimeout = 0;
sp.Open();
sp.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(sp_ DataReceived);
and then I have this
void sp_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
string Mystring = sp.ReadExisting();
byte testbyte = 254;
// Gather all the bytes until 102 is reached
foreach (byte c in Mystring)
{
if(pixelcount<102)
pixel[pixelcount] = c;
pixelcount++;
if (c 126)
Console.WriteLine("big number {0}", c);// biggest number ever printed is 127
}
//got all the bytes, now draw them
if (pixelcount == 102)
{
Console.WriteLine("testbyte = {0}", testbyte);
oldx = 0;
pixelcount = 0;
pictureBox_rawData.Invalidate();
}
}
My problem is that "c" is never over 127.
What am I missing here?
i've test all encoding but i can not solve this problem. please help.
thanks
int91h
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想获取原始字节,您应该使用 SerialPort.Read 将其读入字节数组。使用SerialPort.ReadExisting将数据读入字符串将强制进行某种转换(即编码将字节转换为字符)。
If you want to get the raw bytes, you should be using SerialPort.Read to read it into a byte array. Using
SerialPort.ReadExisting
to read the data into a string is going to force a conversion of some kind (i.e. encoding will convert bytes to chars).在 SerialPort.Write 的文档中(备注部分):
也许 ReadExisting 的行为类似,并将大于 127 的每个字节转换为 63。
In the documentation for SerialPort.Write (Remarks section):
Maybe ReadExisting behaves similar and converts every byte greater then 127 to 63.
您不是在读取字节,而是在读取文本。它是通过根据 SerialPort.Encoding 属性值转换端口接收的字节而生成的。默认为 Encoding.ASCII,这种编码仅包含字节值 0 到 127 的字符。超出该范围的字节值将替换为“?”特点。
这解释了你所看到的。在您的情况下,选择其他编码不太可能是解决方案,请改用 SerialPort.Read() 。 ReadExisting 的等效项是使用足够大的 count 参数调用 Read()。您将返回任何适合的内容,复制到缓冲区的实际字节数是方法返回值。当输入缓冲区为空时它会阻塞。仅当 e.EventType 不等于 SerialData.Chars 时,才会在 DataReceived 事件处理程序中发生。通常不是问题。
请注意,您对 pictureBox_rawData.Invalidate() 的调用无效。 DataReceived 在线程池线程上运行。您只能触摸 UI 线程上的控件成员。您需要使用 Control.BeginInvoke()。
You are not reading bytes, you are reading text. Which is produced by converting the bytes that the port receives according to the SerialPort.Encoding property value. Which defaults to Encoding.ASCII, an encoding that only has characters for byte values 0 through 127. Byte values out of that range are replaced by the "?" character.
Which explains what you see. Choosing another Encoding is an unlikely solution in your case, use SerialPort.Read() instead. The equivalent of ReadExisting is calling Read() with a sufficiently large count argument. You'll get back whatever fits, the actual number of bytes copied into the buffer is the method return value. It blocks when the input buffer is empty. Which can only happen in the DataReceived event handler when e.EventType is not equal to SerialData.Chars. Not usually a problem.
Beware that your call to pictureBox_rawData.Invalidate() is invalid. DataReceived runs on a threadpool thread. You can only touch control members on the UI thread. You'll need to use Control.BeginInvoke().
正如 Hans Passant 所说,您需要使用 SerialPort.Read()。
像这样的东西会起作用
Just as what Hans Passant said, you need to use SerialPort.Read().
Something like this would work