Skip to content

Instantly share code, notes, and snippets.

@amecklenborg
Forked from DraTeots/ComPort over Network.md
Created November 18, 2022 12:58
Show Gist options
  • Select an option

  • Save amecklenborg/7c3d73ade795e5ecd2ee26f24d20df4f to your computer and use it in GitHub Desktop.

Select an option

Save amecklenborg/7c3d73ade795e5ecd2ee26f24d20df4f to your computer and use it in GitHub Desktop.
ComPort over Network

Connecting to serial port (com port) over network

(Serial port or com port? - Serial ports are often refered as COM ports. It is the same to be short. You can read abut it in the Wiki article )


The problem

Suppose we have an application that works with some device using serial port (com port). It could be GPS reader, IRDA, whatever. So it looks like this:

+--------+   serial   +--------------+
| DEVICE | ~~~~~~~~~~ | PC  with APP |
+--------+            +--------------+ 

Now what we want, is to have the device connected to one machine (server), and run the application on the remote machine (client) over the network. Real life example: a device is connected to raspberry pi (very small single-board machine) that is connected to a local network, and read the data on a desktop.

Since the application (APP on diagrams) knows only how to communicate with the device by serial port (we suppose), the client machine has to have some virtual serial port that is used by the application:

+--------+   comport  +--------+       network        +--------+  virtual comport +---+
| DEVICE | ~~~~~~~~~~ | SERVER |========....==========| CLIENT |~~~~~~~~~~~~~~~~~~|APP|
+--------+            +--------+                      +--------+                  +---+

So now the application just works with serial port on the client machine, and doesn't even know that data is actually transmitted over the network.


The solution in theory

One of the solutions is using telnet with RFC2217 - Telnet Com Port Control Option. Is solves exactly the problem above. There are a lot of software that supports telnet+RFC2217 serial port forwarding. It allows you to run the server and the client on linux or windows machines (and MACs I suppose, but haven't tested it). Thus can, as example, run linux server and windows client, both using completely different packages, but because of RFC2217 they 'know' how to communicate.

More over you can multiplex the com ports and encrypt the data. Whatever you want.


The solution in practice

WINDOWS

There is an absolutely brilliant free opensoure solution that can be used for comport forwarding, client and server for windows. It is called com0com.

http://sourceforge.net/projects/com0com


Server

For the server you need only hub4com part of it.

Source forge hub4com download link


Configuration (I just cite the documentation):

You have a server computer with phisical COM1 port and you'd like to share it through the network by the RFC 2217 "Telnet Com Port Control Option" protocol:

Start the com2tcp-rfc2217.bat on COM1 port. For example:

com2tcp-rfc2217 COM1 7000

It will listen TCP/IP port 7000 for incaming connections and redirect them to COM1 port.



Client

To be a windows client you have to install com0com virtual comport driver.

Source forge download link

Create a PAIR of virtual comports where one is used for RFC2217 and the other is the port for your application will use.

(documentation citation) for RFC 2217 client :

You have a server computer your.comport.server with physical serial port shared through the network by the RFC 2217 protocol (see above example) and you'd like to use it on the client computer like a virtual serial port.

With the com0com's Setup Command Prompt create COM5<->CNCB0 virtual COM port pair (see com0com's ReadMe.txt for more info). For example:

>setupc.exe
command> install 0 PortName=COM5,EmuBR=yes -

Start the com2tcp-rfc2217.bat on CNCB0 port. For example:

com2tcp-rfc2217 \\.\CNCB0 192.168.123.30 7000

It will redirect virtual serial port COM5 on the second computer to the physical serial port on the first computer.


Driver Signature

Deprecated part - com0com 3.0.0 comes with driver signarure. But if driver signature bugs you...

According to this bug report and my own experience, on Windows 8x64 you probably will get problem with driver installation if you don't have the driver signature verification turned off.

To enable driver test mode and sign a driver for windows, one may download
DSEO

Then run it

Enable Test Mode (if you hasn't done it before) Sign a system file

c:\Windows\System32\drivers\com0com.sys

d:\Tools\ComOverNetwork\com0com\ - is where I installed com0com

Restart the system



LINUX:

The linux app I've got working pretty easy is ser2net

http://linux.die.net/man/8/ser2net

It has configuration file located at /etc/ser2net.conf.

Ubuntu installation

sudo apt-get ser2net            #install
sudo vim /etc/ser2net.conf      #configure
ser2net                         #run service

Linux Server

The configuration line (for /etc/ser2net.conf) that corresponds to windows setup above

7000:telnet:0:/dev/ttyUSB0:1000000 8DATABITS NONE 1STOPBIT remctl

Here:

  1. 7000 - port
  2. /dev/ttyUSB0 - name of serial port
  3. 1000000 ... - baud rate etc (actually you can skip it because of remctl)
  4. remctl - means using remote port configuration as of RFC2217

That is it. Read ser2net docs for more


Linux Client

Mmmm... Can you write it here, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment