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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…