Esempio n. 1
0
 void comPort_DataReceived(DataReceivedEventArgs e)
 {
     this.txtRecv.BeginInvoke(
         new MethodInvoker(() => {
         if (checkBoxHEX.Checked)
         {
             this.txtRecv.AppendText(SerialPortUtil.ByteToHex(e.DataRecv));   //输出到主窗口文本控件
         }
         else
         {
             this.txtRecv.AppendText(System.Text.Encoding.Default.GetString(e.DataRecv));   //输出到主窗口文本控件
         }
     }
                           )
         );
 }
Esempio n. 2
0
 private void COMSend()
 {
     try
     {
         if (checkBoxHEX.Checked)
         {
             comPort.WriteData(SerialPortUtil.HexToByte(txtSend.Text));
         }
         else
         {
             comPort.WriteData(txtSend.Text);
         }
     }
     catch (System.Exception ex)
     {
         MessageBox.Show("发送错误:" + ex.Message);
     }
 }
Esempio n. 3
0
 void comPort_DataReceived(DataReceivedEventArgs e)
 {
     this.txtRecv.BeginInvoke(
         new MethodInvoker(() => {
         if (checkBoxHEX.Checked)
         {
             this.txtRecv.AppendText(SerialPortUtil.ByteToHex(e.DataRecv));   //输出到主窗口文本控件
         }
         else
         {
             StreamWriter fs3 = new StreamWriter(Application.StartupPath + "\\log.txt", true);
             this.txtRecv.AppendText(System.Text.Encoding.Default.GetString(e.DataRecv));   //输出到主窗口文本控件
             fs3.Write(System.Text.Encoding.Default.GetString(e.DataRecv));
             fs3.Close();
         }
     }
                           )
         );
 }
Esempio n. 4
0
        private void MainFrom_Load(object sender, EventArgs e)
        {
            //comboBox设置数据

            SerialPortUtil.SetPortNameValues(comboBoxDKH);
            if (comboBoxDKH.Items.Count == 0)
            {
                MessageBox.Show("未扫描到该计算机的COM口,请添加硬件后重新打开程序!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            SerialPortUtil.SetBauRateValues(comboBoxBTL);
            SerialPortUtil.SetDataBitsValues(comboBoxSJW);
            SerialPortUtil.SetParityValues(comboBoxXYW);
            SerialPortUtil.SetStopBitValues(comboBoxTZW);



            comboBoxDKH.SelectedIndex         = 0;
            comboBoxBTL.SelectedIndex         = 8;
            comboBoxSJW.SelectedIndex         = 3;
            comboBoxXYW.SelectedIndex         = 0;
            comboBoxTZW.SelectedIndex         = 1;
            comboBoxDKH.SelectedIndexChanged += (o, ex) => { COMClose(); comPort.PortName = comboBoxDKH.SelectedItem.ToString(); COMOpen(); };
            comboBoxBTL.SelectedIndexChanged += (o, ex) => { COMClose(); comPort.BaudRate = (SerialPortBaudRates)Convert.ToInt32(comboBoxBTL.SelectedItem); COMOpen(); };
            comboBoxSJW.SelectedIndexChanged += (o, ex) => { COMClose(); comPort.DataBits = (SerialPortDatabits)Convert.ToInt32(comboBoxSJW.SelectedItem.ToString()); COMOpen(); };
            comboBoxXYW.SelectedIndexChanged += (o, ex) => { COMClose(); comPort.Parity = (Parity)(comboBoxXYW.SelectedIndex); COMOpen(); };
            comboBoxTZW.SelectedIndexChanged += (o, ex) => { COMClose(); comPort.StopBits = (StopBits)(comboBoxTZW.SelectedIndex); COMOpen(); };
            comPort.DataReceived             += new DataReceivedEventHandler(comPort_DataReceived);
            comPort.Error += new SerialErrorReceivedEventHandler(comPort_Error);;
            //COMOpen();
            comPort.PortName = comboBoxDKH.SelectedItem.ToString();
            comPort.BaudRate = (SerialPortBaudRates)Convert.ToInt32(comboBoxBTL.SelectedItem);
            comPort.DataBits = (SerialPortDatabits)Convert.ToInt32(comboBoxSJW.SelectedItem.ToString());
            comPort.Parity   = (Parity)(comboBoxXYW.SelectedIndex);
            comPort.StopBits = (StopBits)(comboBoxTZW.SelectedIndex);

            chkBoxSendOntime.Checked = false;
            checkBoxHEX.Checked      = true;
            txtTime.Text             = "200";
            timer.Elapsed           += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }