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

发布于 2024-12-10 04:30:45 字数 2375 浏览 2 评论 0原文

我正在尝试从程序创建一个 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>");
        }
    }
}

I'm attempting to make a C# from program that is a simple box with four buttons: left, right, up, and down. then when a button is initially pressed(but not let go) it will send a a serial line. then when the button is unclicked(user lets go of left mouse button) i want to send another serial command.

the issue is that each void OnClick... (object sender, System.EventArgs e) is separate from the others and the serial port "port" doesn't work throughout the whole program. i have in main for serial use:

SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Open();

i didn't want to open and close the serial port each time i want to send something because it may be too close together(in terms of time) and may cause the "serial port in use" issue and or lag in the sending of the serial line.

slimmed down as much as i could.

code:

    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>");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抹茶夏天i‖ 2024-12-17 04:30:45
static SerialPort port;
static public void Main()          
{             
    port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
    port.Open();              
    Application.Run(new EAS_ControlForm());         
} 
static SerialPort port;
static public void Main()          
{             
    port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
    port.Open();              
    Application.Run(new EAS_ControlForm());         
} 
只是我以为 2024-12-17 04:30:45
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);
    }

看看这是否是您想要的。

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);
    }

See if this is what you wanted.

天煞孤星 2024-12-17 04:30:45

只需将端口定义为类成员即可。在表单的构造函数中或在 Load 事件中对其进行初始化。

Simply define the port as a class member. Initialize it in the form's constructor, or on the Load event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文