public bool CloseSerialPort() { try //尝试关闭串口 { CurrentSerialPort.DiscardOutBuffer(); //清发送缓存 CurrentSerialPort.DiscardInBuffer(); //清接收缓存 //WaitClose = true;//激活正在关闭状态字,用于在串口接收方法的invoke里判断是否正在关闭串口 CurrentSerialPort.Close(); //关闭串口 // WaitClose = false;//关闭正在关闭状态字,用于在串口接收方法的invoke里判断是否正在关闭串口 SetAfterClose(); //成功关闭串口或串口丢失后的设置 bComPortIsOpen = false; } catch //如果在未关闭串口前,串口就已丢失,这时关闭串口会出现异常 { if (CurrentSerialPort.IsOpen == false) //判断当前串口状态,如果ComPort.IsOpen==false,说明串口已丢失 { SetComLose(); } else//未知原因,无法关闭串口 { //throw new Exception("unable close serial port"); OnSerialPortMiss(new SerialPortMissingEventArgs("unable close serial port")); } } return(true); }
public bool SendDataPacket(byte[] dataPackeg) { try { CurrentSerialPort.DiscardInBuffer();//清接收缓存 ByteMode = true; CurrentSerialPort.Write(dataPackeg, 0, dataPackeg.Length); } catch (Exception exception) { Console.WriteLine(exception.Message); return(false); } return(true); }
private void ComReceive(object sender, SerialDataReceivedEventArgs e) { bReceiveCompleted = false; if (bRecStaus) //如果已经开启接收 { try { Thread.Sleep(50); ReceivedDataPacket = new byte[CurrentSerialPort.BytesToRead]; ReceivedDataPacketChar = new char[CurrentSerialPort.BytesToRead]; // change to char datas if (ByteMode) { CurrentSerialPort.Read(ReceivedDataPacket, 0, ReceivedDataPacket.Length); receiveArrByteQueue.Enqueue(ReceivedDataPacket); receiveArrCharQueue.Clear();//清空Char接收缓存 //OnSerialPortMiss(new SerialPortMissingEventArgs("test event byte")); } else { CurrentSerialPort.Read(ReceivedDataPacketChar, 0, ReceivedDataPacketChar.Length); receiveArrCharQueue.Enqueue(ReceivedDataPacketChar); receiveArrByteQueue.Clear();//清空Byte接收缓存 // OnSerialPortMiss(new SerialPortMissingEventArgs("test event char")); } bReceiveCompleted = true; } catch (Exception) { if (CurrentSerialPort.IsOpen == false) //如果ComPort.IsOpen == false,说明串口已丢失 { SetComLose(); //串口丢失后相关设置 } else { // throw new Exception("unable to receive data"); OnSerialPortMiss(new SerialPortMissingEventArgs("unable close serial port")); } } } else //暂停接收 { CurrentSerialPort.DiscardInBuffer(); //清接收缓存 } }
private void ComReceive(object sender, SerialDataReceivedEventArgs e) { ReceiveCompleted = false; if (_recStaus) //如果已经开启接收 { try { Thread.Sleep(50); ReceivedDataPacket = new byte[CurrentSerialPort.BytesToRead]; ReceivedDataPacketChar = new char[CurrentSerialPort.BytesToRead]; // change to char datas if (ByteMode) { CurrentSerialPort.Read(ReceivedDataPacket, 0, ReceivedDataPacket.Length); /* * string s = ""; * for (int a = 0; a < ReceivedDataPacket.Length; a++) * s += ReceivedDataPacket[a] + " "; * new LOG.LogClass().WriteLogFile(LOG.LogSerial.logSerialnum + "---serialGetData-->" + s); */ } else { CurrentSerialPort.Read(ReceivedDataPacketChar, 0, CurrentSerialPort.BytesToRead); } ReceiveCompleted = true; } catch (Exception) { if (CurrentSerialPort.IsOpen == false) //如果ComPort.IsOpen == false,说明串口已丢失 { SetComLose(); //串口丢失后相关设置 } else { throw new Exception("unable to receive data"); } } } else //暂停接收 { CurrentSerialPort.DiscardInBuffer(); //清接收缓存 } }
public void CleanBuffer() { CurrentSerialPort.DiscardOutBuffer(); //清发送缓存 CurrentSerialPort.DiscardInBuffer(); //清接收缓存 }