无法访问其他形式的开放式串行端口 [英] Not access open serial port in other form

查看:74
本文介绍了无法访问其他形式的开放式串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了用于接收串行端口数据的窗口应用程序.

这是我打开串口的代码:

I developed window application for receive serial port data.

Here is my code for open serial port:

public bool OpenPort()
       {
           try
           {
               if (comPort.IsOpen == true)
               {
                   comPort.Close();
               }
               comPort = new SerialPort();
               comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

               //set the properties of our SerialPort Object
               comPort.BaudRate = int.Parse(_baudRate);
               comPort.DataBits = int.Parse(_dataBits);
               comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), _stopBits);
               comPort.Parity = (Parity)Enum.Parse(typeof(Parity), _parity);
               comPort.PortName = _portName;
               //objhartnetwork.PortNameHart = comPort.PortName;
               comPort.Open();

               DisplayData(MessageType.Normal, "Port opened at " + DateTime.Now);
               return true;
           }
           catch (Exception ex)
           {
               DisplayData(MessageType.Error, ex.Message);
               return false;
           }
       }


我的问题是,当我检查comport是否打开或未以其他形式打开时,我得到的comport是close(值false).

这是我的支票兑换代码:


My problem is when I check comport is open or not in another form then I get comport is close(value false).

This is my code for check comport:

if (comm.comPort.IsOpen == true)  // comm is object of form at where i open port
{
      process....
}



在这里,我得到了错误的值,因此不要进入if循环而不进行进一步处理.



Here I get false value so not go in to if loop and not to further process.

推荐答案

使用端口,您应该按顺序在单个位置进行所有操作.您不需要检查任何东西;您应该使用推入方法,而不是拉入.

首先,所有端口通信都应该是一个单独的线程.如果发生某些更改(端口状态,缓冲区,您需要从外部知道的任何内容),则代码应具有在线程中触发的通知事件.由于事件是从另一个线程触发的,因此事件处理程序的实现应将其考虑在内.特别是,如果事件处理涉及UI(显示端口状态等),则无法在UI上进行任何直接调用,您需要使用Control.InvokeControl.BeginInvoke.

请查看我过去的解决方案以获取详细信息:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

您认为这有点太复杂了吗?好吧,您似乎需要进一步掌握简单的编程拳法.只需看一下您的代码即可:

With the port, you should do everything in a single place in sequential manner. You don''t need to check up anything; you should use push approach, not pull.

First of all, all port communication should be done is a separate thread. The code should have notification event you trigger in the thread if something changes (port status, buffer, whatever you need to know from outside). As the events are triggered from the other thread, the implementation of event handlers should take it into account. In particular, if the event handling involves UI (show port status, or something), any direct call on the UI is not possible, you need to use Control.Invoke, Control.BeginInvoke.

Please see my past solutions for detail:
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[^].

Do you think it''s a bit too complex? Well, it looks like you need to get more grip in simple programming fist. Just look at your code:

if (comm.comPort.IsOpen == true) { /*...*/ }



你不觉得这很傻吗?必须是



Don''t you see this is silly? Must be

if (comm.comPort.IsOpen) { /*...*/ }



这只是关于简单的逻辑和对代码含义的理解.所以...

—SA



This is just about simple logic and understanding of the meaning of the code. So…

—SA


我在我的项目中解决了该问题,在该表单中,我设置了端口详细信息,然后关闭了该页面,所以我没有真正的端口名,所以现在解决仅隐藏该页面并定义公共静态串行端口,以便我在所有形式下均获得相同的值.
I solve it in my project at which form i set port detail i closed that page so i am not get real port name so now solution is to only hide that page and define public static serial port so i get same value at all form.


这篇关于无法访问其他形式的开放式串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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