Beispiel #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // Retrieve the user settings
            mus = new MyUserSettings();

            // Create the serial port
            inp = new SerialPort(mus.comPort, 38400, Parity.None, 8, StopBits.One);
            //    inp = new SerialPort(mus.comPort, 250000, Parity.None, 8, StopBits.One);


            // Hook-up the port data handler
            serEh             = new SerialDataReceivedEventHandler(inp_DataReceived);
            inp.DataReceived += serEh;

            // Attempt to open the port
            try
            {
                inp.Open();
                inp.RtsEnable = false;
            }
            catch (Exception)
            {
                // Oops
                MessageBox.Show("Error opening port", "Port Open", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Set the button text according to the port state
            if (inp.IsOpen)
            {
                buttonConnect.Text = "Disconnect";
            }
            else
            {
                buttonConnect.Text = "Connect";
            }

            // Initialise the port droplist
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBoxPort.Items.Add(port);
            }
            comboBoxPort.SelectedItem = mus.comPort;
        }