Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:52:42+00:00 2026-06-12T02:52:42+00:00

I’m trying to multiplex access to a serial port on linux. I’m working with

  • 0

I’m trying to multiplex access to a serial port on linux. I’m working with an embedded system that have only one serial port and it would be nice to have more than one process talking to it.

The common use case is to have:

  • One main program running tests (sending commands and receiving output);
  • Another logging all serial port activity;
  • An user terminal open to send additional commands and/or perform post morten analysis after some error during the tests.

First, I made a simple python script to open n pseudo terminal pairs (plus the serial port) and used a poll statement to direct input/output to the right places:

# Removed boiler plate and error checking for clarity

##### Serial port setup
ttyS = serial.Serial(device, baudrate, width, parity, stopbits, 1, xon, rtc)
ttyS.setTimeout(0) # Non-blocking

##### PTYs setup
pts = []
for n in range(number_of_slave_terminals):
    master, slave = os.openpty()
    # Print slave names so others know where to connect
    print >>sys.stderr, 'MUX > fd: %d pty: %s' % (slave, os.ttyname(slave))
    pts.append(master)

##### Poller setup
poller = select.poll()
poller.register(ttyS.fd, select.POLLIN | select.POLLPRI)
for pt in pts:
    poller.register(pt, select.POLLIN | select.POLLPRI)

##### MAIN
while True:

events = poller.poll(500)

for fd, flag in events:

    # fd has input
    if flag & (select.POLLIN | select.POLLPRI):
        # Data on serial
        if fd == ttyS.fd:
            data = ttyS.read(80)
            for pt in pts:
                os.write(pt, data)

        # Data on other pty
        else:
            ttyS.write(os.read(fd, 80))

This approach works very well if every pty is connected. If there is some unconnected pty, eventually its buffer fills up and it blocks on write. Seems like I need to either know which slaves are connected or some kind of on demand pty opening.

I found a neat trick on this question, on which the guy needs to do just the reading from the serial port part, so I adapted my script:

##### Serial port setup
ttyS = serial.Serial(device, baudrate, width, parity, stopbits, 1, xon, rtc)
ttyS.setTimeout(0) # Non-blocking

##### PTYs setup
pts = []
for n in range(number_of_slave_terminals):
    master, slave = os.openpty()

    # slaves
    print >>sys.stderr, 'MUX > fd: %d pty: %s' % (slave, os.ttyname(slave))
    os.close(slave) # POLLHUP trick

    # masters
    pts.append(master)

##### Poller setup
reader = select.poll()
writer = select.poll()

reader.register(ttyS, select.POLLIN | select.POLLPRI)
for pt in pts:
    reader.register(pt, select.POLLIN | select.POLLPRI)
    writer.register(pt, select.POLLIN | select.POLLPRI | select.POLLOUT)

def write_to_ptys(data):
    events = writer.poll(500)

    for fd, flag in events:

        # There is someone on the other side...
        if not (flag & select.POLLHUP):
            os.write(fd, data)

##### MAIN
while True:
    events = reader.poll(500)

    for fd, flag in events:

    if flag & (select.POLLIN | select.POLLPRI):
        # Data on serial
        if fd == ttyS.fd:
            write_to_tty(ttyS.read(80))
        # Data on other pty
        else:
            ttyS.write(os.read(fd, 80))

Which works, but uses 100% of the CPU because the reader poll is flooded with POLLHUP events.

I imagine I could get what I want if I used TCP sockets instead of pseudo terminals. The downside is that I would have to modify all other scripts that already work with terminals to use sockets (I know I can use socat, I just want something simpler). Besides, there’s all that networking overhead…

So, any ideas?

I don’t mind using other tools, as long as it’s simple to setup. I also don’t mind using other languages, I just like Python the most.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:52:44+00:00Added an answer on June 12, 2026 at 2:52 am

    In the end I wrote a simple TCP server, just like I said I didn’t want to… It works really well though. It uses the same general architecture as the code on the question but with TCP sockets instead of pseudo terminals.

    I posted it here in case anyone wants to use it.

    To call it:

    md:mux_serial> ./mux_server.py --device /dev/ttyS0 --baud 115200 --port 23200
    MUX > Serial port: /dev/ttyS0 @ 115200
    MUX > Server: localhost:23200
    

    I’m using socat on another terminal to access the port directly…

    md:~> socat -,raw,echo=0,escape=0x0f TCP4:localhost:23200
    

    …or to create a pseudo terminal to use inside scripts that require those:

    md:~> socat -d -d pty,raw,echo=0 TCP4:localhost:23200
    2012/10/01 13:08:21 socat[3798] N PTY is /dev/pts/4
    2012/10/01 13:08:21 socat[3798] N opening connection to AF=2 127.0.0.1:23200
    2012/10/01 13:08:21 socat[3798] N successfully connected from local address AF=2 127.0.0.1:35138
    2012/10/01 13:08:21 socat[3798] N starting data transfer loop with FDs [3,3] and [5,5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.