9

I would like to know if exists some utility that connects to serial port and redirect input and output to a tcp port. I need this for a Java application, I need to write and read to a serial port but seems very hard... java will better work with socket connection.

MIND: I found a lot of "virtual com to tcp" but I have a real com! And I want to connect to its.

Thanks.

Tobia
  • 2,203
  • 13
  • 41
  • 64

3 Answers3

13

It's pretty easy. Under Linux there are serial devices, redirection and netcat for that. On the server you can run a netcat process listening on a given tcp port with stdin and stdout redirected to/from the serial device like that:

nc -l 9801 > /dev/ttyS0 < /dev/ttyS0

Where 9801 in this example is the tcp port to listen on. You can setup the serial port with setserial(8).

These device files also exist on cygwin (Windows).

wnrph
  • 3,633
  • 1
  • 26
  • 39
  • Is there a solution also for windows? Usign java I would like to run my app as multiplatform. – Tobia Jul 02 '13 at 12:18
  • Windows: Use Hub4Com, see my [gist](https://gist.github.com/arberg/0378204852551950f4ecb809102df2fb) Linux: With added redirect of input and output to log-files: `cat /dev/ttyUSB0 | tee /var/log/lyngdorfSerial.ComToTcp.log | netcat -l 7000 | tee /var/log/lyngdorfSerial.TcpToCom.log > /dev/ttyUSB0 &` [see full script](https://gist.github.com/arberg/ca4b325632034b218c2de4a353132b4f) – arberg Nov 25 '18 at 10:24
8

I think socat has the functionality you are looking for as well.

$ socat TCP-LISTEN:4161,fork,reuseaddr FILE:/dev/ttyUSB0,b57600,raw
Ian
  • 201
  • 2
  • 4
0

The thing is that you can redirect your serial port with the software, like this , for example https://www.eltima.com/serial-port-redirector/

But, still it will redirect the data from the serial port to another machine without serial connection, thus creating a virtual serial com.