Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to handle two serialReceiveevents on different COM port

Posted on 2014-01-16
14
Medium Priority
?
163 Views
Last Modified: 2014-03-17
OK, I have two separate  serialdataReceiveEvents on two different COM port that may be firing off at the same time. One SerialDataReceiveEvent is called in the form class the other SerialReceiveDataEvent is called in a different class (not a form class) the problem iI am having and im thinking that they are colliding. Is there any way to avoid this?
0
Comment
Question by:cmdolcet
[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
  • 8
  • 6
14 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39787975
Why should they collide?
Isn't one class linked to one of the two ports and the other class linked to the other port ?
0
 

Author Comment

by:cmdolcet
ID: 39788360
I am only guessing they are colliding because it stalled my program
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39788396
If it is the root of your problem then you probably need to have the receive events in different threads.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cmdolcet
ID: 39803212
How do I do a receive event in a new thread?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39803438
Your main app is in reality a thread.
So you currently do receive the event within a thread.
One object is in one thread, the other object is in the other thread.
0
 

Author Comment

by:cmdolcet
ID: 39833395
OK, It seems that I get an error in my program when inside the frmRutnime_Formclosing event. Its not actually an error the program just does not respond and I need to force a close with a Cltr+alt+Delete.

I just need a better way of handling this issue.
0
 

Author Comment

by:cmdolcet
ID: 39841803
OK, after further investigation, both DataReceivedHandlers are there own SerialDataReceivedEvent and there is a possibility that they can be both fired at the same time. One SerialData ReceiveEvent is an event in the form that monitors the serial port of an incoming signal. The other SerialDataReceiveEvent monitors a completely seperate serial port signal on the class side. What I need is that if the event handler that monitor the serialport on the form is triggered it will take prescience  and not go into the second serialdataeventhandler.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39841900
I don't quite understand your previous comment.
Are you saying my first comment was correct - one class responds only to one port, the other class responds only to the other port (= your guess about a collision was incorrect) ?
Or are you saying both classes respond when one serial port receives a signal ?
0
 

Author Comment

by:cmdolcet
ID: 39842022
Well one DatareceivedEnvent in on the form event and the other DataReceived event is in a class that monitors a different COM port. I have an attached file that outlines both DataReceivedEvents (see attaches). What I did was on the first DataReceivedEvent i placed a variable for COM1 that if it is called at the same time it will throw a flag, if that flag is trow it will get out of the DataReceivedEvents.

Im not sure if there is a better way.
Example-Code.txt
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39842411
I'm still puzzled - when an event is raised on one port do you see activity from BOTH handlers.  It should be easy to test by writing something to a log.
0
 

Author Comment

by:cmdolcet
ID: 39842559
Events are being raised on two separate COM ports. ( one event is on COM 1 and the other is on COM 6) The problem is that it is possible that both COM ports get events on them relatively at the same time. The problem is that if COM 6 gets its event first then it throws the program into a crazy endless loop that I can find my issue. If COM 1 gets its event first then everything works well. If you notice on the frmRuntime the event handler I have boolean COM flags set to if COM 1 is first  and COM6 follow a flag is set in the event handler COM 6 that restricts it from carrying on in its method.

I hope that helps.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39843783
Then you should use a separate thread for handling the work being done on receiving the COM6 event.  That will leave the main thread available for working with COM1 events.
Eg. use a BackgroundWorker:
http://msdn.microsoft.com/en-us/library/System.ComponentModel.BackgroundWorker%28v=vs.110%29.aspx

You can still trap the event as you do at present, just move the processing code into the BackgroundWorker (see DoWork method)
0
 

Accepted Solution

by:
cmdolcet earned 0 total points
ID: 39854223
What I was able to do is set a couple of Boolean flags that determine if I am in the thread of not. COM1 will take precedence and COM2 will follow. If both threads are called and COM 2 is entered it will check for the Boolean flag being set, if not flag then it will continue to execute.
0
 

Author Closing Comment

by:cmdolcet
ID: 39933714
I was able to solve the solution myself
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 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