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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:12:30+00:00 2026-05-18T01:12:30+00:00

As serial port communication is asynchronous, I figured out early on into my project

  • 0

As serial port communication is asynchronous, I figured out early on into my project involving communication with a RS 232 device that I will have to have a background thread constantly reading the port for data received. Now, I’m using IronPython (.NET 4.0) so I have access to the slick SerialPort class built into .NET. This lets me write code like this:

self.port = System.IO.Ports.SerialPort('COM1', 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One)
self.port.Open()
reading = self.port.ReadExisting() #grabs all bytes currently sitting in the input buffer

Simple enough. But as I mentioned I want to be constantly checking this port for new data as it arrives. Ideally, I would have the OS tell me anytime there’s data waiting. Whaddaya know, my prayers have been answered, there is a DataReceived event provided!

self.port.DataReceived += self.OnDataReceived

def OnDataReceived(self, sender, event):
    reading = self.port.ReadExisting()
    ...

Too bad this is worthless, though, because this event isn’t guaranteed to be raised!

The DataReceived event is not guaranteed to be raised for every byte received.

So back to writing a listener thread, then. I’ve accomplished this rather quickly with a BackgroundWorker that just calls port.ReadExisting() over and over again. It reads bytes as they come in, and when it sees a line ending (\r\n), it places what it read into a linebuffer. Then other parts of my program look at the linebuffer to see if there are any complete lines waiting to be used.

Now, this is a classic producer-consumer problem, obviously. The producer is the BackgroundWorker, placing complete lines into linebuffer. The consumer is some code that consumes those lines from the linebuffer as fast as possible.

However, the consumer is sort of inefficient. Right now he’s constantly checking the linebuffer, getting disappointed each time to find it empty; though every once in a while does find a line waiting inside. What’s the best way to optimize this so that the consumer only wakes up when there is a line available? That way the consumer isn’t spinning around constantly accessing the linebuffer, which might introduce some concurrency issues.

Also, if there is a simpler/better way of reading constantly from a serial port, I’m open to suggestions!

  • 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-18T01:12:31+00:00Added an answer on May 18, 2026 at 1:12 am

    I don’t see why you can’t use the DataReceived event. As the docs state

    The DataReceived event is not
    guaranteed to be raised for every byte
    received. Use the BytesToRead property
    to determine how much data is left to
    be read in the buffer.

    All that is saying is that you aren’t guaranteed to get a separate event for each individual byte of data. You may need to check and see if there more than one byte available on the port using the BytesToRead property and read the corresponding number of bytes.

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

Sidebar

Related Questions

I am trying to use a RS-232 serial port on my PC with javax.comm
I'm working on an App that talks to a serial port, and on my
I need to do some communications over a serial port in Ruby. From my
I'm working on serial port, transmitting and receiving data to some hardware at 8bit
I asked this question: Serial Port (rs232) in Mono for multiple platforms and this
I need to test a serial port application on Linux, however, my test machine
To send a serial string character to the serial port. I would need to
We've got some old serial code which checks whether a serial port is available
I'm trying to open and read from a serial port using the System.IO.Ports.SerialPort class.
Here's some background on what I'm trying to do: Open a serial port from

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.