如何在我的Windows窗体应用程序中使用串口?

时间:2011-10-18 01:42:35

标签: c# forms button serial-port

我正在尝试从程序中创建一个C#,它是一个带有四个按钮的简单框:左,右,上和下。然后,当最初按下按钮(但不放手)时,它将发送一个串行线。然后当按钮未被点击时(用户放开鼠标左键)我想发送另一个串行命令。

问题是每个void OnClick... (object sender, System.EventArgs e)与其他SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); port.Open(); 是分开的,并且串口“端口”在整个程序中不起作用。我主要用于连续使用:

    public class EAS_ControlForm : System.Windows.Forms.Form
    {
        private Button Up4;
        private Button Down3;
        private Button Left2;
        private Button Right1;
        public EAS_ControlForm()
        {
            Text = "Etch-a-Sketch Control";
            Down3 = new Button ();
            Up4 = new Button ();
            Left2 = new Button ();
            Right1 = new Button ();

            Down3.Text = "Down";
            Down3.Name = "Down3";
            Down3.Size = new System.Drawing.Size (72, 30);
            Down3.Location = new System.Drawing.Point ((ClientRectangle.Width - Down3.Size.Width) / 2, ClientRectangle.Height - 10);
            Controls.AddRange(new System.Windows.Forms.Control[] {this.Down3});
            Down3.Click += new System.EventHandler(OnClickDown3);

            Up4.Text = "Up";
            ///...button up4 stuff here like down 3 above.

            Left2.Text = "Left";
            ///...button left2 stuff here like down 3 above.

            Right1.Text = "Right";
            ///...button right1 stuff here like down 3 above.
        }

        static public void Main() 
        {
            SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
            port.Open();

            Application.Run(new EAS_ControlForm());
        }

        void OnClickDown3 (object sender, System.EventArgs e)
        {
            port.Write("<3,100>");
        }

        void OnClickUp4 (object sender, System.EventArgs e)
        {
            port.Write("<4,100>"); //error here because of port initialization not in same code
        }

        void OnClickLeft2 (object sender, System.EventArgs e)
        {
            port.Write("<2,100>"); 
        }

        void OnClickRight1 (object sender, System.EventArgs e)
        {
            port.Write("<1,100>");
        }
    }
}

我不想每次想要发送东西时打开和关闭串口,因为它可能太靠近(在时间方面)并且可能导致“串口使用”问题和/或滞后发送串行线。

尽我所能减肥。

代码:

{{1}}

3 个答案:

答案 0 :(得分:1)

Try introducing a member function to share the serial port reference or send it through the constructor.

    SerialPort port ;
    public void SetSerialPort(SerialPort p_serialPort )
    {
        port = p_serialPort;
    }

    static public void Main() 
    {
        SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
        port.Open();
        EAS_ControlForm myform = new EAS_ControlForm();
        myform.SetSerialPort(port);

        Application.Run(myform);
    }

看看这是否是你想要的。

答案 1 :(得分:1)

static SerialPort port;
static public void Main()          
{             
    port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
    port.Open();              
    Application.Run(new EAS_ControlForm());         
} 

答案 2 :(得分:0)

只需将port定义为类成员即可。在表单的构造函数或Load事件中初始化它。