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 8465143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:57:27+00:00 2026-06-10T14:57:27+00:00

I am dealing with an Industrial Environment where I must record readings from a

  • 0

I am dealing with an Industrial Environment where I must record readings from a serial device out in the field. My current setup involves the device connected to a Moxa 5150A or 5130 Serial Device Server which connects to my network thru wired or wireless means depending on the location of the individual device. At current I am reliant upon software from my hardware vendor to manage the data collection. The current software is windows X86 based and has cause no end of trouble on newer hardware we have purchased due to the failure of previous hardware. I selected the Moxa serial servers specifically for their linux drivers and compatibility. The device communicates over RS-422 (4-wire Half Duplex) 7-bit 1-stop odd-parity and NO-flow control and does not send any information unless requested.

I am working to develop my own solution that will store the information in a database (probably MySQL). In terms of terminal communication this is two part. I must send a request and receive the response.

To date I have installed the Moxa drivers for the serial servers onto my linux server. I have written a bash script that reads from the /dev/ttyrXX device, interprets the input and loads the information into a mysql database.

      while read line
      do
      something something....
      done < /dev/ttyrXX

I am still working out minor scripting bugs such as restarting properly when a device goes offline and later comes back online and other such issues, but my script is reading and logging my data well within my needs.

This solution relies on two factors.

1) the Moxa Devices support FIFO and allow multiple connections simultaneously

2) the vendor provided software running on a separate windows machine is currently submitting requests for information.

I need to remove the windows based software all together. I have written and tested a script that prints to /dev/ttyrXX making my desired requests and received the appropriate responses and had them logged as desired by my first script.

     some list of things
     for x in list
     do         
     printf "request" > /dev/ttyrXX
     done

One of the major issues I have experienced with the vendor supplied software is its hard coded dependence on receiving a timely response to every single request. This creates a number of issues including completely freezing up windows and crashing should the device fall out of service at an inopportune time.

My two scripts working in unison collect the data at a significantly better interval and with alot less human intervention.

Here is my question. What method can I use in Python to obtain comparable results to my Bash script?

I have been unable to locate a Python equivalent to printf or cat that does not require me to open/close a file. Furthermore every terminal interface I’ve looked at has the built-in or required wait for response or read for so long before giving up. I’m certain that something exists out there I have simply had little to no luck finding it. I’ll admit that I am brand spanking new to Python and have not written more than a few lines of code to poke around at its abilities.

Let me be very clear. I want to write to my terminal without concern for anything else when I’m writing (using a thread or separate script I don’t care). I want to read from my terminal without a care in the world for when my next bit of text is coming. (I’m not suggesting a never ending loop, but I do need it to simply stand by and wait for input). I WANT/NEED my write to and read from to be completely independent of one another with no concern for what the other is doing yesterday, right now, or next week.

Thank You in advance.

I will be able to provide more detailed code if that is necessary, However I feel that I am in more need of direction in my searching than for bug fixing. Though I am still completely open to advice and “Don’t EVER DO THIS…. BECAUSE OF THIS…” type input

  • 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-10T14:57:29+00:00Added an answer on June 10, 2026 at 2:57 pm

    To expand on what Ignacio is saying. It sounds like the simplest possible solution would be to have two scripts which are started on boot.

    One of them opens opens the tty device and listens to it forever. This is a blocking read. It continues to look for new lines of input until it receives (or reaches) an EOF. Presumably this will never happen in this case. It would look something like this:

    # tty reader
    
    def listen_to_device():
        with open('/dev/ttyXX', 'r') as tty:
            for line in tty:
                create_db_record(line)
    
    if __name__ == '__main__':
        listen_to_device()
    

    The only thing you would need to implement for that to be a complete working bit of code is the ‘create_db_record’ function.

    The second script could be very similar. But maybe it would keep on looking for a particular file in a directory, that contains commands. When it finds any it sends them to the tty device one after the other.

    # tty controller
    
    import os
    import shutil
    import time
    
    def wait_for_commands():
        while True:
            if os.path.exists('/tmp/commands.txt'):
                with open('/tmp/commands.txt', 'r') as c:
                    for command in c:
                        send_command(command)
                shutil.move('/tmp/commands.txt', 'tmp/proccesed-' str(datetime.utcnow()))
            time.sleep(1)
    
    
    def send_command(command):
        with open('/dev/ttyXX', 'w') as tty:
            # maybe do some additional changes to command here?
            tty.write(command)
    
    
    if __name__ == '__main__':
        wait_for_command()
    

    You could add some additional handling code into the send_command function so that your instructions inside the commands.txt file become very simple.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While dealing with JSON returned from YQL, I found myself looking for a way
I'm working on an industrial project dealing with large image processing (50Mo per image)
With dealing with fonts, is there a way to figure out what the space
When dealing with multiple classes in a Javascript/NodeJS game, I'm having trouble figuring out
When dealing with a count mismatch from fwrite (and therefore error), dealing with the
Currently I am dealing with an HtmlDocument in c# from a website: return doc.DocumentNode.SelectSingleNode(//span[@title=input]).InnerText;
Dealing with some legacy code and in trying to get a poorly designed database
Dealing with Java - which the option you prefer in the most cases -
When dealing with a series of numbers, and wanting to use hash results for
When dealing with a single file, I'm used to: /blah do some work n

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.