c# - 串口接收字节不超过127

标签 c# serial-port

我有一个程序可以将流字节发送到其他电脑。 值范围从 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/

相关文章:

c++ - 如何增加ESP32程式库Hardwareserial(平台IO)的RX序列缓冲区大小

c# - 同一 Controller 中的 AmbiguousActionException

c# - 值的范围 - 如何在没有 if 循环的情况下确定?

c# - 将 List<string>[] 读入动态标签

c++ - 从 Windows 上的 C++ 或 Python 的串口读取

c++ - 如何确定正在使用哪个USB端口?

c# - 在运行时配置 ASP.NET session 状态

c# - Android转WP7绝对新手问题: C# or VB?

serial-port - 115,200 或更高的实际波特率是否可能?

.NET 串行端口多线程