串口接收到的字节不超过127

发布于 2024-11-04 18:06:53 字数 1011 浏览 0 评论 0原文

我有一个程序可以将流字节发送到另一台电脑。 值范围从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然#的苍凉 2024-11-11 18:06:53

如果您想获取原始字节,您应该使用 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).

森林散布 2024-11-11 18:06:53

SerialPort.Write 的文档中(备注部分):

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

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

In the documentation for SerialPort.Write (Remarks section):

By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater then 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.

Maybe ReadExisting behaves similar and converts every byte greater then 127 to 63.

撩动你心 2024-11-11 18:06:53

您不是在读取字节,而是在读取文本。它是通过根据 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().

海之角 2024-11-11 18:06:53

正如 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)

Just as what Hans Passant said, you need to use SerialPort.Read().

Something like this would work

'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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文