Solved

Read COM Port into backup thread

Posted on 2013-11-20
6
459 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
ID: 39665440
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
ID: 39709604
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
ID: 39710535
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:l2Bravo
ID: 39711194
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
ID: 39711785
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
ID: 39715053
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
This video demonstrates basic masking and how to edit the mask to reveal the desired image.
This video shows how use content aware, what it’s used for, and when to use it over other tools.

860 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