如何使用串口在我的应用程序中获取数据? [英] How do I use the serial port to get data in my application?

查看:96
本文介绍了如何使用串口在我的应用程序中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visual studio c ++ 2013,我想创建一个应用程序,允许我使用串口从我的PC获取数据并在文本框中显示。



我尝试了什么:



我尝试使用serialport事件自动读取数据时序列中的某些东西port。

I'm using visual studio c++ 2013, I want to create an application that allow me to use the serial port to get data from my PC and show it on textbox.

What I have tried:

I try to use an event of serialport to read data automatically when some thing in the serial port.

推荐答案

请看这里: SerialPort.DataReceived Event(System.IO.Ports) [ ^ ] - 它包含一个基本示例。

您将遇到的唯一复杂因素是您无法访问UI控件,除了创建它们的线程 - UI线程 - 并且从未在该线程上触发DataReceived事件。因此,如果您尝试直接访问TextBox,则会出现交叉线程错误。为了解决这个问题,你需要调用文本框并将数据恢复到正确的线程: Control.Invoke方法(委托)(System.Windows.Forms) [ ^ ]
See here: SerialPort.DataReceived Event (System.IO.Ports)[^] - it includes a basic example.
The only complication you will meet to that code is that you can't access UI controls except from the thread that created them - the UI thread - and the DataReceived event is never fired on that thread. SO if you try to access the TextBox directly , you will get a Cross Threading error. To get round this, you need to Invoke the Textbox and to get the data back to the correct thread: Control.Invoke Method (Delegate) (System.Windows.Forms)[^]


这篇关于如何使用串口在我的应用程序中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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