将串行端口与新的Port类一起使用 [英] Using serial ports with the new Port class

查看:69
本文介绍了将串行端口与新的Port类一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在学习串行端口编程.因此,我研究了MSDN中的代码示例-VB101SamplesBCL2

http://download.microsoft.com/download/c/2/3/c2318968-80aa-43de-a755-9c0763a2dca8/VB101SamplesBCL2.msi

运行"UsingTheSerialPort"的解决方案时,显示错误:

它突出显示了一个句子 textBox2.Text = SerialPort1.ReadLine()-第40行,并说:

未处理InvalidOperationException-跨线程操作无效:控件'textBox2'是从不是在其上创建线程的线程访问的."

有人可以帮我调试吗?

谢谢
Hi,

I'm learning the serial port programming. So, I study the code samples from MSDN - VB101SamplesBCL2

http://download.microsoft.com/download/c/2/3/c2318968-80aa-43de-a755-9c0763a2dca8/VB101SamplesBCL2.msi

While ran the solution of "UsingTheSerialPort", It shows error:

It highlights one sentence textBox2.Text = SerialPort1.ReadLine() -- Line 40 ,and says :

"InvalidOperationException was unhandled - Cross-thread operation not valid: Control 'textBox2' accessed from a thread other than the thread it was created on."

Anyone can give me help to debug?

Thanks

推荐答案

我认为您需要发布代码.如果您是我,我将设置一个全局变量字符串并将其存储在SerialPort1.readline中.然后使用标签显示该变量.通常使用文本框将文本输入到字符串中.它有很多例程 涉及到它.从串行端口返回的字符串可能具有无法与文本框一起使用的字符,具体取决于其设置方式.

同样,您似乎正在一个线程中获取串行端口数据,并试图将文本框显示给另一个线程.没有您的代码,我无法确定这是否是问题所在.

I think you need to post your code. If I were you I would set up a global variable string and store the SerialPort1.readline in. Then use a label to display that varaiable. A textbox is normally used to enter text into a string. It has numerous routines that are involved with it. The string being returned from the serial port may have characters that can not be used with the textbox depending on the way it is set up.

Also it appears that you are getting the serial port data in one thread and trying to display the textbox to another thread. Without you code I cannot determine if this is the problem or not.

 

柯蒂斯


这篇关于将串行端口与新的Port类一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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