Reading from serial port affecting values

独自空忆成欢 提交于 2021-02-04 20:47:28

问题


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

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.

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;
}

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?

Some thing worth mentioning:

  • 0x7E "~" is my end of line character
  • 200 is the maximum amount of bytes I'll ever receive at one time
  • The UART code is sending data one byte at a time, but only after receiving a proper request

回答1:


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.

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.

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.




回答2:


Try this. I think mostly you hadn't implemented it correctly.

    private byte[] ReadLine(SerialPort serialPort)
    {
        byte[] buffer = new byte[4096];
        int count = 0;

        for (; count < buffer.Length; count++)
        {
            buffer[count] = (byte)serialPort.ReadByte();
            if (buffer[count] == 0x7E) break;
        }

        return buffer.Take(count);
    }


来源:https://stackoverflow.com/questions/18698292/reading-from-serial-port-affecting-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!