public override void Disconnect() { try { ClearCmd(); Cmd.Clear(); ReadTimer.Change(-1, -1); WriteTimer.Change(-1, -1); WaitUntilThreadOver(); serialport.Close(); CreateEvent(EventReason.Disconnected, null); } catch (Exception ex) { CreateEvent(EventReason.Disconnected, ex.Message); } finally { PACK.FlushReadBuffer(); PACK.FlushWriteBuffer(); CMD.ClearTask(); serialport = null; } }
public override void Connect(object device) { if (device is SerialPort) { IsUSBSerial = false; serialport = (SerialPort)device; try { serialport.DataBits = 8; serialport.StopBits = StopBits.One; serialport.Parity = Parity.None; serialport.ReadTimeout = 50; serialport.ReceivedBytesThreshold = 1; serialport.DataReceived += new SerialDataReceivedEventHandler(SerialReadThread); serialport.ReadBufferSize = 8192; serialport.WriteBufferSize = 8192; PACK.FlushReadBuffer(); PACK.FlushWriteBuffer(); CMD.ClearTask(); serialport.Open(); WriteTimer.Change(0, 25); ReadTimer.Change(0, 25); SerialChannel = SerialChannelEnum.ChipConnect; CreateEvent(EventReason.ChipConnected, null); // 仅串口连接成功 return; } catch (Exception ex) { Disconnect(); CreateEvent(EventReason.ConnectFailed, ex.Message); return; } } CreateEvent(EventReason.ConnectFailed, "连接的不是串口设备!"); }