private void btnSnd_Click(object sender, EventArgs e)
        {
            String[] sd = txtT.Text.Split(' ');
            byte[]   sb = new byte[sd.Length];
            int      i  = 0;

            foreach (String a in sd)
            {
                //state_machine(a);

                //  this.txtShowData.Invoke(new Action(() =>
                //   {
                try
                {
                    if (a.Trim().Length > 0)
                    {
                        sb[i++] = Convert.ToByte(a, 16);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            ComDevice.Write(sb, 0, i);
            tcnt       += i;
            lblSts.Text = "Tx:" + tcnt.ToString() + "; Rx:" + rcnt.ToString();
        }
示例#2
0
 private static bool SendData(byte[] data, int length)
 {
     if (ComDevice.IsOpen)
     {
         try
         {
             ComDevice.Write(data, 0, length);//发送数据
             return(true);
         }
         catch (Exception ex)
         {
             if (sendDataError != null)
             {
                 sendDataError(ex.Message);
             }
         }
     }
     return(false);
 }
示例#3
0
 public bool SendData(byte[] data)
 {
     if (ComDevice.IsOpen)
     {
         try
         {
             ComDevice.Write(data, 0, data.Length);//发送数据
             return(true);
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
     else
     {
         //MessageBox.Show("串口未打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
         AddContent("串口未打开\r\n");
     }
     return(false);
 }