Python in the Middle Serial Port Monitor How to
Making a quick Python based serial monitor
For some reason everyone has seemingly forgotten about the poor old serial port! While you don’t see it very often in consumer electronics, it’s still a very important dependecy in developing most embedded systems. Even if you are just using serial data over Bluetooth and/or USB, it is still important to be able to monitor and/or log that data for making new tools or fixing existing bugs.
There used to be support in Windows for such things with PortMon and there were even a couple free to use programs that got the job done. Now none of those things seem to work with modern operating systems or they want to charge you an arm and a leg for something you only need to use occasionally. Sure, we could do this in Linux with socat or something else, but in the embedded world we are often hamstrung to Windows. So what to do??
PySerial and com0com to the rescue!
With a simple and very useful feature hidden away in PySerial and a virtual set of serial ports using com0com, we can get a Python based logging/monitoring application up and running without much impact on the interface. This example will be quick and crude, but is functional and should certainly get you going if you want to make something more robust with threading and such.
Setup com0com and operation
Com0com essentially gives us 2 ‘virtual’ serial ports that are tied together. Why on earth would we want something like that? This allows us to put a ‘spy’ program in the middle of our serial line without any extra hardware. We can send the serial port from the device to our PC, monitor that stream with our Python script while forwarding out everything we read to the first side of the virtual COM port and have our application connect to the other side of our virtual COM port to operate as normal.
- Here is the link: com0com - ewww, yes it is on sourceforge…
Seems a little convoluted, maybe a chart will help!
+---------+ +---------------+ +-----+ | Device |---[PC COM]----| PySerial App |--[V COM A]--[V COM B]--| App | +---------+ +---------------+ +-----+
’’’ Code coming soon!! ‘’’