Click here to Skip to main content
15,896,382 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get values form two serial ports in VB.Net. Can you give some Example Codes using VB.Net?
Posted
Updated 17-Jan-13 18:46pm
v3

1 solution

I think your problem is that you are trying to PInvoke an event handler from inside another event handler. I've never been able to get that to work very reliably and really... you shouldn't use PInvoke to call an event handler unless you have some very specific needs to marshal the call cross-thread. (And then you should be calling a delegate... not the actual implementation) You should probably create an event to notify the main part of your code that a port needs attention then RaiseEvent that event.

Alternately, you could just create a buffer for each port, let the event handler put data into the buffer, then use a periodic timer in the main code to check the buffers and handle any data within them.

Finally... if you aren't doing a bunch of work with the data (I'm guessing you are sending data from one port to the other and vice versa) just do the work in the event handler. The COM port implementations already include a receive buffer so there isn't a pressing need to grab every single character as it comes in. You could grab the whole receive buffer and process it in the event handler. If more characters come in they will just get buffered then the DataReceived event will fire again once the current event is handled.

I hope this gives you some direction. You didn't include your entire project so I can't be sure what you are trying to do and what the best decision for you may be. But at the same time, I wouldn't tell you what the best decision would be any way... you've got to figure that out for yourself.

Good luck!

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900