Virtual Serial Port For Linux

Virtual Serial Port for Linux

You can use a pty ("pseudo-teletype", where a serial port is a "real teletype") for this. From one end, open /dev/ptyp5, and then attach your program to /dev/ttyp5; ttyp5 will act just like a serial port, but will send/receive everything it does via /dev/ptyp5.

If you really need it to talk to a file called /dev/ttys2, then simply move your old /dev/ttys2 out of the way and make a symlink from ptyp5 to ttys2.

Of course you can use some number other than ptyp5. Perhaps pick one with a high number to avoid duplicates, since all your login terminals will also be using ptys.

Wikipedia has more about ptys: http://en.wikipedia.org/wiki/Pseudo_terminal

How to configure virtual serial port's baud rate

You may apply Socat option b115200 when creating the PTYs.

With something like

stty -f /dev/ttys009 115200

it might be possible to change the speed afterwards.

Create a virtual serial port connection over TCP

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.

How to connect two virtual serial ports to read data from GPSFeed+?

Steps

  • Download gpsfeed+ from https://gpsfeed.sourceforge.io/
  • Make executable the file downloaded:
$ chomod +x gpsfeed+_amd64
  • Install socat package:
$ sudo pacman -S socat
  • Create a pair of virtual serial ports (VSPs) with socat:
$ socat -d -d pty,raw,echo=0 pty,raw,echo=0
  • It displays virtual ports created, in this case: /dev/pts/4 and /dev/pts/5. One port will be transmitter and the another one will be the receiver.
  • Open gpsfeed+ application:
$ ./gpsfeed+_amd64\
  • In Configuration for gpsfeed+ to do:
  1. Connection >> check: Serial, and uncheck: TCP, UDP, Http
  2. Serial/IP >> Port: /dev/pts/4 (transmitter), and Speed: 9600
  • Run simulator (button with a concentric circle as icon)
  • Read data from /dev/pts/5 (receiver)

Python code (install pyserial package):

import serial

ser = serial.Serial('/dev/pts/5', 9600)
iter = 5

while iter > 0:
print(ser.readline().decode("utf-8"))
iter -= 1

The very few existing packages for Dart (like dart_serial_port) do not work with virtual ports.

Emulate serial port

Yes, you can use socat to simulate a serial port.

You need to use socat's PTY address type:

PTY: Generates a pseudo terminal (pty) and uses its master side. Another
process may open the pty's slave side using it like a serial line or
terminal.

The simplest option is:

socat PTY,link=./virtual-tty,raw,echo=0 -

Have the application you are testing opens virtual-tty. Output from your
application will print to the console. Text you type will be sent to your
application.

As noted above, the PTY address type creates a
peudo-terminal. The link
option creates a soft-link between the pseudo-terminal and the given file.
You can choose any filename you wish. Without the soft-link you would need to
open the device and it is difficult to determine the correct one. raw
puts the pseudo-terminal in raw mode. You usually want this as you don't want
any of the special terminal handling options. echo=0 disables echo mode.

If you have (or create) an application that simulates the code executing on
the Arduino, you can connect it via socat as well. If your simulator
comunicates via stdin/stdout, then use the following command:

socat PTY,link=./virtual-tty,raw,echo=0 EXEC:simulator-command

The above connects the stdin/stdout of simulator-command to the
pseudo-terminal.

If your simulator communicates via a serial port as well, then use the PTY
command twice:

socat PTY,link=./arduino-sim,raw,echo=0 PTY,link=./virtual-tty,raw,echo=0

Have your simulator open arduino-sim.

Make a TELNET CONNECTION exposed as a Virtual COM to the OS

as far as kunif just commented my post (thank you again) I would like to share the info as an answer to make everybody has my problem, able to fix it properly.

Following this link you'll get instruction that make you able to spawn a virtual COM to connect to an APP can be link to a generic serial over ethernet.

WINDOWS - ComPort over Network

This works fine with me. Hope it works with you too.

PS. instruction would introduce you to both Windows or Linux approach to the issue.

Ale_Trex

How to create a dummy pipe pseudo-serial device on Linux?

Credit to meuh for his tip-off.

socat UDP:127.0.0.1:5001,bind=127.0.0.1:5000 \
PTY,link=/dev/ttyS0,raw,echo=0,waitslave

This listens on UDP port 5000 on the loopback network interface. All data received is sent to the virtual serial device at /dev/ttyS0. All data received on the virtual serial device is sent to UDP address 127.0.0.1:5001.

The IP address can be remote.

The command must be run as root, as must the process connecting to the serial port. To avoid this use a different file path, e.g. /tmp/ttyS99.

Apparently the file path specified must not already exist. However my PC has /dev/ttyS0 all the way to /dev/ttyS31 despite not having any serial ports, and using /dev/ttyS0 works fine. I suppose if I actually had a real serial port this wouldn't work.



Related Topics



Leave a reply



Submit