如何使用VB.NET访问多个串口 [英] How to Access Multiple serial port using VB.NET

查看:280
本文介绍了如何使用VB.NET访问多个串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从VB.Net中的两个串口获取值。你能用VB.Net?

How to get values form two serial ports in VB.Net. Can you give some Example Codes using VB.Net?

推荐答案

给出一些示例代码吗?我认为你的问题是你试图从另一个事件处理程序内部PInvoke事件处理程序。我从来没有能够非常可靠地工作......你不应该使用PInvoke来调用事件处理程序,除非你有一些非常具体的需要来编组调用跨线程。 (然后你应该调用一个委托...而不是实际的实现)你应该创建一个事件来通知你的代码的主要部分端口需要注意然后RaiseEvent那个事件。



或者,您可以为每个端口创建一个缓冲区,让事件处理程序将数据放入缓冲区,然后在主代码中使用周期性计时器来检查缓冲并处理其中的任何数据。



最后......如果你没有做一堆数据工作(我猜你是将数据从一个端口发送到另一个端口,反之亦然)只需在事件处理程序中完成工作。 COM端口实现已经包含一个接收缓冲区,因此不需要抓取每个字符。您可以获取整个接收缓冲区并在事件处理程序中处理它。如果有更多的角色进入,他们只会被缓冲,那么一旦处理完当前事件,DataReceived事件将再次触发。



我希望这能给你一些方向。你没有包括你的整个项目所以我不能确定你想要做什么以及你可能做出的最佳决定。但与此同时,我不会告诉你什么是最好的决定......你必须自己解决这个问题。



祝你好运!



祝你好运!
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!


这篇关于如何使用VB.NET访问多个串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆