Linux SOCat & TUN/TAP
References
- HowTo: Virtual Serial Ports on Linux using socat, and more, JustChecking's Weblog.
- Socat: The General Bidirectional Pipe Handler, Linux Developer Network.
- TUN/TAP, WikiPedia.
- Universal TUN/TAP device driver.
- Tun/Tap interface tutorial, backreference.org.
- Lab task1 creating a host to host tunnel using, CourseHero.com (pay wall).
- Socat-fu lesson, Pen Test Partners.
- Accessing a USB IO Module in a Network by using Linux and socat. Lucid Control.
- Using Socat to Simulate Networking Traffic to Test and Debug, Solar Winds.
Send Serial From Desktop Windows PC To Linux Device
I have a program on my Windows laptop that opens a COM device to communicate with a bit of kit. Normally it is connected directly to the kit using a USB to serial device. However, now I want to work downstairs so the kit is now connected to a Linux desktop upstairs using the same USB to serial device.
So, I want the Windows program to open a pseudo COM device, which forwards the serial data over Ethernet to the Linux destop. The desktop should receive the serial data over Ethernet and write it to the real serial device, and send any replies back.
How can this be accomplished? A way that worked for me is this:
On the Windows laptop, run the com0com utility and create a pseudo COM port pair. Call them COM10 and COM11. My windows program can now connect to COM10.
On the laptop, using WSL, run the following socat command:
socat FILE:/dev/ttyS11,cfmakeraw UDP4:<my-desktop-IP>:<random-port-number>
I'm using UDP because the serial protocol being used has error recovery anyway. "UDP" can be replaced with "TCP".
On the Linux box, type the following:
socat UDP-LISTEN:5004,reuseaddr,fork FILE:/dev/ttyUSB0,cfmakeraw,b38400,parenb=0,nonblock=1
Note, if you get permission denied error that you user must be part of the dialout
group.
The paremeter meanings [Ref]
used with the TTY device are as follows:
Parameter | Meaning |
cfmakeraw | Sets the terminal to something like the "raw" mode of the old Version 7 terminal driver: input is available character by character, echoing is disabled, and all special processing of terminal input and output characters is disabled. |
b38400 | Sets the baud rate |
parenb=0 | Disables parity generation on output and parity checking for input. |