如何在我的 Windows 窗体应用程序中使用串行端口?
我正在尝试从程序创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看这是否是您想要的。
See if this is what you wanted.
只需将
端口
定义为类成员即可。在表单的构造函数中或在Load
事件中对其进行初始化。Simply define the
port
as a class member. Initialize it in the form's constructor, or on theLoad
event.