how to call a shared resouce (Serial Port) from multiple threads in - best Practice

Posted on 2014-09-02
Last Modified: 2014-09-08
Hi Coding Experts,

I have several threads that need to occasionally query a serial port.  The threads are constantly running and so it's quite possible that more than one will try to query and send data via the Serial port at any one time.

Whats the best way (pattern?) to do this?  

If I get the threads to query the Port directly, then I expect there would be a conflict when they open the Serial Port.

In Java I could call a synchronized method on an Object, but I'm not sure in Having said this, there may be a better way than that.

Question by:jmsjms
    LVL 21

    Expert Comment

    by:Randy Poole
    You could create a static class that handles this and stores the requests and sends/reads them as needed.  This way you are not having to open and close the port on each read/write.

    Author Comment

    THanks Randy.  This sounds very interesting but I'm not familiar with static classes in  COuld you give me an example of how to use one?
    LVL 21

    Accepted Solution

    Sorry in VB it is called shared, basically instead of making an instance of the class you can call routines or variable defined as shared in the base class itself.
    Here is an example if you would like to read up on it:

    Author Comment

    Right. OK. So I could set the SerialPort as a shared Variable in a class.  

    Then I could instantiate a Class in the calling threads code whenever I need to communicate with the Port. But then a thread calling a method in the class will still not have exclusive access to the Serial port as it could be sending data out whilst another was using another instance to collect...

    I dont think I can just setup a queue of actions as each thread has to wait for the method it calls to complete. (It needs to power off/on serial devices and wait whilst this process completes)

    Author Comment

    I'm currently testing a structure as below:

    Serial port is opened on the main thread.

    Threads all instantiate a Class that handles the SerialPort communication,

    This class has a shared lock.

    When the thread wants to access the shared com port, it calls a method in the class.
    The class's method then trys to obtain the lock.
    If it can obtain the lock, it does the work, if not it waits until it can.

    Seems to work but I've got to do more testing but it looks like the way to go.

    THanks Randy

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now