C# 串行端口中接收的字节从不超过127

C# 串行端口中接收的字节从不超过127,c#,serial-port,C#,Serial Port,我有一个程序,可以向其他pc发送一个字节流。 值的范围为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 Sys

我有一个程序,可以向其他pc发送一个字节流。 值的范围为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();
}
}
void sp_DataReceived(对象发送方,
System.IO.Ports.SerialDataReceivedEventArgs(e)
{
字符串Mystring=sp.ReadExisting();
字节testbyte=254;
//收集所有字节,直到达到102
foreach(Mystring中的字节c)
{

if(pixelcount如果要获取原始字节,应使用将其读入字节数组。使用
SerialPort.ReadExisting
将数据读入字符串将强制进行某种转换(即编码将字节转换为字符).

如果要获取原始字节,应使用将其读入字节数组。使用
SerialPort.ReadExisting
将数据读入字符串将强制进行某种类型的转换(即编码将字节转换为字符)。

文档中的(备注部分):

默认情况下,SerialPort使用AscienceODing对字符进行编码。AscienceODing将大于127的所有字符编码为(字符)63或“?”。若要支持该范围内的其他字符,请将编码设置为UTF8Encoding、UTF32Encoding或Unicode编码

可能ReadExisting的行为类似,并将大于127的每个字节转换为63。

的文档(备注部分):

默认情况下,SerialPort使用AscienceODing对字符进行编码。AscienceODing将大于127的所有字符编码为(字符)63或“?”。若要支持该范围内的其他字符,请将编码设置为UTF8Encoding、UTF32Encoding或Unicode编码


可能ReadExisting的行为类似,并将大于127的每个字节转换为63。

您不是在读取字节,而是在读取文本。该文本是通过根据SerialPort.Encoding属性值转换端口接收的字节生成的。该属性值默认为Encoding.ASCII,该编码仅包含字节值0的字符到127。超出该范围的字节值将替换为“?”字符

这就解释了您看到的情况。在您的情况下,选择另一种编码不太可能是一种解决方案,请改用SerialPort.Read()。ReadExisting的等效方法是调用Read()使用足够大的count参数。您将得到任何合适的结果,复制到缓冲区中的实际字节数就是方法返回值。当输入缓冲区为空时,它会阻塞。这只能在e.EventType不等于SerialData.Chars时在DataReceived事件处理程序中发生。通常不是问题


请注意,对pictureBox_rawData.Invalidate()的调用无效。DataReceived在线程池线程上运行。您只能在UI线程上触摸控件成员。您需要使用control.BeginInvoke().

您不是在读取字节,而是在读取文本。该文本是通过根据SerialPort.Encoding属性值转换端口接收的字节生成的。该属性值默认为Encoding.ASCII,该编码仅包含字节值0到127的字符。超出该范围的字节值将替换为“?”字符

这就解释了您看到的情况。在您的情况下,选择另一种编码不太可能是一种解决方案,请改用SerialPort.Read()。ReadExisting的等效方法是调用Read()使用足够大的count参数。您将得到任何合适的结果,复制到缓冲区中的实际字节数就是方法返回值。当输入缓冲区为空时,它会阻塞。这只能在e.EventType不等于SerialData.Chars时在DataReceived事件处理程序中发生。通常不是问题


请注意,对pictureBox_rawData.Invalidate()的调用无效。DataReceived在线程池线程上运行。您只能在UI线程上触摸控件成员。您需要使用control.BeginInvoke()。

正如Hans Passant所说,您需要使用SerialPort.Read()

像这样的东西会有用的

'retrieve number of bytes in the buffer
Dim bytes1 As Integer = ComPort.BytesToRead

'create a byte array to hold the awaiting data
Dim comBuffer As Byte() = New Byte(bytes1 - 1) {}

'read the data and store it to comBuffer
ComPort.Read(comBuffer, 0, bytes1)

正如Hans Passant所说,您需要使用SerialPort.Read()

像这样的东西会有用的

'retrieve number of bytes in the buffer
Dim bytes1 As Integer = ComPort.BytesToRead

'create a byte array to hold the awaiting data
Dim comBuffer As Byte() = New Byte(bytes1 - 1) {}

'read the data and store it to comBuffer
ComPort.Read(comBuffer, 0, bytes1)

您可能需要检查是否接收到负数。您可能需要检查是否接收到负数。