Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

Your app connects to devices (ECS777, Glory300, etc.) via a serial port, but you don’t have the real hardware and still want to test and improve your code. Here’s a simple solution.

The architecture looks like this:

##################################### Linux os ############################################
###   [your app] <---->[/dev/ttychan] <====> [/dev/ttychansim] <---->  [dummyData app]  ###
##################################### Linux os ############################################

How to implement

Socat can create two bidirectional byte streams and link them together, allowing data to flow between /dev/ttychan and /dev/ttychansim. Currently, Socat is available in the package repository and you can install it using the command below

Strengths

  • Simple and easy to set up test data: Socat can connect almost anything to anything (TCP, UDP, UNIX sockets, PTY/TTY/Serial Ports, SSL, etc.). This makes it easy to simulate devices using Serial/COM ports and send data just like a real device
  • Supports autotesting: Since it is a command-line tool, Socat is ideal for automation (CI/CD pipelines, Bash/Python scripts). It can run in the background and create virtual port pairs for testing without manual work.

Weaknesses

  • Installing and using it through the command line can be difficult
  • Test data is fixed, so you need to create many different cases manually.Socat only forwards data. It cannot automatically respond with custom rules (e.g., “if command A is received, reply B after 500ms”). You need extra scripts (Python, Perl, etc.) for that

Installation

    # apt install socat
    # socat -V
        socat by Gerhard Rieger and contributors - see www.dest-unreach.org
        socat version 1.8.0.0 on 08 Apr 2024 14:50:22

A simple example

Suppose [yourApp] connects to a real hardware through /dev/ttychan, and you want to create a Python application [dummyData] to simulate the data sent to [yourApp].

Refer to the diagram below:

  • yourApp.c file
...
    const char *portname = "/dev/ttychan"; 
    int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    set_interface_attribs(fd, B9600, 'n');
...
...
    ser = serial.Serial(
        port='/dev/ttychansim',
        baudrate=9600
...
  • Create two bidirectional streams to allow data to flow between /dev/ttychan and /dev/ttychansim.
   sudo socat -d -d PTY,link=/dev/ttychan,raw,echo=0,b9600 PTY,link=/dev/ttychansim,raw,echo=0,b9600 &
  • Testing
Start dummyData

Start yourApp

Conclusion

The challenge of testing serial port applications without physical hardware is easily solved using Socat. By creating linked Virtual Serial Ports (PTY), Socat provides a simple, command-line solution that perfectly simulates real device communication. This immediately decouples development from hardware availability, making it an ideal method for autotesting and continuous integration pipelines. While complex device logic still requires custom scripting, the core barrier to flexible, automated testing is removed.

References:

https://man7.org/linux/man-pages/man1/socat.1.html

Ready to get started?

Contact IVC for a free consultation and discover how we can help your business grow online.

Contact IVC for a Free Consultation
Written by
Author Avatar
Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

COMPANY PROFILE

Please check out our Company Profile.

Download

COMPANY PORTFOLIO

Explore my work!

Download

ASK ISB Vietnam ABOUT DEVELOPMENT

Let's talk about your project!

Contact US