Esempio n. 1
0
 private void btnSaveConfig_Click(object sender, EventArgs e)
 {
     ConfigManager.SaveSerialPortConfigurnation(cmbPortName.Text,
                                                cmbBaudRate.Text,
                                                cmbParity.Text,
                                                cmbDataBits.Text,
                                                cmbStopBits.Text);
     StaticSerialPort.resetStaticSerialPort();
     this.Close();
 }
 public static SerialPort getStaticSerialPort()
 {
     if (StaticSerialPort.comport == null)
     {
         StaticSerialPort.comport = new SerialPort();
         comport.DataReceived    += StaticSerialPort.port_DataReceived;
         StaticSerialPort.resetStaticSerialPort();//使用统一配置参数
     }
     return(StaticSerialPort.comport);
 }
        /// <summary>
        /// 串口关闭时可能引起线程死锁,因此这里要求首先安全关闭串口
        /// </summary>
        /// <param name="portName"></param>
        /// <param name="baudRate"></param>
        /// <param name="parity"></param>
        /// <param name="dataBits"></param>
        /// <param name="stopBits"></param>
        public static void resetStaticSerialPort(string portName, string baudRate, string parity, string dataBits, string stopBits)
        {
            SerialPort sp         = StaticSerialPort.getStaticSerialPort();
            bool       biniOpened = sp.IsOpen;

            if (biniOpened)
            {
                //sp.Close();
                MessageBox.Show("请先关闭串口!");
                return;
            }
            try
            {
                sp.PortName = portName;
                sp.BaudRate = int.Parse(baudRate);
                sp.DataBits = int.Parse(dataBits);
                sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
                sp.Parity   = (Parity)Enum.Parse(typeof(Parity), parity);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("设置串口时出现异常错误!" + ex.Message);
            }
        }
Esempio n. 4
0
 public void closeSerialPort()
 {
     StaticSerialPort.removeParser(_RFIDHelper.Parse);
 }
Esempio n. 5
0
 public void openSerialPort()
 {
     StaticSerialPort.AddParser(_RFIDHelper.Parse);
 }