在串行端口中收到的字节永远不会超过127

use*_*810 1 c# serial-port

我有一个程序,将流字节发送到另一台PC.值的范围是0到255.我像这样设置了我的serialport

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);
Run Code Online (Sandbox Code Playgroud)

然后我有了这个

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();
}
}
Run Code Online (Sandbox Code Playgroud)

我的问题是"c"永远不会超过127.我在这里错过了什么?我测试了所有编码,但我无法解决这个问题.请帮忙.

谢谢int91h

Jim*_*hel 6

如果要获取原始字节,则应使用SerialPort.Read将其读入字节数组.使用SerialPort.ReadExisting读取数据到一个字符串是要迫使一些类型的转换(即编码将字节转换成字符).