Sharing USB IO Modules on Linux with the Raspberry Pi
The LucidControl USB IO modules operate with Windows and Linux and they are the perfect solution turning your computer into a data acquisition and control center.
The USB IO modules are able to acquire and control analog and digital signals. They can for example measure temperatures, switch digital lines and generate analog voltages and currents. The analog input module can be used in order to measure voltages of 0-10V and was built in order to realize 0-10V or 4-20mA interfaces.
The USB functionality of the Raspberry Pi B+ and Raspberry Pi 2 B has improved and these RPi versions are able to manage up to 4 USB IO modules.
In the previous article we described how the LucidIoCtrl command line tool can be used and how it can be accessed from a client via secure shell (SSH).
Using SSH is a secure solution how to realize a network device server making LucidControl USB IO modules available e.g. to the Internet. In this case the LucidIoCtrl program is running on the device server and it is started by the client computer.
In this article I describe how to connect to devices connected to a remote network device server by using TCP sockets. TCP sockets can be accessed by an application or they can be used by redirected virtual serial devices on Linux or virtual comports on Windows. Virtual serial devices make it easy to switch between local and remote USB IO modules without changing the application.
Communicating via TCP Sockets
The picture shows the principle of a network device server. The USB IO modules are connected to the USB ports of the network device server which is a Raspberry Pi in this case, but could be any PC running Linux.
The network device server runs ser2net. This tool opens a TCP socket for each configured device and establishes a connection between a local device and the TCP socket. All traffic the TCP socket receives is directed to the device and all data the device send is transmitted over the TCP socket to the destination (over the network).
The USB IO modules are USB/CDM devices creating /dev/ttyACM device names when connected to USB which are treated as standard serial devices (like a comport). This is a very mature concept known from old RS-232 interfaces.
The application ser2net should also work with other devices, but it is a proven concept for serial ports since a long time.
In order to check if ser2net is already installed on your system you check if the init scripts are available or simple try to start or stop the service:
sudo /etc/init.d/ser2net start
These two lines restart the ser2net service. If an error occurs, then ser2net might not be installed on the system what can be done by:
After installation ser2net needs a bit of configuration.
This line opens the configuration file ser2net.conf.
The configuration file contains also a detailed description of all parameters. In order to share a USB IO module the following line must be added to the file.
With this configuration ser2net listens on port 4001 and redirects all data traffic from TCP port 4001 to the device /dev/ttyACM0. The port and the device name can be adapted to your requirements. Please refer also to our article how to create constant device names with Linux when multiple USB IO modules are connected to the network device server. All USB IO modules provide a unique serial number which allows a system wide identification.
Additional lines can be added for other USB IO modules connected to this computer and a 2nd device could be made accessible e.g. on port 4002. All devices can be reached by the same server name or IP address. The devices themselves are than identified by the specified port numbers.
After the file was saved and closed ser2net has to be restarted in order to apply the settings.
Now the connection can be tested.
The LucidIoCtrl command line tool supports connections to TCP sockets since version 1.3.0.
In this example we query the device information from a USB IO module connected to the network device server RPI-AZ-2. The USB IO module is accessible via port 4001. The LucidIoCtrl command line tool shows that a temperature measurement module for PT 100 sensors is connected to this address.
The -d argument defines the device to use. The token tcp: specifies to use TCP socket communication instead of local serial ports. The arguments servername and port specify the server to connect to and the used port. Beside of the server name the IP address is a valid argument here also.
Our LucidControl APIs are available for C#, Java and Python. They will have support for TCP socket communication in the next version.
The application ser2net is a powerful tool when serial ports should be shared over a network. Here I have shown how to connect to a LucidControl USB IO module over the network by using TCP sockets.
Using a network device server running ser2net is a very easy way to share a local USB IO module in a network e.g. by using a Raspberry Pi having 4 USB ports where each device is assigned to a specific port number.
Additionally, the principle described here is the introduction for the further work I will describe on this blog later. Then I will show that it is not even needed to use a TCP socket connection on the client side.
Tools like socat for Linux and com0com for Windows are able to create virtual devices on the client computer which are routed through the TCP socket to the network device server.
Assuming that an existing application is using the USB IO modules via local serial port devices this allows to use the modules on a remote network device server without changing any line of code on the client computer.