Click here to Skip to main content
12,255,210 members (33,610 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET serialport
How to get values form two serial ports in VB.Net. Can you give some Example Codes using VB.Net?
Posted 3-Nov-12 2:42am
Edited 17-Jan-13 18:46pm
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100