• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

Read COM Port into backup thread

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
l2Bravo
Asked:
l2Bravo
  • 3
  • 3
1 Solution
 
hjgodeCommented:
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
 
l2BravoAuthor Commented:
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
 
hjgodeCommented:
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
l2BravoAuthor Commented:
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
 
hjgodeCommented:
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
 
l2BravoAuthor Commented:
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now