[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Read COM Port into backup thread

Posted on 2013-11-20
6
Medium Priority
?
468 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Invest in your employees with these five simple steps to improve employee engagement and retention.
Here in this article, you will get a step by step guidance on how to restore an Exchange database to a recovery database. Get a brief on Recovery Database and how it can be used to restore Exchange database in this section!
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.

649 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