30

I am developing an application that should be able to write to a virtual serial port and receive data through the same port from remote clients over network.

The application runs on a linux server. I am new in using serial ports and I have some questions on this topic.

Clients

The client can establish a TCP connection to a server. When we setup a client, we have to provide the IP address of the server, a tcp port (usually 8080) and a virtual com port.

The client then will automatically try to connect to the server.

Server

The server has a virtual com port, the same we set in the client config (e.g. COM1). When an application on the server writes data to this port, the data should be send to all clients connected via tcp. The response from the clients is send over TCP back to the server which can read it over the virtual serial port.

Question

On windows I used a virtual serial port connector http://www.eterlogic.com/Products.VSPE.html which did most of the work. However I want to solve this problem on linux machines.

My question is, how can I create a TCP server that has a virtual serial port attached and can send/receive data through this port over TCP to listening clients?

user207421
  • 305,947
  • 44
  • 307
  • 483
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

3 Answers3

44

Try socat. Possible scenario:

socat  pty,link=/dev/virtualcom0,raw  tcp:192.168.254.254:8080&

socat creates TCP connection to 192.168.254.254:8080, so that everything, that will be written to /dev/virtualcom0 will be forwarded to 192.168.254.254:8080 and vice versa.

Another approach would be to use RFC2217 via ser2net on Linux sever side and RFC2217 driver on Windows side (for example http://www.hw-group.com/products/hw_vsp/index_en.html single port version). You can also try to get http://pyserial.sourceforge.net/ to work with ser2net.

yegorich
  • 4,653
  • 3
  • 31
  • 37
  • Thanks, does this create the virtual com port? I think this just links an existing one to the tcp server. However, I already did what you say here http://superuser.com/questions/733552/how-to-send-message-to-virtual-serial-port-created-with-socat my problem is that I cannot write to the com port. – DarkLeafyGreen Mar 27 '14 at 13:35
  • `socat` would create `/dev/virtualcom0`, so this is a virtual port. I'm using socat with a [VScom NET-CAN 110](http://www.visionsystems.de/produkte/422.html) device and it is working like a charm. The instructions are [here](http://faq.visionsystems.de/index.php?action=artikel&cat=8&id=71&artlang=en&highlight=socketcan). – yegorich Mar 27 '14 at 15:29
  • 1
    and can you say what virtualcom0 would be on windows? If I want to connect from windows client to linux that has virtualcom0 port, what should I select on windwos, COM1, COM2...? – DarkLeafyGreen Mar 27 '14 at 15:32
  • How do i disconnect this bridge? – Marlord Dec 26 '16 at 15:39
  • 1
    @multiplayer1080 try `killall socat` – yegorich Dec 28 '16 at 07:45
  • @yegorich, do you think [this adapter](https://www.amazon.co.uk/dp/B00UR3Q6X0/ref=pe_385721_37986871_TE_item) would also work? – mickadoo Jan 04 '17 at 10:55
  • @mickadoo if the server supports TCP raw mode (haven't tried UDP so far) i.e. transparently sending/receiving data from/to TCP port, it should work. – yegorich Jan 05 '17 at 06:06
4

you have socat and ser2net and other programs but my experience is very bad... not working properly. I've done this small python program, can be useful. Update port, baudrate... then use any tcp client. Remove first line if don't want to use is as auto executable script

#!/usr/bin/python

import socket
import sys
import serial

#open serial port
ser = serial.Serial('/dev/ttyAMA0', 115200, timeout=0)
#create socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

#bond to the port. Don't use localhost to accept external connections
server_address = ('', 2105)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

#listen
sock.listen(1)

#loop
while True:
    #waits for a new connection
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('connection from', client_address)
        #continously send from serial port to tcp and viceversa
        connection.settimeout(0.1)
        while True:
            try:
                data = connection.recv(16)
                if data == '': break
                ser.write(data)
            except KeyboardInterrupt:
                connection.close()
                sys.exit()
            except Exception as e:
                pass
            received_data = ser.read(ser.inWaiting())
            connection.sendall(received_data)
    except Exception as e:
        print e

    finally:
        #clean up connection
        connection.close()
Martin Valgur
  • 5,793
  • 1
  • 33
  • 45
1

The software will help to establish server and client connection over TCP http://www.serial-com-port.com/

I use it for creating virtual serial communications over network, but I have the real RS232 port on the computer. So I just transfer the data over network. If you need to create a virtual COM on the server too, use the Virtual Serial Port Driver.