using BPASmartClient.Helper; using System; using System.IO.Ports; using System.Linq; namespace BPASmartClient.SerialPort { public class SerialPortClient { #region 变量属性 private DataStorage dataStorage; public event SerialErrorReceivedEventHandler ErrorReceived; private System.IO.Ports.SerialPort comPort = new System.IO.Ports.SerialPort(); private string portName = "COM1";//串口号,默认COM1 private BaudRates baudRate = BaudRates.BR_9600;//波特率 private Parity parity = Parity.None;//校验位 private StopBits stopBits = StopBits.One;//停止位 private DataBits dataBits = DataBits.Eight;//数据位 /// /// 串口号 /// public string PortName { get { return portName; } set { portName = value; } } /// /// 波特率 /// public BaudRates BaudRate { get { return baudRate; } set { baudRate = value; } } #endregion #region 构造函数 /// /// 无参构造函数 /// public SerialPortClient(string name, BaudRates baud) { this.portName = name; this.baudRate = baud; BoundEvents(); } void BoundEvents() { comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived); comPort.ErrorReceived += new SerialErrorReceivedEventHandler(comPort_ErrorReceived); } /// /// 参数构造函数(使用枚举参数构造) /// /// 波特率 /// 奇偶校验位 /// 停止位 /// 数据位 /// 串口号 public SerialPortClient(string name, BaudRates baud, Parity par, DataBits dBits, StopBits sBits) { this.portName = name; this.baudRate = baud; this.parity = par; this.dataBits = dBits; this.stopBits = sBits; BoundEvents(); } /// /// 参数构造函数(使用字符串参数构造) /// /// 波特率 /// 奇偶校验位 /// 停止位 /// 数据位 /// 串口号 public SerialPortClient(string name, string baud, string par, string dBits, string sBits) { this.portName = name; this.baudRate = (BaudRates)Enum.Parse(typeof(BaudRates), baud); this.parity = (Parity)Enum.Parse(typeof(Parity), par); this.dataBits = (DataBits)Enum.Parse(typeof(DataBits), dBits); this.stopBits = (StopBits)Enum.Parse(typeof(StopBits), sBits); BoundEvents(); } #endregion #region 事件处理函数 /// /// 数据接收处理 /// void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (comPort.IsOpen) //判断是否打开串口 { byte[] receivedData = new byte[comPort.BytesToRead]; //创建接收字节数组 comPort.Read(receivedData, 0, receivedData.Length); //读取数据 //触发整条记录的处理 if (dataStorage != null) { dataStorage.PutData(receivedData); } } else { Console.WriteLine("请打开某个串口", "Error"); } } /// /// 错误处理函数 /// void comPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) { if (ErrorReceived != null) { ErrorReceived(sender, e); } } #endregion #region 串口关闭/打开 /// /// 端口是否已经打开 /// public bool IsOpen { get { return comPort.IsOpen; } } /// /// 判断是否有这个串口 /// public bool IsHavePort => System.IO.Ports.SerialPort.GetPortNames().Contains(PortName); /// /// 打开端口 /// /// public void Open() { lock (lck4Serial) { if (!IsHavePort) return; if (comPort.IsOpen) comPort.Close(); comPort.PortName = portName; comPort.BaudRate = (int)baudRate; comPort.Parity = parity; comPort.DataBits = (int)dataBits; comPort.StopBits = stopBits; comPort.Open(); } } /// /// 关闭端口 /// public void Close() { if (!IsHavePort) return; if (comPort.IsOpen) comPort.Close(); } /// /// 丢弃来自串行驱动程序的接收和发送缓冲区的数据 /// public void DiscardBuffer() { comPort.DiscardInBuffer(); comPort.DiscardOutBuffer(); } #endregion private object lck4Serial = new object(); #region 写入数据 /// /// 写入数据 /// /// public void Write(byte[] buffer, int offset, int count) { lock (lck4Serial) { if (!IsHavePort) return; if (!(comPort.IsOpen)) comPort.Open(); comPort.Write(buffer, offset, count); } } /// /// 读取数据 /// /// public byte[] Read() { lock (lck4Serial) { if (!IsHavePort) return new byte[0]; if (!(comPort.IsOpen)) comPort.Open(); byte[] buffer = new byte[1024]; comPort.Read(buffer, 0, buffer.Length); return buffer; } } public void Start() { Open(); } public void Stop() { Close(); } /// /// 发送数据 /// /// 发送数据 public void SendData(byte[] data) { Write(data, 0, data.Length); } public void SetDataStorage(DataStorage dataStorage) { this.dataStorage = dataStorage; } #endregion } }