예제 #1
0
 /// <summary>
 /// When serial received data, will call this method
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     if (sp.BytesToRead <= 0)
     {
         return;
     }
     //Thread Safety explain in MSDN:
     // Any public static (Shared in Visual Basic) members of this type are thread safe.
     // Any instance members are not guaranteed to be thread safe.
     // So, we need to synchronize I/O
     lock (thisLock)
     {
         int    len  = sp.BytesToRead;
         Byte[] data = new Byte[len];
         try
         {
             sp.Read(data, 0, len);
         }
         catch (System.Exception)
         {
             //catch read exception
         }
         SerialPortEventArgs args = new SerialPortEventArgs();
         args.receivedBytes = data;
         if (comReceiveDataEvent != null)
         {
             comReceiveDataEvent.Invoke(this, args);
         }
     }
 }
예제 #2
0
        /// <summary>
        /// Open Serial port
        /// </summary>
        /// <param name="portName"></param>
        /// <param name="baudRate"></param>
        /// <param name="dataBits"></param>
        /// <param name="stopBits"></param>
        /// <param name="parity"></param>
        /// <param name="handshake"></param>
        public void Open(string portName, String baudRate,
                         string dataBits, string stopBits, string parity,
                         string handshake)
        {
            if (sp.IsOpen)
            {
                Close();
            }
            sp.PortName = portName;
            sp.BaudRate = Convert.ToInt32(baudRate);
            sp.DataBits = Convert.ToInt16(dataBits);

            /**
             *  If the Handshake property is set to None the DTR and RTS pins
             *  are then freed up for the common use of Power, the PC on which
             *  this is being typed gives +10.99 volts on the DTR pin & +10.99
             *  volts again on the RTS pin if set to true. If set to false
             *  it gives -9.95 volts on the DTR, -9.94 volts on the RTS.
             *  These values are between +3 to +25 and -3 to -25 volts this
             *  give a dead zone to allow for noise immunity.
             *  http://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners
             */
            if (handshake == "None")
            {
                //Never delete this property
                sp.RtsEnable = true;
                sp.DtrEnable = true;
            }

            SerialPortEventArgs args = new SerialPortEventArgs();

            try
            {
                sp.StopBits     = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
                sp.Parity       = (Parity)Enum.Parse(typeof(Parity), parity);
                sp.Handshake    = (Handshake)Enum.Parse(typeof(Handshake), handshake);
                sp.WriteTimeout = 1000; /*Write time out*/
                sp.Open();
                sp.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
                args.isOpend     = true;
            }
            catch (System.Exception)
            {
                args.isOpend = false;
            }
            if (comOpenEvent != null)
            {
                comOpenEvent.Invoke(this, args);
            }
        }
예제 #3
0
        public void CloseComEvent(Object sender, SerialPortEventArgs e)
        {
            if (this.InvokeRequired)
            {
                Invoke(new Action <Object, SerialPortEventArgs>(CloseComEvent), sender, e);
                return;
            }

            if (!e.isOpend) //close successfully
            {
                //baudRateCbx.Enabled = false;
                //dataBitsCbx.Enabled = false;
                //stopBitsCbx.Enabled = false;
                //parityCbx.Enabled = false;
                //handshakingcbx.Enabled = false;
            }
        }
예제 #4
0
        /// <summary>
        /// Close serial port thread
        /// </summary>
        private void CloseSpThread()
        {
            SerialPortEventArgs args = new SerialPortEventArgs();

            args.isOpend = false;
            try
            {
                sp.Close(); //close the serial port
                sp.DataReceived -= new SerialDataReceivedEventHandler(DataReceived);
            }
            catch (Exception)
            {
                args.isOpend = true;
            }
            if (comCloseEvent != null)
            {
                comCloseEvent.Invoke(this, args);
            }
        }
예제 #5
0
        public void OpenComEvent(Object sender, SerialPortEventArgs e)
        {
            if (this.InvokeRequired)
            {
                Invoke(new Action <Object, SerialPortEventArgs>(OpenComEvent), sender, e);
                return;
            }

            if (e.isOpend)  //Open successfully
            {
                try
                {
                    timer1.Enabled = false;//关闭定时器
                    //baudRateCbx.Enabled = true;//“波特率”可选
                    //parityCbx.Enabled = true;//“校验位”可选
                    //dataBitsCbx.Enabled = true;//“数据位”可选
                    //stopBitsCbx.Enabled = true;//“停止位”可选
                }
                catch
                {
                    MessageBox.Show("串口断开失败", "错误");
                }
            }
        }
예제 #6
0
        public void ComReceiveDataEvent(Object sender, SerialPortEventArgs e)
        {
            if (this.InvokeRequired)
            {
                try
                {
                    Invoke(new Action <Object, SerialPortEventArgs>(ComReceiveDataEvent), sender, e);
                }
                catch (System.Exception)
                {
                    //disable form destroy exception
                }
                return;
            }

            //  for(int i=0;i<e.receivedBytes.Length;i++){
            //     RecBytes[RecNum++] = e.receivedBytes[i];
            //  }

            if (radioButton3.Checked) //display as string
            {
                this.memoEdit1.MaskBox.AppendText(Encoding.Default.GetString(e.receivedBytes));
            }
            else //display as hex
            {
                if (memoEdit1.Text.Length > 0)
                {
                    memoEdit1.MaskBox.AppendText(" ");
                }
                memoEdit1.MaskBox.AppendText(IController.Bytes2Hex(e.receivedBytes));
            }

            if ((e.receivedBytes[0] == 0x0D) && (e.receivedBytes[e.receivedBytes.Length - 1] == 0x0D))
            {
                memoEdit1.Text += "\r\n";
                switch (e.receivedBytes[1])
                {
                case 0xfe:
                    byte[] verbuf = new byte[3];
                    verbuf[0] = e.receivedBytes[2];
                    verbuf[1] = e.receivedBytes[3];
                    verbuf[2] = e.receivedBytes[5];
                    string result = string.Join(".", verbuf);
                    barButtonItem11.Caption = "固件版本:" + result;
                    barButtonItem9.Enabled  = true;
                    break;

                case 0xf1:
                    f3.Output_Status_Show(e.receivedBytes);
                    break;

                case 0xf9:
                    f3.Note_Message(e.receivedBytes);
                    break;

                case 0xf8:
                    f3.SingleReverse_Parameter_Display(e.receivedBytes[2], e.receivedBytes[3], e.receivedBytes[4], e.receivedBytes[5], e.receivedBytes[6], e.receivedBytes[6]);
                    break;

                case 0xe0:
                    f3.Spreadsheet_Content_Show(e.receivedBytes);
                    break;

                case 0xf0:
                    f3.calibration_process(e.receivedBytes);
                    break;
                }

                //Array.Clear(RecBytes, 0, 32);
                Array.Clear(e.receivedBytes, 0, e.receivedBytes.Length);
                //RecNum = 0;
            }
        }