RS485 Sniffer
Posted: Fri Sep 06, 2024 11:44 pm
Hello All,
I have been working an RS485 tool and have reached a point of frustration. I am not professional programmer but when our software engineering do not have the bandwidth or budget I will pretend to be one. Anyway, I am trying to create a custom sniffer tool to replace an old application that was done on VB6. The goal is to parse variable length messages that could easily be as fast as every 50ms. Messages begin with a 0x0a start sentinel and a length byte in the 4th position this would be followed by payload bytes and single byte check sum or two byte CRC. So an idle message would have zero length (No pay load) and be comprised of the following bytes; Start, Address, type, length, pay load bytes , chksum or first crc , next crc. Check sum and crc are calculated against Address, type, length, pay load bytes.
Now my approach:
Lets assume check sum error checking for now. and for testing purposes the data is streaming in at about 100ms
I decided to use a list structure to load the incoming data from the receive buffer. I placed this code into a thread(1). For the parsing/processing I created another thread (2). Thread2 would check for in coming 0x0a/10 if one was found check the length and the see if one byte past the check sum was also start sentinel. If so I would consider the packet good to evaluate for a valid check sum.
I take the prospective message check sum value and compare that to a calculated value by sending Address, type, length, pay load bytes to a check sum function.
If they match I write them to a file with a time hack(ms resolution) appended to the record.
My problem is the code works but randomly I get errors or crashes. Most often it is a " list has no current element" when I know there is more data available in the List. At first I saw this often because the Processing seemed to be consuming the data faster than thread(1 ) could keep it populated. I slowed down the the processing thread(2) based on how low the list size became to allow the Serial input to catch up. That seems to help but impacts the accuracy of the time stamp. I am willing to share the code. There is more detail to the processing that I did not include but there is at least enough detail to begin a discussion. I am wondering if using the List object as a secondary processing buffer was a bad idea. I refuse to believe Purebasic is not up to this task.
I have been working an RS485 tool and have reached a point of frustration. I am not professional programmer but when our software engineering do not have the bandwidth or budget I will pretend to be one. Anyway, I am trying to create a custom sniffer tool to replace an old application that was done on VB6. The goal is to parse variable length messages that could easily be as fast as every 50ms. Messages begin with a 0x0a start sentinel and a length byte in the 4th position this would be followed by payload bytes and single byte check sum or two byte CRC. So an idle message would have zero length (No pay load) and be comprised of the following bytes; Start, Address, type, length, pay load bytes , chksum or first crc , next crc. Check sum and crc are calculated against Address, type, length, pay load bytes.
Now my approach:
Lets assume check sum error checking for now. and for testing purposes the data is streaming in at about 100ms
I decided to use a list structure to load the incoming data from the receive buffer. I placed this code into a thread(1). For the parsing/processing I created another thread (2). Thread2 would check for in coming 0x0a/10 if one was found check the length and the see if one byte past the check sum was also start sentinel. If so I would consider the packet good to evaluate for a valid check sum.
I take the prospective message check sum value and compare that to a calculated value by sending Address, type, length, pay load bytes to a check sum function.
If they match I write them to a file with a time hack(ms resolution) appended to the record.
My problem is the code works but randomly I get errors or crashes. Most often it is a " list has no current element" when I know there is more data available in the List. At first I saw this often because the Processing seemed to be consuming the data faster than thread(1 ) could keep it populated. I slowed down the the processing thread(2) based on how low the list size became to allow the Serial input to catch up. That seems to help but impacts the accuracy of the time stamp. I am willing to share the code. There is more detail to the processing that I did not include but there is at least enough detail to begin a discussion. I am wondering if using the List object as a secondary processing buffer was a bad idea. I refuse to believe Purebasic is not up to this task.