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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:18:06+00:00 2026-06-11T23:18:06+00:00

I am reading data sent to a serial port (that is, COM3) using code

  • 0

I am reading data sent to a serial port (that is, COM3) using code like this:

_serialPort.PortName = "COM3";
_serialPort.BaudRate = 9600;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
_serialPort.Open();

The data coming from that serial port are commands and can be a string of any length ending in ‘\0’. I’m looking for best practices on how to efficiently read such data. I’m thinking a producer/consumer pattern might be best, where:

  1. Every time the DataReceived event fires, I read the string from the port and append to a stringbuffer or some other structure
  2. I then have another thread that constantly inspects the stringbuffer or structure for char ‘\0’ and if found, parses the and remove the command string.

Would this be the most efficient way to handle this? Is there any sort of blocking serial read function that doesn’t rely on the dataReceived event?

  • 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-11T23:18:07+00:00Added an answer on June 11, 2026 at 11:18 pm

    I have found that the dataReceieved event can fire too often and does block the SerialPort when code is running in the event. You say you might want this functionality but here is more information that might or might not be useful.

    I use a Timer, often at 10 Hz. In the Timer, I check SerialPort.BytesToRead > 0. Then I use SerialPort.ReadLine which reads one line of text and saves a lot of code to processes the inbound buffer.

    If you Loop until BytesToRead == 0, reading each line either into a buffer for later processing or process directly after the read.

    The ReadTimeout property in your example is useful if you know how long it takes the Arduino to construct a line of text and will cause an Exception if only part of a line is received within the ReadTimeout.

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

Sidebar

Related Questions

I'm reading data from a serial port using an event listener from the SerialPort
I am reading data from a scale using a serial port. Communication is working
I am reading data from multiple serial ports. At present I am using a
I am reading data from XML like this: (Contacts.xml) <?xml version=1.0 encoding=utf-8 standalone=yes?> <!--LINQ
I am having some trouble reading some data from a serial port I opened
I am reading data from a file that has, unfortunately, two types of character
I've got a TCP server written in C# that processes POST data sent to
I have a function on my service that looks like something this: addStatement: function(user,
I'm very new to sending data over a serial port through .net and I've
I'm trying to read raw bytes from a serial port sent by a IEC

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.