Integrating serial snooping into your applications

Integrating serial snooping into your applications

One of the specificities of Yoctopuce serial interfaces is their capacity to listen to messages circulating on an existing line in both directions to reconstruct the communications. Most of the time, it's used to understand how an undocumented protocol works, but you can also use this function in production to monitor and measure an existing system. That's why we have recently added a new API making it easy to integrate serial snooping into your projects.

The aim of designing an application which listens on a RS232 or RS485 serial line can be, for example, to modernize the reporting of measures towards a cloud database, without interfering in any way with the control process itself. By adding an interface which only listens in to the communication line, as we showed you in a previous post, we avoid any modification of the control code.

Another situation where it can be interesting to use serial message capture in production is for tracing: by memorizing all the messages transmitted on the serial line, you can keep a trace of all the measures and all the commands sent on a system, and thus analyze them afterwards in case of occasional malfunction.

Up to now, if the information to be collected was simple enough to be retrieved by the ten genericSensor present in our serial interfaces, you could decode them directly in the serial interface and manage them directly as with Yoctopuce native sensors. But if you needed to access the full content of the exchanged messages, you had to add to your code a loop repeatedly calling :

messagesList = serialPort.snoopMessages(0)

With the new version of our library, you can instead define a callback function which is automatically called by our library each time a message is received, in one direction or the other, at the same time as all the other value change callbacks, like in the following Python example:

def messageSnooper(serialPort, msg):
    dt = str(msg.get_time()) + "ms"
    if msg.get_direction() == 1:
        print(dt + " >> " + msg.get_message())
        print(dt + " << " + msg.get_message())


This method by callback is particularly interesting if you want to add traceability to an application designed with Yoctopuce serial interfaces, as it allows you with only a few lines of code to keep track of all the messages sent or received by your application, including timings, without interfering with the other functions from our libraries which send and receive information on the serial line.

In the same spirit as catching logs that we presented a few weeks back, adding message tracing to your application is a tool which will help long term maintenance. So, think about it from the design stage onwards!

Add a comment No comment yet Back to blog

Yoctopuce, get your stuff connected.