?
Solved

Read COM Port into backup thread

Posted on 2013-11-20
6
Medium Priority
?
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Viewers will learn how to use the Hootsuite Dashboard.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

770 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