从串口读取影响值 [英] Reading from serial port affecting values

查看:47
本文介绍了从串口读取影响值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从 SerialPort 类正确读取时,我有些头疼.

I'm having a little headache trying to read properly from the SerialPort class.

如果我使用阻塞方法 ReadLine ReadExisting ,则应用程序的工作方式类似于charm,但由于 string ,我的某些值会受到影响转换.即: 0xFC 字节显示为 0x3F (?").我的猜测是,ASCII范围之外的任何值也会丢失.

If I use the blocking method ReadLine or ReadExisting the aplication works like charm, but some of my values get affected because of the string conversion. I.e: the 0xFC byte shows as 0x3F ("?"). My guess is that any value outside of the ASCII scope will get lost too.

我尝试使用 SerialPort.ReadBufferSize Read(byte []缓冲区,int偏移量,int计数)方法来查看应该读取多少字节,但是即使我试图仅从UART读取8个字节,它也总是返回近5000.我必须创建一个函数将此数组修剪为较小的数组并使用它.即使效率不高,我也可以使用此方法,但是有时我会因使用该方法而超出数组执行错误.调试后,我发现读取完整的零数组时会发生这种情况.

I tried using the Read(byte[] buffer,int offset,int count) method using the SerialPort.ReadBufferSize to see how many bytes I was supposed to read, but it is always returning almost 5000 , even when I'm trying to read only like 8 bytes from the UART. I had to create a function to trim this array to smaller one and the use it. I could use this method even though it's not efficient, but I sometimes get array out of bounds execution error using it. After debugging I found out this happens when I read a full array of zeroes.

我最后一次尝试使用此代码:

My last try was using this code:

private void DataRec(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    byte[] myarray = new byte[200];
    int pos = 0;
    bool eol = false;
    while (!eol)
    {
        myarray[pos++] = (byte) sp.ReadByte();
        if (myarray[pos] == 0x7E) eol = true;
    }

    byte[] newarray = Recorta(myarray);
    this.Dispatcher.Invoke(new AtualizaCallBack(this.atualiza), new object[] { newarray });
    sp.DataReceived -= DataRec;
}

在调试此代码时,我从没碰到 byte [] newarray = Recorta(myarray); 断点,但是没有错误.奇怪的是,无论何时再次触发事件(我告诉UART向我发送一个数据包), pos 变量都不会以零开头.

When debugging this I never get to the byte[] newarray = Recorta(myarray); breakpoint, no errors though. A weird thing is that whenever the event is fired again (I tell the UART to send me a packet) the pos variable does not start a zero.

有什么可能发生的想法吗?

Any ideas of what could be happening?

一些值得一提的东西:

  • 0x7E〜"是我的行尾字符
  • 200是我一次将收到的最大字节数
  • UART代码一次发送一个字节的数据,但仅在收到适当的请求之后

推荐答案

您正在使用错误的SerialPort类成员.ReadLine()和ReadExisting()返回字符串,但是您的数据无法存储在字符串中.没有0xFC的ASCII字符代码,因此它只是放弃并产生无法转换该字节"值.这是一个0x3F,一个问号.

You are just using the wrong members of the SerialPort class. ReadLine() and ReadExisting() return strings, but your data cannot be stored in a string. There is no ASCII character code for 0xFC so it just gives up and produces the "cannot convert that byte" value. Which is 0x3F, a question mark.

类似地,除非您重新分配该值,否则ReadBufferSize是一个固定数字.它设置了串行端口驱动程序在程序读取之前用于存储接收到的字节的缓冲区的大小.充其量您会对BytesToRead属性感兴趣.告诉您在该缓冲区内可以读取多少字节.

Similarly, ReadBufferSize is a fixed number, unless you reassign the value. It sets the size of the buffer that the serial port driver uses to store received bytes before your program reads it. At best you'd be interested in the BytesToRead property. Which tells you how many bytes are available for reading inside that buffer.

只需使用SerialPort.Read()方法.并且一定要注意它的返回值,它告诉您读取了多少字节.几乎总是一个很小的数字,通常是一个或两个字节.串行端口很慢.您需要继续阅读,直到获得设备的完整响应.对于您的情况,您得到的是0x7E.

Just use the SerialPort.Read() method. And be sure to pay attention to its return value, it tells you how many bytes were read. Which is almost always a small number, typically one or two bytes. Serial ports are slow. You'll need to keep reading until you got the device's full response. In your case when you got that 0x7E.

这篇关于从串口读取影响值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆