I'm writing this with C# and .net 2.0
I'm being sent a 4 byte array by a device as individual bytes.
I currently read this in the following way
while(m_ReadThreadisRunning)
{
if(canRead)
{
lock (m_Serialport)
{
try
{
//Check if data read needs reset
if(DataRead[3] != 0)
{
m_ReadBuffer.Add(DataRead);
DataRead = new byte[4];
ReadCounter = 0;
}
int ByteRead = m_Serialport.ReadByte();
Debug.Log("Byte : " + ByteRead);
try
{
DataRead[ReadCounter] = ConvertIntToByte(ByteRead);
}
catch(Exception e)
{
Debug.Log("Error when setting element in DataRead");
}
finally
{
if(ByteRead != 0)
ReadCounter ++;
}
}
catch(TimeoutException e)
{
}
catch(Exception e)
{
ReadCounter = 0;
}
}
}
}
The problem is that multiple devices can send me information at the same time and this causes some data to end up in wrong arrays.
A byte array being sent to me always starts with the hex '2f' and ends with the check-sum.
Any advice on what I can do to handle this would be greatly appreciated.
[EDIT]
Sorry, I knew I'd miss out important information. All devices are attached to the same port. The Byte array is in the following order :- '2f' 'Device id' 'msg' 'Check-sum'.