English 中文(简体)
如何在整个视窗应用中使用序列港?
原标题:how to use serial port throughout my Windows Forms application?

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.

问题是,每一条<代码>都避免了OnClick...(标注器,系统)。 活动Args e)与他人分离,系列港口“港口”在整个方案中都有工作。 i 主要用于序列用途:

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

i 每一次都希望开放和关闭序列港,因为港口可能过于接近(按时间计算),可能会造成“航空港正在使用”问题,或造成发送序列线的滞后。

.。

法典:

    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>");
        }
    }
}
最佳回答
static SerialPort port;
static public void Main()          
{             
    port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
    port.Open();              
    Application.Run(new EAS_ControlForm());         
} 
问题回答
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);
    }

如果是你想要的话。

简单地界定了<代码>port为一类成员。 以施工者的形式或在<代码>Load上启动。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签