您好, 我正在学习串行端口编程.因此,我研究了MSDN中的代码示例-VB101SamplesBCL2 download.microsoft/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 download.microsoft/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类一起使用
发布评论