top of page
Search
tromettherbacarme

Create Virtual Serial Port With Socat and Use Different Address Types



The first pty options creates the virtual serial port. It turns out terminal echo using rawer. The port is created using the file id given by the link option. The EXEC option will run the executable (given by the COMMAND value) and connect to the virtual serial port using stdin & stdout.




Create Virtual Serial Port With Socat




As part of the testing process, I wanted to run the system tests in the CI/CD pipeline. This came with the challenge that the test environment would not have the device connected to the serial port. Thinking about how to handle this, I eventually settled on a neat solution using the socat utility.


Using the tool, I could create a virtual serial port in the test environment. This virtual serial port was configured to pipe all data transferred to an executable (using stdin and stdout). The embedded code was compiled into an executable for the test platform - using a custom runner script and a mock serial API which read from and wrote to stdin and stdout. The systems test just needed to use the virtual port name to run in the test environment without modification.


Being able to create virtual serial ports (which pipe data to stdin/stdout or custom executables) is a really useful feature for testing and debugging programs which use serial ports for communication!


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.


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


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


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.


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.


When the module is loaded, it creates 4 pairs of serial ports. The devices are /dev/tnt0 to /dev/tnt7 where tnt0 is connected to tnt1, tnt2 is connected to tnt3, etc.You may need to fix the file permissions to be able to use the devices.


You may want to look at Tibbo VSPDL for creating a linux virtual serial port using a Kernel driver -- it seems pretty new, and is available for download right now (beta version). Not sure about the license at this point, or whether they want to make it available commercially only in the future.


In Open Source, Remserial (GPL) may also do what you want, using Unix PTY's. It transmits the serial data in "raw form" to a network socket; STTY-like setup of terminal parameters must be done when creating the port, changing them later like described in RFC 2217 does not seem to be supported. You should be able to run two remserial instances to create a virtual nullmodem like com0com, except that you'll need to set up port speed etc in advance.


Socat (also GPL) is like an extended variant of Remserial with many many more options, including a "PTY" method for redirecting the PTY to something else, which can be another instance of Socat. For Unit tets, socat is likely nicer than remserial because you can directly cat files into the PTY. See the PTY example on the manpage. A patch exists under "contrib" to provide RFC2217 support for negotiating serial line settings.


RFC 2217 covers a com port to TCP/IP standard that allows a client on one system to emulate a serial port to the local programs, while transparently sending and receiving data and control signals to a server on another system which actually has the serial port. Here's a high-level overview.


What you would do is find or implement a client com port driver that would implement the client side of the system on your PC - appearing to be a real serial port but in reality shuttling everything to a server. You might be able to get this driver for free from Digi, Lantronix, etc in support of their real standalone serial port servers.


Alternately, the serial port driver source for Linux is readily available. Take that, gut the hardware control pieces, and have that one driver run two /dev/ttySx ports, as a simple loopback. Then connect your real program to the ttyS2 and your simulator to the other ttySx.


But the easiest thing to do right now? Spend $40 on two serial port USB devices, wire them together (null modem) and actually have two real serial ports - one for the program you're testing, one for your simulator.


socat is extremely useful when one end is a closed app with hard coded serial port. In cases like this, you will have to make a symlink from a terminal anyway (after doing the tty magic on your end of the serial link). Might as well simply use socat from a terminal and forget about adding extra code to your end ?


HelloI create a virtual com port,and use socat to let ethernet port communicate with serial portI send the comman which issocat TCP-LISTEN:23000 /dev/ttyS0,b19200,raw,echo=0But when I transmit data in two ports,the baudrate is not 19200Thank you


You can add mode and group to the attributes of your pty by adding a few more expressions to your command line. To follow your example, but with your new serial port owned by the dialout group and with read/write access for that group, do this:


The physical setup is that I currently have the serial port plugged into my laptop so I can see the AT commands coming up the pipe (on ttyS1) I then ssh into the SBC and fire up minicom to view the new "virtual ports" created by socat.


I can type into minicom on the SBC and see the output turning up on the serial port reader on my laptop, likewise I can type into the serial port reader on my laptop and see the input in minicom on the SBC, however what I cannot seem to intercept is the actual AT commands being sent from the c# software even though the software is configured to use ttyS1.


The one bit of control I do have over the software is which port it uses to talk to the modem on. Therefore I have also tried changing this to a virtual port e.g. /dev/ttyV1 and running socat to create the virtual port during startup and before the c# program starts so that the virtual port is actually available. Again I have exactly the same issue where I still cannot see the AT commands being sent by c# software.


Typically there would be a 1st socat command for the first half (the PTY) that would call with EXEC: an executable for traffic alteration (which can be a shell script or python or whatever and will have to manage both sides possibly using up to 4 different FDs) which would call a 2nd socat for the second half (ttyS1).


I need to retrieve config file from guest VM without network support. The simplest way to do that is probably to use serial port. As far as I understand I need to create kind of serial port "loop" so that I'll connect using eg. minicom to lets say /dev/vttyS0 and I'll assign /dev/vttyS1 to VirtualBox guest.


First Setup Serial Console Setting in Host OS. In virtualbox setting, serial Ports --> Port 1Port No. COM1Port Mode - Host Pipeconnect to existing pipe/socked - UNCHECKEDPath/Addess: /tmp/vboxNow, Set Minicomsudo minicom -s -c on Serial port setup Serial Device : unix#/tmp/vbox Exit


I is useful to test new IO device software in PC without actual hardware. Serial communication ofa simulated IO device can be tested either in linux or Windows. In Linux one can use socat to createvirtual serial ports connected together. For example


Here is another reason why it is critical that virtual serial ports are supported by Xojo. Traditional bluetooth modules with a serial port profile, which many people have been using to communicate with hardware wirelessly using serial terminals like CoolTerm, are being phased out in favor of Bluetooth LE hardware. BLE no longer supports serial profiles, so BLE-Serial bridges (such as ble-serial) are necessary to allow access of BLE hardware via virtual serial ports. However, just like serial ports created by socat, these virtual serial ports are not enumerated by SerialDevice and when connecting to these ports using the port name, an error 25 is thrown.


The ser2net program normally installs as a system service and you can edit /etc/ser2net.conf to configure it. I took out all the default lines and added the two serial ports I wanted to expose along with the baud rates I wanted:


I have also had great success with ESPHome on ESP32 (which actually has 3 serial ports, should you need that many). This requires a custom StreamServer component, which I found here: A small modification to the constructor allows multiple instances to be created trivially. I am using this to connect my Texecom Premier alarm panel to my home network, rather than shell out for the official (expensive) COMIP or COMWifi peripherals.


If you're working in an industrial, IoT, or scientific setting you might find yourself communicating with various devices via serial protocols. In my experience, ASCII-over-serial is the JSON of the scientific world in the sense that just about any piece of equipment you buy will have some sort of ASCII/serial support. For example, every single piece of equipment in my experiment uses ASCII-over-serial.


It would be a real shame if you needed a real serial device to even try this out. Luckily, some smart people made a tool called socat which lets you create virtual serial ports. Not only is this great for just tooling around, but it also means that you can test your serial-facing code in a CI environment as opposed to using a hardware loopback adapter or sticking a wire into the TX/RX pins of a serial cable (experimental physicists are half Einstein, half MacGyver). 2ff7e9595c


0 views0 comments

Recent Posts

See All

Comments


bottom of page