简体   繁体   English

C#中的连续RS485通信和UI

[英]Continious RS485 communication and UI in C#

I have RS485 slave devices connected to a computer. 我有RS485从设备连接到计算机。 Computer ask a device some question and waits on response. 计算机询问设备一些问题并等待响应。

The communication should be continious with user interface to show data from devices and to control them in real time. 通信应该是连续的用户界面,以显示来自设备的数据并实时控制它们。

I have developed some applications with RS485 communication with use of bacgroundWorker. 我开发了一些使用bacgroundWorker进行RS485通信的应用程序。

I'm not sure but I think this is not the proper way to use it this time, becouse user interface elements can not be changed or updated from backgroundWorker. 我不确定,但我认为这次使用它不是正确的方法,因为用户界面元素无法从backgroundWorker更改或更新。

I could probably use backgroundWorker and global data variables, and then update UI on timer event. 我可以使用backgroundWorker和全局数据变量,然后在timer事件上更新UI。 But I have concerns about this solution. 但我担心这个解决方案。 thanks! 谢谢!

You don't need to use a background worker, as Serial Ports support asynchronous operations - you subscribe to DataReceived and after opening the port, your event will be called periodically with received data. 您不需要使用后台工作程序,因为串行端口支持异步操作 - 您订阅了DataReceived ,在打开端口后,将定期使用接收到的数据调用您的事件。

You then need to buffer the data until you build up a complete message packet (you may get data in dribs and drabs, there is no guarantee that you will get fully formed packets), and then update your UI (usually using a BeginInvoke to pass control back to your UI thread) 然后,您需要缓冲数据,直到您构建完整的消息包(您可能会获得数据,并且无法保证您将获得完整形成的数据包),然后更新您的UI(通常使用BeginInvoke传递控制回你的UI线程)

I designed the system of remote monitoring and control which works with controllers by RS-485. 我设计了远程监控系统,通过RS-485与控制器配合使用。 It implemented as Windows Service which writes data in SQL Server. 它实现为Windows服务,它在SQL Server中写入数据。 UI Application (It is Web Application in my last version) takes the data from SQL. UI应用程序(它是我上一版本中的Web应用程序)从SQL获取数据。 In previous versions I used Named Pipes. 在以前的版本中,我使用了命名管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM