Solved

Read COM Port into backup thread

Posted on 2013-11-20
6
449 Views
Last Modified: 2013-12-12
I need to be able to read data from a COM port into a backup thread.  Most of the data I'm receiving for my application is from a separate device that I'm reading on a backup thread. Is there an easy and thread-safe way to receive data from a serial port on the backup thread.  


Incidentally, I need to receive this data no less frequently than 4 times per second.  


Thanks.
0
Comment
Question by:l2Bravo
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:hjgode
Comment Utility
Do I understand you correctly? You want to run a thread that reads data off a serial comm port.

Where is the issue?

If you need to use data between the background thread and the main app you may use a queue that you share between the main app and the background thread.
0
 

Author Comment

by:l2Bravo
Comment Utility
OK, so I posed my question in part because I was used to adding SerialPort objects from the toolbox, but creating them in code worked well enough.  What I'm having a problem with now is related to how to dispose of the serial port correctly when closing the application.  I've encountered some errors with the application when closing it down which I can trace to the serial ports I've added, but don't know what is causing the problem other than an issue with disposing of them.  

Thanks.
0
 
LVL 16

Expert Comment

by:hjgode
Comment Utility
Without seeing some code we cannot say what is wrong with it.

When working with background threads, a problem may be the app shutdown. You first have to shutdown all threads and then you can exit the main app securely.

Creaitin one serial port obejct and closing and disposing it should not be a problem.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:l2Bravo
Comment Utility
Part of the problem, I think, may be that a port (there are 8 ports from which I am reading repeatedly) in the middle of a readline() when the thread is aborted.

Is there a thread termination event I can access to close and dispose of the serial ports before the thread is finally....a thread closing event?


Thanks.
0
 
LVL 16

Accepted Solution

by:
hjgode earned 500 total points
Comment Utility
Do nt be that economical with code snippets.

In general a readline will block and that will also block a thread. Instead of using a Thread.Abort() you may use a variable that is checked in the threads do-while loop.

To avoid an unlimitted lock on the readline, you may use a Thread.Join(thread, timeout) and then check if the thread could be joined. If not you may have to use Thread.Abort(). Before the thread returns you can close the port. The thread.abort() will fire an exception. In the try-catch-finally block you can close the com port in the finally part.

But all this is hard to explain without a code snippet by you.
0
 

Author Comment

by:l2Bravo
Comment Utility
OK.  So, I used bool that is turned on just before I start reading from the ports and is turned off after reading from them.  When the user goes to close the program, the software checks to see if the bool is true and if it does, waits for the operations to complete before aborting the thread.  This solution seems to work and is a product of ideas I got from reading your suggestions.  

Thanks for the help.  If you see any potential problems with my solution, please let me know.  I would assume that the read timeout on the ports will prevent it from locking even if an issue arises with reading from the serial ports during shutdown.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.
This video will demonstrate how to find the puppet warp tool from the edit menu and where to put the points to edit.

763 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

12 Experts available now in Live!

Get 1:1 Help Now