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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:40:02+00:00 2026-06-17T05:40:02+00:00

I have 3 SerialPort components and 2 of them has DataReceived handlers. When data

  • 0

I have 3 SerialPort components and 2 of them has DataReceived handlers.
When data is received, they are sending output through other SerialPort
(the third one).
I know that the possibility is not high in my application that both SerialPorts will try to send at the same time, but I tried to simulate this and of course I get an error that the COM PORT is being used.

private void sendToCOM(String comNumber, String msg, String speed)
    {
        try
        {
            using (SerialPort comPort = new SerialPort(comNumber, Int32.Parse(speed), Parity.None, 8, StopBits.One))
            {
                comPort.Open();
                comPort.Write(msg);
                comPort.Close();
                comPort.Dispose();
            }
        }
        catch(Exception ex)
        {
                cstFuncs.errorHandler(ex.Message, SettingsForm);
        }
    }

I understand that each SerialPort has it’s own thread.

How can I make a queue or how can I prevent from both serial port to try access the same resources on the same time (and not loose the data that is supposed to be sent out)

  • 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-17T05:40:03+00:00Added an answer on June 17, 2026 at 5:40 am

    There are a few ways you could get locking in the sendToCOM method. This will stop the method from executing twice by causing any subsequent calls to wait until the first is complete. This isn’t going to help if another process tried to use the same SerialPort, but it will stop any local contention.

    One way would be to use the lock keyword on an object like so:

    private readonly object _myLock = new object();
    
    private void sendToCOM(String comNumber, String msg, String speed)
    {
        try
        {
            lock(_myLock)
            {
                using (SerialPort comPort = new SerialPort(comNumber, Int32.Parse(speed), Parity.None, 8, StopBits.One))
                {
                    comPort.Open();
                    comPort.Write(msg);
                    comPort.Close();
                    comPort.Dispose();
                }
            }
        }
        catch(Exception ex)
        {
            cstFuncs.errorHandler(ex.Message, SettingsForm);
        }
    }
    

    Another way could be to use a ManualResetEventSlim (or something similar) to achieve the same effect:

    private ManualResetEventSlim _myLock = new ManualResetEventSlim(true);
    
    private void sendToCOM(String comNumber, String msg, String speed)
    {
        _myLock.Wait();
        try
        {
            _myLock.Reset();
            using (SerialPort comPort = new SerialPort(comNumber, Int32.Parse(speed), Parity.None, 8, StopBits.One))
            {
                // ..
            }
        }
        catch(Exception ex)
        {
            cstFuncs.errorHandler(ex.Message, SettingsForm);
        } 
        finally
        {
            _myLock.Set();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an open SerialPort and receive data through the DataReceived event. Is there
My problem: I have DataReceived event for SerialPort, which listens to serial port and
I have some SerialPort code that constantly needs to read data from a serial
Let's say I want to have a function which reads data from the SerialPort
I have C# multithreaded application that has to interface with a hardware using SerialPort.
I have a module with an event for serial port signal serialPort.DataReceived.AddHandler(SerialDataReceivedEventHandler(DataReceived)); where DataReceived
I have a piece of hardware that I'm connecting to using the .NET SerialPort
I have to monitor a serial port and process its data. As a test
I previously have been reading NMEA data from a GPS via a serial port
I have a winform program that does some asynchronous IO on a SerialPort .

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.