serialport有两种形式 [英] serialport in two forms

查看:122
本文介绍了serialport有两种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..,

现在正在使用2个表单。这两个表单包含serialport控件。当加载第一个表单时,我打开了serialport1。每当我从serialport获得一个字符'M'时我写在我使用代码应用程序Application.Run(new Form1())打开form2之后,此序列端口的字符'A';之后我想将数据发送到第一个表单的serialports datareceived事件。但是当我发送数据时,serialport1的数据接收事件没有调用....这背后的问题是什么?我无法理解..请帮助我...

解决方案

唔不,它不会。



想一想:串口通过一根电线连接两台机器。因此,一次只有一件事可以与另一端的机器通信,或者远程机器会因为它可能同时从两个软件接收数据而感到困惑 - 而且它无法分辨它们来自何处因为字符没有标签,上面写着我来自软件A或我来自软件B。这有点像站在厨房,而你的妻子和孩子都试图告诉你同时做什么:你不能同时听他们两个!



再加上你打开一个新表格的方式,事情开始变得非常困惑。



Application.Run不适用于打开新表单:它打开一个新的应用程序,与现有的表单分开,代码在新的应用程序完成之前不会返回。



那么你想要什么do为您的应用程序提供一个SerialPort,并将数据路由到相应的表单。

因此Form1打开并连接串行端口。它收到M,写下A并打开一个新的表格2:

  if  (bytes == '  M'
{
serialPort1.Write(< span class =code-string> A);
Form2 f2 = new Form2();
f2.Show();
do
{
bytes = serialPort1.ReadByte();
f2.ProcessByte(字节);
} while (bytes!= X );
f2.Close();



可能不是你想要的,但它有机会工作!


< blockquote>串口通信应该异步完成或在单独的线程中完成;我通常会推荐一个单独的线程,它为您提供了更加统一的方法来使用不同的API:仍然是相同的线程同步。说到这一点:您的问题是关心通知UI从您从端口获得的数据。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA


hi..,
now am using 2 forms.these two forms contains serialport control.when loading the first form i opened the serialport1.whenever i got a character'M' from serialport i write character 'A' to this serialport after that i opened the form2 using the code application Application.Run(new Form1()); after that i want to send data to the first form's serialports datareceived event.but when i send data serialport1's data received event doesnt invoke.... what is the problem behind this?? i cant understand..pls help me...

解决方案

Well no, it won't.

Think about it: a serial port connects two machines by a single piece of wire. So only one thing at a time can talk to the machine at the other end, or the remote machine will just get confused because it can be receiving data from two pieces of software at the same time - and it can't tell where they came from because characters don't have a "tag" which says "I came from software A" or "I came from software B". It's a bit like standing in the kitchen while your wife and child both try to tell you what to do at the same time: you can't listen to both of them at the same time!

Add to that the way you are "opening a new form" and things start to get very confused.

Application.Run is not intended for opening new forms: it opens a new application, separate form the existing one, and the code will not return until the new app is finished.

So what you want to do is have one single SerialPort for your application, and have it route the data to the appropriate form.
So Form1 opens and connects the serial port. It receives the "M", writes the "A" and opens a new Form 2:

if (bytes == 'M')
   {
   serialPort1.Write("A");
   Form2 f2 = new Form2();
   f2.Show();
   do
      {
      bytes = serialPort1.ReadByte();
      f2.ProcessByte(bytes);
      } while (bytes != "X");
   f2.Close();


May not be exactly what you want, but it stands a chance of working!


The serial port communications should be done asynchronously or in a separate thread; I usually recommend a separate thread, which gives you more uniform approach to using different API: still the same thread synchronization. Speaking of which: your problem will be to care about notifying the UI on your data you get from the port. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于serialport有两种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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