33

I need to get data from the serial port of a Linux system and convert it to TCP/IP to send to a server. Is this difficult to do? I have some basic programming experience, but not much experience with Linux. Is there an open source application that do this?

1
  • You might find this useful for the TCP/IP-part, and the serial ports you might get a lot of help here. Keep in mind, everything in Linux is "a file", so you could probably "cat" the serial port. Jan 27, 2009 at 19:14

10 Answers 10

35

You don't need to write a program to do this in Linux. Just pipe the serial port through netcat:

netcat www.example.com port </dev/ttyS0 >/dev/ttyS0

Just replace the address and port information. Also, you may be using a different serial port (i.e. change the /dev/ttyS0 part). You can use the stty or setserial commands to change the parameters of the serial port (baud rate, parity, stop bits, etc.).

10
  • 2
    Typo: change "< cat /dev/ttyS0" to just "< /dev/ttyS0". Function: Might put it in a shell script while : do netcat ... done If there is danger of the file I/O ending, e.g. if it's a modem, loses carrier, etc. Jan 28, 2009 at 2:57
  • 2
    You might also want to look at socat Jan 28, 2009 at 7:02
  • I changed the snippet per Liudvikas. I didn't get a chance to check it last night because of the 3 week old baby at the house! Jan 28, 2009 at 20:16
  • 1
    Amazing! What is the purpose of piping in and out to the same device?
    – devwannabe
    May 29, 2017 at 19:51
  • 1
    It would be nice if someone can edit this answer and explain about the logic behind the < and > , what happend first ? how that works ? maybe it looks trivial, but i need to make some redirections to logfiles also and find that challanging ...
    – chenchuk
    Feb 7, 2019 at 8:48
13

I stumbled upon this question via a Google search for a very similar one (using the serial port on a server from a Linux client over TCP/IP), so, even though this is not an answer to exact original question, some of the code might be useful to the original poster, I think:

  • Making a Linux box with a serial port listen on the TCP port to share the modem: ser2net
  • Using this "shared" modem from another Linux workstation: remtty
1
  • I got here the same way; +1 for providing a link to ser2net, it looks like what I was looking for.
    – Dennis
    Feb 12, 2010 at 4:05
8

You can create a serial-over-LAN (SOL) connection by using socat. It can be used to 'forward' a ttyS to another machine so it appears as a local one or you can access it via a TCP/IP port.

3
  • 2
    According to this page, socat doesn't directly support RFC 2217, but it provides a patch to support it. Mar 18, 2013 at 4:59
  • ttyS? Do you mean ttys? Apr 20, 2018 at 14:39
  • If the client side expects serial ioctls this won't work.
    – andyn
    Nov 2, 2021 at 11:50
6

All the tools you would need are already available to you on most modern distributions of Linux.

As several have pointed out you can pipe the serial data through netcat. However you would need to relaunch a new instance each time there is a connection. In order to have this persist between connections you can create a xinetd service using the following configuration:

service testservice
{
    port        = 5900
    socket_type = stream
    protocol    = tcp
    wait        = yes
    user        = root
    server      = /usr/bin/netcat
    server_args = "-l 5900 < /dev/ttyS0"
}

Be sure to change the /dev/ttyS0 to match the serial device you are attempting to interface with.

3

Open a port in your server with netcat and start listening:

nc -lvp port number

And on the machine you are reading the serial port, send it with netcat as root:

nc <IP address> portnumber < /dev/ttyACM0

If you want to store the data on the server you can redirect the data to a text file.

First create a file where you are saving the data:

touch data.txt

And then start saving data

nc -lvp port number > data.txt
1
  • This will work but it requires access to terminal on the machine that hosts the port. A more robust solution is to use ser2net where the server acts like a regular server for accessing remote resource. Of course this way is more "secure" as you need to physically connect using SSH to the remote system first before you're able to access the serial port but at the same time it is far less convenient than ser2net. Kind of depends on what you need... Oct 13, 2019 at 10:12
3

TCP to Serial Systemd service

When your Linux machine runs systemd (most do), you can create a neat service to make an (USB) serial device available over TCP (telnet).

SECURITY WARNING: Exposing a serial device over TCP is a security risk.

In this example I am using:

  • TCP Port 5900
  • Serial device /dev/ttyUSB0 (used 2 times)
  • Serial speed 115200 Bps

You can edit these in the example below. Check the manual of stty and nc (netcat) for more options.

All commands below assume you are user root. If you aren't execute: sudo su -

The directory /etc/systemd/system should already exist. If it doesn't your system probably is not running Systemd.

Create the file /etc/systemd/system/tcp2serial.service with contents:

[Unit]
Description=TCP to Serial

[Service]
TTYPath=/dev/ttyUSB0
ExecStartPre=/usr/bin/stty -F /dev/ttyUSB0 speed 115200
ExecStart=/usr/bin/nc -k -l 5900
StandardInput=tty
StandardOutput=tty
Restart=always

[Install]
WantedBy=default.target

When you created the file you can execute systemctl start tcp2serial to start the service.

From another Linux computer in the same network you can connect to it using telnet [server] 5900. To exit telnet press Ctrl+] and type quit Enter

When you have edited the service file, execute these two commands:

systemctl daemon-reload
systemctl restart tcp2serial

To make the service start on boot execute:

systemctl enable tcp2serial
2

I had the same problem.

I'm not quite sure about open source applications, but I have tested command line Serial over Ethernet for Linux and... it works for me.

Also thanks to Judge Maygarden for the instructions.

1
  • I am -1-ing this answer as it looks like an ad for commercial product. Maybe it is maybe it isn't - I don't really know however things like accessing resources by remote on Linux is provided pretty much out of the box and no fancy user interface is needed to do it. Oct 13, 2019 at 10:09
1

I think your question isn't quite clear. There are several answers here on how to catch the data coming into a Linux's serial port, but perhaps your problem is the other way around?

If you need to catch the data coming out of a Linux's serial port and send it to a server, there are several little hardware gizmos that can do this, starting with the simple serial print server such as this Lantronix gizmo.

No, I'm not affiliated with Lantronix in any way.

1

I have been struggling with the problem for a few days now.

The problem for me originated with VirtualBox/Ubuntu. I have lots of USB serial ports on my machine. When I tried to assign one of them to the VM it clobbered all of them - i.e. the host and other VMs were no longer able to use their USB serial devices.

My solution is to set up a stand-alone serial server on a netbook I happen to have in the closet.

I tried ser2net and it worked to put the serial port on the wire, but remtty did not work. I need to get the port as a tty on the VM.

socat worked perfectly.

There are good instructions here:

Example for remote tty (tty over TCP) using socat

-1

You might find Perl or Python useful to get data from the serial port. To send data to the server, the solution could be easy if the server is (let's say) an HTTP application or even a popular database. The solution would be not so easy if it is some custom/proprietary TCP application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.