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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:40:36+00:00 2026-05-18T07:40:36+00:00

I have the following python code that expects data coming from the serial port,

  • 0

I have the following python code that expects data coming from the serial port, and writes it to a file.

import time
import serial

def write_log ( text ):
    f = open('logger.log', 'a')
    f.write( text )
    f.close()

ser = serial.Serial()
ser.port = "/dev/ttyS0"
ser.baudrate = 4800
ser.open()
if ser.isOpen():
    while 1:
        while ser.inWaiting() <= 0:
            time.sleep(1)
            response = ser.read(ser.inWaiting())
            if len ( response ):
                write_log( response )
                print response

It works to an extent, as after some time, it starts to hang, bringing the CPU all the way up, and not writing anything (or sometimes writing only pieces of text) to the .log file.

The process here is pretty intensive, as my serial port will be writing an 8 bytes string every second, and this python script is supposed to then receive it, and write its contents to the log file.

I’m thinking the problem here is the fact that I’m opening and closing the file too much, and this is somehow making the whole process slow. I’m no python wizz, so any help or advice on improving this code would be greatly appreciated.

Thanks in advance,

  • 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-05-18T07:40:37+00:00Added an answer on May 18, 2026 at 7:40 am

    I think the problem here is that you are polling for updates.

    Python’s serial.read() function actually blocks the current thread until something becomes readable on the thread, until timeout. What you should do, therefore, is break out another thread to handle serial IO. Have it loop indefinitely, checking a condition (the master thread wants you to stay listening and the serial port is still available). This thread will do something like:

    while ser.isOpen() && thisthread_should_keep_doing_this:
        response = ser.read(ser.inWaiting())
        if len(response):
            write_log(response)
            print response
    

    Then, when you want it to exit, your master thread sets thisthread_should_keep_doing_this=False and when your slave thread has finished reading, it kills itself.

    Two things:

    • Do have the read timeout relatively frequently.
    • Do not “remote kill” the thread. Pass it a message and have it kill itself. Killing threads remotely creates an awful mess.

    See http://pyserial.sourceforge.net/examples.html#miniterm

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

Sidebar

Related Questions

I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
In Python compiled regex patterns have a findall method that does the following: Return
I have the following code in Python: def point_to_index(point): if point not in points:
I have a question about using os.execvp in Python. I have the following bit
I am a newbie to Python and have come across the following example in
When using IF statements in Python, you have to do the following to make
I'm having troubles getting this to work. Basically I have a python program that
I have written a CGI script that creates an image dynamically using GET data.
I have a piece of code that works in a background process which looks
I have a table that, some of its columns are unknown at compile time.

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.