コード例 #1
0
    // Connect to the serial port
    private void btn_Connect_Click(object sender, EventArgs e)
    {
        if (port == null)
        {
            // Port is closed

            int baudrate = -1;
            try {
                baudrate = int.Parse(cmb_baudRate.Text);
            } catch { }

            if (baudrate < 0)
            {
                btn_Connect.BackColor = colorClosed;
                Program.errors.Add("Faulty baud rate.");
            }
            else
            {
                btn_Connect.BackColor = colorOpen;
                port = new SerialConnection(cmb_port.Text, baudrate, this);
                port.Open();
                timerbuffer.Start();
            }
        }
        else
        {
            // Port is open
            btn_Connect.BackColor = colorClosed;
            if (port.IsOpen())
            {
                port.Close();
            }
            timerbuffer.Stop();
        }
    }