0

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

Erwin
  • 4,757
  • 3
  • 31
  • 41
Dave
  • 161
  • 1
  • 4
  • 18
  • Are the devices using different ports? Since you lock them. The only way this seems possible to me is if you can identify the device that is sending the data. You would then make a dictionary with the name of the device and the array collecting data. – MrFox Aug 30 '12 at 13:19
  • You have multiple counterparts on a COM port? – H H Aug 30 '12 at 13:19
  • 1
    So, `2F` + msg + ChkSum in 4 bytes means the msg is only 2 bytes? – H H Aug 30 '12 at 13:20
  • Are you sure that zero-bytes don't count for your ReadCounter ? – H H Aug 30 '12 at 13:21
  • In this instance Two hexadecimal digits together represent 1 byte, which is equal to 8 bits. – Dave Aug 30 '12 at 13:22
  • I should never receive a 0 byte. – Dave Aug 30 '12 at 13:27
  • Try logging all recieved bytes. If you are really getting them from several sources in any random order, there's no way to put them back. You could note that you received the first byte and push other bytes away until you receive the second byte, but you can't know if the second byte belongs with the first one you stacked or to some other random source (who send his first byte before). – MrFox Aug 30 '12 at 13:50
  • Having multiple devices on a single port will never work. Not only can you not prevent bytes from being interleaved, if they Tx at the same time, they will corrupt the data. Why would you even try to do this? – TJD Aug 30 '12 at 14:44
  • It's the setup I was given to work with, someone the client outsourced to do the hardware aspects of this project. – Dave Aug 30 '12 at 15:15

1 Answers1

0

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.

Dave
  • 161
  • 1
  • 4
  • 18