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

  • SEARCH
  • Home
  • 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 7798025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:52:37+00:00 2026-06-01T23:52:37+00:00

I am reading lines from a serial connection (pyserial), at the moment I am

  • 0

I am reading lines from a serial connection (pyserial), at the moment I am using a while loop to read the line and then perform a series of functions on that input and then store it in an object ( a range finder ) .

It has been mentioned that I should treat the serial input as a generator, as that is how things are done in python.

Does anyone have any experience with this?
Or could at least explain in principle how this would be achieved?
Why it is better? Is it purely for memory / speed?

EDIT:

where does the function:

at_end()

come from?
I’m getting:

AttributeError: 'Serial' object has no attribute 'at_end'

If I use

while True:
    yield source.readline()

then I get the output.

  • 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-01T23:52:39+00:00Added an answer on June 1, 2026 at 11:52 pm

    You might have a look at the Iterator Types. Basically you implement a class:

    class SerialReader(object):
        def __init__(self, source):
            super(SerialReader, self).__init__()
            self.source = source
    
        def next(self):
            """Provide next piece of data from the serial source."""
            # If we have no more data, we have to raise StopIteration exception
            if self.source.at_end():
                raise StopIteration
            else:
                return self.source.read()
    
        def __iter__(self):
            return self
    
    reader = SerialReader(some_serial_source)
    
    for data in reader:
        do_something_with_data(data)
    

    Advantage is the use of such iterator/generator – you can use it with any python method that accepts iterators:

    • list comprehension: sample = [data for data in serial_reader]
    • itertools
    • qick and simple conversion to a list: list(serial_reader) – will read all the data and will return a list
    • … much more

    Iterator is very pythonic pattern and you can meet quite frequently. Many python libraries make use of iterators.

    Concerning memory usage: imagine you want to process your source with another function that accepts stream of data. You do not have to have load all source data, you just pass the generator (iterator) to the processing function and the data will be read as needed.

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

Sidebar

Related Questions

I'm running a while loop reading each line in a file, and then fork
I'm reading lines from a text file using (line-seq (reader input.txt)) . This collection
Using Delphi 7 I am reading from a serial port. The read is always
When reading lines from a text file using python, the end-line character often needs
I'm reading lines from a file, and I believe that once I've read all
I'm reading values from a sensor via serial (accelerometer values), in a loop similar
I am reading several lines from the console in a java program using the
Possible Duplicate: C# Reading a File Line By Line How to loop over lines
I am reading data from a police scanner using C# and serial ports (USB
My C/C++ program takes a file from the command line as an argument. Reading

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.