所有窗体中的串行端口数据访问 [英] Serial Port Data access in all windows forms

查看:84
本文介绍了所有窗体中的串行端口数据访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,



我在Windows应用程序中工作。我们使用条形码阅读器登录客户。应用程序的当前架构师是以每种形式创建SerialPoet Class对象并从DataRecieve接收事件数据。以前的开发人员无法制作单例类SerialPort并以各种形式提供访问数据。他在每种形式中创建了serialport类的对象来访问serialport数据。现在,我想改变应用程序的体系结构。我想创建一个创建串口对象的单个类,这个对象为当前的Active form提供数据。所以,我创建了Single Class SerialPortCommunication,其中我创建了SerialPort类的对象我调用了Serialport.open()方法,但是当我调用方法时出现错误调用的目标引发了异常。请给我一个基本的想法,我如何才能充分利用建筑。



以上问题可能很棘手或不易理解。请免费问我问题。

Hello Guys,

I am working in windows application. We use barcode reader to login the customers. Current architect of application is in each form SerialPoet Class object is created and from DataRecieve Event data is received. Previous Developer was not able to make a singleton class SerialPort and give access data in every form. He created object of serialport class in every form to access the serialport data. Now, I want to change the architecture of the application.I want to create a single class which create object of serial port and this object provides data to current Active form.So, I created Single Class SerialPortCommunication in which I create object of SerialPort class and I Call the Serialport.open() method but I am getting error when I am calling the method "Exception has been thrown by the target of an invocation". Please give me basic idea how I can I make best of architecture.

Above question might be tricky or not easily understandable .please free to ask me questions.

推荐答案

我们无法直接回答:你有代码我们没有!

但是......从实际开始看异常对象:具体来说,查看内部异常 - 这通常包含实际异常的详细信息,这可以帮助您缩小原因。


对不起 - 但是在那一刻,那个和调试器是你最好的朋友!
We can't answer that directly: you have the code and we don't!
But...start by looking at the actual exception object: specifically, look at the Inner Exception - this normally contains details of the actual exception, which should help you narrow down the cause.

Sorry - but at the moment, that and the debugger are your best friends!


你的静态类会看起来像这样



Your static class will look someting like this

public class SerialPortClass
 {
     private static SerialPortClass instance;

     public static SerialPortClass GetInstance()
     {
         return instance ?? new SerialPortClass();
     }

     SerialPort port;
     //Rest of your serial port class members and methods here....

     public void SendData(byte[] data)
     {
         port.Write(data,0,data.Count());
     }

     protected void Connect()
     {
         port = new SerialPort("COM1");
         //portsettings
         //port.BaudRate = 1024;
     }
 }





您在其他表单中的实现或调用将类似于





Your implementation or calls inside the other forms will look like

SerialPortClass.GetInstance().Write(data);


让多个表单访问任何类型的串行类甚至设备的明显问题是如何处理谁在处理数据。



我的意思是串行对象无论看起来如何保存数据都是第一种形式要求它得到它或者是预期回调的形式并说好的下一个数据我已经处理了那个。然后你有部分数据的问题某种程度上串行流必须有某种形式的数据块与数据标记结束,表格显然需要用完整的块解析数据而不是半块。



这让我得出了明显的结论,上述任何一个想法都不会真的有效。



我觉得你在尝试制作一个消息队列,其中所有目的看起来非常像标准的Windows队列,这将为您提供一个很好的线索,以非常优雅的方式执行此操作。



1.)设计自己的消息队列系统

2.)将自己的私人消息附加到标准的Windows处理程序中



我个人会去对于选项2,Windows消息处理程序表现良好,你知道它将如何表现,所以你所做的只是制作你自己的私有WM命令,并说例如将解析后的数据附加到LPARAM。微软很容易让他们这么做。



The obvious issue with having multiple forms accessing a any sort of serial class or even device is working out how to deal with who is handling the data.

I mean the serial object whatever it looks like holds data does the first form to ask for it get it or are the forms expected to callback and say ok next data I have handled that one. Then you have the issue of partial data somehow the serial stream must have data blocks of some form with end of data markers and the forms are obviously needing to parse that data with a complete block not a half block.

That leads me to the obvious conclusion none of the ideas above will really work well.

It would appear to me you are trying to make a message queue which for all purposes looks very much like a standard windows queue and that would give you a good clue to do this in a very elegant way.

1.) Design your own message queue system
2.) Attach your own private message into the standard windows handler

Personally I would go for option 2 the windows message handler is well behaved and you know how it will behave so all you do is make your own private WM command and say for example attach the parsed data to the LPARAM. Microsoft makes this easy they expect you to do it.

WM_MYPRIVATEMSG (WM_USER + 0x0001) 





如果您只想基于窗口句柄,可以使用标准窗口PostMessage和SendMessage命令将它们发布到特定表单或系统范围。你甚至可以使用RegisterWindowMessage注册消息,如果你真的想要并将串行对象作为它自己的线程或应用程序。



重点是它保持一个串行对象播放使用串口和表格只需处理一个新的WM_MYPRIVATEMSG,它将以通常的方式发布。



地狱你可以真正看中并发回消息给如果你需要控制它,可以使用相同的技术来处理串行对象。



所有这一切都使得整个结构具有可移植性和一致性,并且随着时间的推移会得到支持,因为有很多应用程序以这种方式工作,这是推荐的方法。



You use standard windows PostMessage and SendMessage commands to post them up to a specific form or system wide if you want simply based on window handle. You can even Register the message using RegisterWindowMessage if you really want and make the serial object as it's own thread or application.

The point is it stays with one serial object playing around with the serial port and the forms just handle a new WM_MYPRIVATEMSG which will be posted out in usual way.

Hell you could get really fancy and send message back to the serial object by using the same technique if you need to control it.

What all that does it makes the whole the structure portable and consistent and will be supportable over time because there are many many applications that work that way and it is the recommended way to do it.


这篇关于所有窗体中的串行端口数据访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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