Windows应用程序中的串行端口/ Com端口 [英] Serial Port /Com Port in windows Application

查看:117
本文介绍了Windows应用程序中的串行端口/ Com端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



在我的窗口应用程序中,我将使用条形码扫描器(串口/ Com端口)。我有20多个窗体。客户可以以任何活动形式扫描条形码。我将使用SerialDataReceivedEventHandler类来获取数据。我必须在每个表单中编写相同的SerialDataReceivedEventHandler代码。那么,我应该在每种形式中使用这个类,或者我如何在mainform中编写所有串口代码并以各种形式访问数据?



如果你有任何形式建议请告诉我。

解决方案

我会将串行例程,即低级串行处理的包装类视为单例代码 - 它们应该出现一次 - 可能在主要形式 - 我有点担心你为什么需要从这么多形式访问它 - 20?



关于如何在表单之间共享信息,有很多链接/代码,但是,我想说你最大的问题是阻止两个表单尝试访问串口一次 - 在form1和form 2中发生什么都试图发送?例如?


首先,我建议使用一个单独的线程进行通信。它会使逻辑方式比这个事件更简单。



但主要问题看起来非常明显:不仅你不应该做你在每种形式中提到的任何事情,而是你应该做一些相反的事情:从一般的UI中彻底隔离每个表单的通信(以及代码的所有其他方面)。



-SA

Hey,

In my window application I will be using barcode scanner through(Serial port /Com port).I have more than 20 windows forms. Customer may scan barcode in any active form. I will be using SerialDataReceivedEventHandler class to get data. I have to write same stuff code of SerialDataReceivedEventHandler in each form. So,Should i use this class in every form or how can i achieve writing all serial port code stuff in mainform and access data in every form?

If you have any suggestions please let me know.

解决方案

I would be treating the Serial routines ie a wrapper class around the low level serial handling as 'singleton' code - they should appear once - possibly in the mainform - Im a bit worried about why you'd need to access it from so many forms - 20 ?

There are plenty of links/code around on how to share information between forms, but, I'd say your biggest issue was preventing two forms from trying to access the serial port at once - whats going to happen in form1 and form 2 both try to send for example ?


First of all, I would suggest to use a separate thread for communication. It would make the logic way simpler than with this event.

But the main issue looks quite obvious: not only you should not do anything you mention in each form, but you should do something opposite: thoroughly isolate communication (as well as all other aspects of the code) from each and every form, from UI in general.

—SA


这篇关于Windows应用程序中的串行端口/ Com端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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