This is a project to demonstrate how to use the raspberry pi to interface to an industrial RS232 device and send it's data elsewhere over an ethernet WiFi link. This could be done over a hardwired ethernet link or as in this case over the WiFi to a computer several metres away.
Industrial protocol converters are quite expensive so it makes sense to do this with something as powerful as the raspberry Pi.
Device | Path | Device | Path | Device |
---|---|---|---|---|
Gyro compass | RS232 lead (0.5m) | Raspberry Pi | Wifi (10m) | My laptop |
Here we're using a marine compass 'CDL INSENSE' which communicates using a protocol called 'TOGS' over RS232 - this reports heading, pitch and roll of the device.
Firstly we need to connect out compass to our raspberry pi. Here we've used a commercial board to do the interfacing since we need to mount it in panel and this unit has several mounting options as well as some 24V interfacing we may use to set the network address or just indicate status. (https://www.piio.co.uk) but you could just as easy use a USB device etc. Interfacing to RS232 requires a minimum of 3 wires Tx,Rx and GND.
Compass | Raspberry Pi |
---|---|
Tx | Rx |
Rx | Tx |
GND | GND |
24V | - |
0V | - |
- | 5V power |
Next log onto your router and get your Pi's IP address, you can use this to remote log on vis SSH using a terminal program like putty.
Download putty or some other ssh terminal program if you wish to develop this in 'headless mode' - this allows us to develop on the Pi from another computer such as a laptop.
Next we can log on with ssh and use a program called minicom to talk to the compass locally from the raspberry pi.
[minicom -D /dev/ttyAMA0 -b19200) opens up a local terminal on the pi to communicate with the serial port and thus the compass. This just lets us view data from the port and send characters to the connected device using the keyboard.
Here we see the data back from the device via the ssh session, we can use minicom to enter a setup mode to setup the device now if required (type menu). As it is we're happy so we're going to try and write our python program now.
A good strategy here is to copy the string to the clipboard then develop the initial code using an online python compiler. Here we're using https://www.programiz.com/python-programming/online-compiler/ . We use this to quickly prototype some code to parse the incoming RS232 string we received earlier. It's convenient here to just check this works and doesn't crash if the string changes (see github example for more robust implementation).
Once this is done we can paste this into our putty ssh session and test it with the real device with some code running locally. Here we just run the python program on the local pi using the real 232 port again. Now the code is parsing the incoming string on the Pi.
We can now write some code to send this data out over UDP using the network to another computer which may be located several metres away. Here I'm using a program called 'pcomm terminal emulator' which can receive UDP messages on a windows PC. We'll use this to temporarily test that we can indeed send the data.
The code to send out data over UDP is quite straightforward in python. We just need the target IP and port, here we've added a header string s well so we can easily identify the string.
We can use this to send our parsed string back to another computer (which doesn't have an RS232 port).
Creating our output MESSAGE is very easy in python, we add our header as discussed earlier.
We can now use our UDP string monitoring program to receive the RS232 data from the Pi Remotely. This is just 10m away here but could be read back anywhere on a much larger network, RS232 is generally limited to 10-20m, here se can send the data any distance we like.
So it really is that easy to make a protocol converter using the raspberry pi.
Complete code listing available at....
https://github.com/lawsonkeith/PiIO/tree/master/examples/PiIO_H_boards/232_H
Featured IO board
https://piio.co.uk/piio-232-h/
https://www.tindie.com/products/industry/raspberry-pi-r232-interface-board/