none
serialport 组件如何进行自动触发? RRS feed

  • 问题

  • 嗨,亲爱的朋友们:

          快过年咯,来我这里研讨研讨serialport吧

        我现在的是把serialPOrt放在按钮下的事件里进行click 触发的,那么程序一运行,非得点击按钮才会触发,serilport.open()

        有点麻烦哦,我想更智能化一点,每次一运行程序,就自动进行触发serialPort.open();

        我该怎么写代码呢?求指点,一起探讨下哈。

    先解释一下哦,我已经把方法放在Form -Load 事件之下,测试过了,提示无法访问COM口,但是在按钮事件下就没问题哦,如图:

    private void button3_Click(object sender, EventArgs e)
            {
                if (serialPort1.IsOpen)
                {
    
                    MessageBox.Show("已经联机");
    
                }
                //serialPort1.Open();
            }

    2018年1月15日 1:19

答案

  • 不要在构造方法写代码,这里只负责构造对象结构,应该把代码放到FormLoad/OnLoad事件中

    protected override void OnLoad(EventArgs e)   

    {

                base.OnLoad(e);      

                button3.PerformClick();

      }

    或是

    Form1_Load事件:private void Form1_Load(object sender, EventArgs e)

    {

        button3.PerformClick();

     }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年1月16日 0:52

全部回复

  • Hi,

    欢迎在MSDN论坛发帖。

    根据这个错误信息提示我们知道,有两种情况下会导致这个事情发生。

    1, 访问这个端口被拒绝。 你有没有尝试使用管理员权限去运行这个程序。

    2, 当前的进程,或者其他进程,已经有个特殊的端口被这个serial 实例打开,或者被其他unmanaged code打开。

    我个人认为第二种的可能性比较大,因为不知道你是否做了其他操作来再次触发load event,为了保证instance的唯一性,你可以加一个static 变量和if判断语句,保证open 函数只会被执行一下。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月15日 6:04
    版主
  • 谢谢你,能否把代码给我写出来,我复制进去运行下效果。
    2018年1月15日 8:41
  • 我就是用管理员权限去运行程序的。
    2018年1月15日 8:44
  • 确实,open函数是只能被激活一次的话,即使在按钮事件里,点一次按钮,open函数就打开了,如果再点击第2次,就会报警说端口已打开。

    现在的问题你的判断应该是对的,可否给我写一段代码,让我测试测试,谢谢您了。

    2018年1月15日 8:47
  • Hi,

    我写了一份伪代码,因为我这边没com 口,没有办法测试。

    但是逻辑大致如下,你可以修改一下。

    	static int count = 0;
    	if (count == 0)
    	{
    		serial.open();
    		count=1;
    	}
    	else
    	{ 
    		count = 2;
    	}

    上面这段代码主要保证serial open函数只调用一次。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月15日 9:11
    版主
  • 亲爱的版主,Hart Wang:

           您好,谢谢您的指导。

         1) 我测试了下,把它放在Form2事件下,提示,static 对该项无效。

         2)  我把static 去掉,运行程序后,仍然提示对端口访问被拒接。

     如下图示:

    2018年1月16日 0:31
  • 不要在构造方法写代码,这里只负责构造对象结构,应该把代码放到FormLoad/OnLoad事件中

    protected override void OnLoad(EventArgs e)   

    {

                base.OnLoad(e);      

                button3.PerformClick();

      }

    或是

    Form1_Load事件:private void Form1_Load(object sender, EventArgs e)

    {

        button3.PerformClick();

     }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年1月16日 0:52
  • 您好

        放在

    private void Form1_Load(object sender, EventArgs e)

    事件下,端口并没有打开,但是放在button 事件下,端口就被打开了,同样的代码,但是点击button 端口打开,让Form 自己加载事件,却没有打开端口:

    如下:

      private void Form1_Load(object sender, EventArgs e)
            {
                button2.PerformClick();
                int count = 0;
                if (count == 0)
                {
                    serialPort1.Open();
                    count = 1;
                }
                else
                {
                    count = 2;
                }
    
                var bytes = new Byte[] { 0x05, 0x30, 0x30, 0x46, 0x46, 0x42, 0x52, 0x41, 0x4D, 0x30, 0x30, 0x30, 0x32, 0x30, 0x31 };
                serialPort1.Write(Encoding.UTF8.GetString(bytes));
    
            }

     private void button2_Click(object sender, EventArgs e)
            {
                serialPort1.Open();
                var bytes = new Byte[] { 0x05, 0x30, 0x30, 0x46, 0x46, 0x42, 0x52, 0x41, 0x4D, 0x30, 0x30, 0x30, 0x32, 0x30, 0x31 };
                serialPort1.Write(Encoding.UTF8.GetString(bytes));
            }


    2018年1月16日 2:06
  • Hi,

    我用static 变量的原因,是因为static 变量是有记忆的特性。

    它会保存上次离开后的值,现在你换成局部变量,如果load event 触发两次,每次count 都会是1, 所以这段代码就没有意义了。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月16日 2:15
    版主
  • 亲爱的版主,那我这段代码应该放在哪里呢?
    2018年1月17日 2:05
  • 我现在就是不知道,应该把以下代码放在什么地方,我试着放在按钮事件以外的地方,总是有很多报警内容。

    static   int count = 0;
                if (count == 0)
                {
                    serialPort1.Open();
                    count = 1;
                }
                else
                {
                    count = 2;
                }

    2018年1月17日 2:07
  • FormLoad/OnLoad中启动一个线程试下,把代码放到线程中。


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年1月17日 4:13
  • 好的,谢谢你。
    2018年1月17日 7:29
  • 您好

        我只知道线程由Thread 关键字创建,还没有真的用到过线程。

       你可否帮我把线程创建1个,然后代码放进来。你帮我写出来吧,我复制进去测试下。

    2018年1月17日 7:36