Ejemplo n.º 1
0
 private async void OpenPort(object sender, EventArgs e)
 {
     await Task.Run(() => {
         if (PD.Port.IsOpen)
         {
             PD.Close();
         }
         else
         {
             Exception Err = PD.Open();
             if (Err != null)
             {
                 BeginInvoke((MethodInvoker)(() =>
                 {
                     MessageBox.Show(Err.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }));
             }
         }
         BeginInvoke((MethodInvoker)(() =>
         {
             if (PD.Port.IsOpen)
             {
                 BtnClose.Visible = true; BtnOpen.Visible = false;
             }
             else
             {
                 BtnClose.Visible = false; BtnOpen.Visible = true;
             }
         }));
     });
 }
Ejemplo n.º 2
0
        public Form1()
        {
            InitializeComponent();
            COMPort.Items.AddRange(SerialPort.GetPortNames());
            COMPort.Text  = Properties.Settings.Default.ComPort;
            textBox3.Text = Properties.Settings.Default.Speed.ToString();
            Close.Enabled = false;
            Open.Enabled  = true;

            textBox1.KeyDown += (s, e) => { if (e.KeyCode == Keys.Enter)
                                            {
                                                Transmit_Click(null, null);
                                            }
            };
            Refresh.Click        += (s, e) => { COMPort.Items.Clear(); COMPort.Items.AddRange(SerialPort.GetPortNames()); };
            Close.Click          += (s, e) => { PortA.Close(); Open.Enabled = true; Close.Enabled = false; };
            COMPort.TextChanged  += (s, e) => { Properties.Settings.Default.ComPort = COMPort.Text; Properties.Settings.Default.Save(); };
            textBox3.TextChanged += (s, e) =>
            {
                try
                {
                    int a = Convert.ToInt32(textBox3.Text);
                    Properties.Settings.Default.Speed = a;
                    Properties.Settings.Default.Save();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            };
            Open.Click += (s, e) =>
            {
                try
                {
                    PortA.Name  = COMPort.Text;
                    PortA.Speed = Convert.ToInt32(textBox3.Text);
                    PortA.Open();
                    Open.Enabled = false; Close.Enabled = true;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            };

            PortA.Receive_Event += () => BeginInvoke((MethodInvoker)(() =>
            {
                Data.Add(PortA.Receive_Array);
                textBox2.Lines = Data.ToArray();
                textBox2.SelectionStart = textBox2.TextLength;
                textBox2.ScrollToCaret();
                while (Data.Count > 100)
                {
                    Data.RemoveAt(0);
                }
                label1.Text = PortA.Result[0].ToString();
            }));
            PortA.Transmit_Event += () => BeginInvoke((MethodInvoker)(() =>
            {
                Data.Add(PortA.Transmit_Array);
                textBox2.Lines = Data.ToArray();
                textBox2.SelectionStart = textBox2.TextLength;
                textBox2.ScrollToCaret();
                while (Data.Count > 100)
                {
                    Data.RemoveAt(0);
                }
            }));
        }