Question

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'.

Was it helpful?

Solution

I'm just going to ignore the underlying problem.

I have no control over the hardware as that aspect was outsourced.

He includes a command where I can check status, so I could when I receive dodgy arrays now just poll all devices for their status. But when all devices start sending me their status I'm more likely to get confused information.

I just discard dodgy data and hope for the best.

I have however started building simple devices in my spare time so I can cover the hardware parts of projects as well.

Best bet would to be use some sort of HUB with all devices attached to it I think?

Any advice or notes welcome.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top