I can't figure out what my SerialPort.DataReceived event handler is doing... It gets triggered when there are no data sent by my controller, and just in a very specific case.
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Invoke all code in another method to enable GUI updates from subthread
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)(() => OnDataReceived(sender, e)));
}
else
{
OnDataReceived(sender, e);
}
}
private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
// COPY DATA FROM SERIAL BUFFER AND DISPLAY
SerialPort spL = (SerialPort)sender;
byte[] buf = new byte[spL.BytesToRead];
Console.WriteLine("DATA RECEIVED!");
spL.Read(buf, 0, buf.Length);
foreach (Byte b in buf)
{
Console.Write(b.ToString() + " ");
}
Console.WriteLine();
//Console.WriteLine(BitConverter.ToString(buf, 0, buf.Length)); // hexString
switch (buf[0])
{
case GEN:
if (next_op == rdGEN) // if General data requested, decode it
{
DecodeGEN(buf);
}
break;
case BAS:
if (next_op == rdSingle || next_op == rdAll) // if BAS or All data requested, decode BAS (and continue if All)
{
DecodeBAS(buf);
}
else if (next_op == wrSingle || next_op == wrAll) // if BAS or All data written, decode BAS response (and continue if All)
{
DecodeBASerr(buf);
}
break;
case PAS:
if (next_op == rdSingle || next_op == rdAll) // if PAS or All data requested, decode PAS (and continue if All)
{
DecodePAS(buf);
}
else if (next_op == wrSingle || next_op == wrAll); // if PAS or All data written, decode PAS response (and continue if All)
{
DecodePASerr(buf);
}
break;
case THR:
if (next_op == rdSingle || next_op == rdAll) // if THR or All data requested, decode THR (and discard further data)
{
DecodeTHR(buf);
}
else if (next_op == wrSingle || next_op == wrAll) // if THR or All data written, decode THR response (and discard further data)
{
DecodeTHRerr(buf);
}
break;
}
}
Basically I get a sequence of bytes sent from my controller and then I decode them to update GUI controls. This is an example:
82 24 41 18 0 15 26 35 50 60 70 80 90 100 0 80 85 90 100 100 100 100 100 100 54 1 43
DATA RECEIVED!
The crazy weird thing is that in the only and very specific case where my byte array contains the number "26" as [6] element, the serialPort1_DataReceived gets called an extra time, and basically my program crash at switch buf [0] instruction since the buffer is empty https://i.stack.imgur.com/zf59Y.png. The strange thing is that DataReceived event shouldn't even be triggered with no data being passed! I tryed all accepted values for my program from 0 up to 100, and only "26" triggers this strange behaviour and it makes no sense at all to me...