我有一个程序可以将流字节发送到其他电脑。 值范围从 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。 我在这里错过了什么? 我已经测试了所有编码,但我无法解决这个问题。请帮忙。
谢谢 int91h
最佳答案
如果你想获取原始字节,你应该使用 SerialPort.Read将其读入字节数组。使用 SerialPort.ReadExisting
将数据读入字符串将强制进行某种类型的转换(即编码会将字节转换为字符)。
关于c# - 串口接收字节不超过127,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5848907/