RS485 Sniffer

Just starting out? Need help? Post your questions and find answers here.
msteffes
New User
New User
Posts: 6
Joined: Thu Oct 27, 2022 4:35 pm

RS485 Sniffer

Post by msteffes »

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.
User avatar
Erolcum
User
User
Posts: 51
Joined: Fri Jun 07, 2024 10:45 am
Location: Turkiye
Contact:

Re: RS485 Sniffer

Post by Erolcum »

Did you try producer/consumer pattern between your 2 threads ?
https://www.purebasic.com/documentation ... phore.html
You may visit my new Purebasic blog here..
:arrow: https://erolcum-github-io.translate.goo ... r_pto=wapp
msteffes
New User
New User
Posts: 6
Joined: Thu Oct 27, 2022 4:35 pm

Re: RS485 Sniffer

Post by msteffes »

I tried this initially but did not see the benefit of the mutex and semaphore as my code became more complicated. I was advised to use postevent to signal when data was present but that caused problems with the way the main handled UI. I was only using a producer thread at that point and calling the data post processing procedure from the main. I will try to implement the mutex semaphore again now that I have two threads running.
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RS485 Sniffer

Post by infratec »

PB is the right tool for such a case. :wink:

The problem is that you need definately a well done thread procedure which handles the serial data stream.
Then I would do such things in a state machine.

I already did many examples here about serial stuff in a thread.
But without seeing your code .., no help is possible, only hints.
plouf
Enthusiast
Enthusiast
Posts: 281
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Re: RS485 Sniffer

Post by plouf »

Just a hint
Your description sound like you trying to make a pelco sniffer

But there a myriads out there just google for "serial sniffer" and use a usb<>rs485 module . Some or all, most probably fit your needs and you dont need to mess your mind rediscovering the wheel :)

Adaptor is easy found in advanced computer shops or ebay etc

As for more specific example (if needed) as the other said
Your question is very abstract and cant reply "in topic"
Christos
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RS485 Sniffer

Post by infratec »

infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RS485 Sniffer

Post by infratec »

If the packet is complete and ok, copy the packet to a new buffer and use postevent with eventdata to let the main loop write it to the file.

I think there is no need for a list.
Post Reply