示例#1
0
 public bool Install(
     string portname,
     int baudrate,
     int databits,
     string parity,
     string stopbits,
     string handshake,
     CommPort.delegateSerialRead cbSerialRead)
 {
     try
     {
         this.m_CommPort                  = new SerialPort(portname, baudrate, (Parity)Enum.Parse(typeof(Parity), parity), databits, (StopBits)Enum.Parse(typeof(StopBits), stopbits));
         this.m_CommPort.Handshake        = (Handshake)Enum.Parse(typeof(Handshake), handshake);
         this.m_CommPort.DiscardNull      = true;
         this.m_CommPort.RtsEnable        = true;
         this.m_CommPort.DtrEnable        = true;
         this.m_CommPort.ReadTimeout      = 2000;
         this.m_CommPort.WriteTimeout     = 2000;
         this.m_CommPort.ReadBufferSize  *= 16;
         this.m_CommPort.WriteBufferSize *= 16;
         this.m_CommPort.DataReceived    += new SerialDataReceivedEventHandler(this.SerialDataReceived);
         this.m_cbSerialRead              = new CommPort.delegateSerialRead(cbSerialRead.Invoke);
         this.m_CommPort.Open();
         this.m_CommPort.DiscardInBuffer();
         this.m_CommPort.DiscardOutBuffer();
     }
     catch (Exception ex)
     {
         this.m_CommPort = (SerialPort)null;
         return(false);
     }
     return(true);
 }
示例#2
0
 public bool Uninstall()
 {
     try
     {
         if (this.m_CommPort != null)
         {
             if (this.m_CommPort.IsOpen)
             {
                 this.m_CommPort.DataReceived -= new SerialDataReceivedEventHandler(this.SerialDataReceived);
                 this.m_cbSerialRead           = (CommPort.delegateSerialRead)null;
                 Thread thread = new Thread(new ThreadStart(this.ClosePortThread));
                 thread.Start();
                 thread.Join();
             }
             this.m_CommPort.Dispose();
             this.m_CommPort = (SerialPort)null;
         }
     }
     catch (Exception ex)
     {
         return(false);
     }
     return(true);
 }