private SerialPort CreateSerialPort(SerialPortModel model) { var serialPort = new SerialPort { PortName = model.SelectSerialPort, BaudRate = model.SelectBaudRate, DataBits = model.SelectDataBits, Parity = model.GetParity(), StopBits = model.GetStopBits() }; serialPort.DataReceived += SerialPort_DataReceived; return(serialPort); }
public bool OpenSP() { if (SPserialPort != null && SPserialPort.IsOpen) { return(CloseSP()); } try { SPserialPort.PortName = SerialPortModel.SPPort; SPserialPort.BaudRate = SerialPortModel.SPBaudRate; SPserialPort.DataBits = SerialPortModel.SPDataBits; SPserialPort.StopBits = SerialPortModel.GetStopBits(SerialPortModel.SPStopBits.ToString()); SPserialPort.Parity = SerialPortModel.GetParity(SerialPortModel.SPParity.ToString()); SPserialPort.WriteBufferSize = 1048576; /* 输出缓冲区的大小为1048576字节 = 1MB */ SPserialPort.ReadBufferSize = 2097152; /* 输入缓冲区的大小为2097152字节 = 2MB */ /* 字节编码 */ if (SerialPortModel.ASCIIEnable) { SPserialPort.Encoding = System.Text.Encoding.ASCII; } else if (SerialPortModel.UTF8Enable) { SPserialPort.Encoding = System.Text.Encoding.UTF8; } else if (SerialPortModel.UTF16Enable) { SPserialPort.Encoding = System.Text.Encoding.Unicode; } else if (SerialPortModel.UTF32Enable) { SPserialPort.Encoding = System.Text.Encoding.UTF32; } /* 发送请求(RTS)信号 */ if (SerialPortModel.RtsEnable) { SPserialPort.RtsEnable = true; } else { SPserialPort.RtsEnable = false; } /* 数据终端就绪(DTR)信号 */ if (SerialPortModel.DtrEnable) { SPserialPort.DtrEnable = true; } else { SPserialPort.DtrEnable = false; } /* 流控制 */ if (SerialPortModel.NoneEnable) { SPserialPort.Handshake = Handshake.None; } else if (SerialPortModel.RequestToSendEnable) { SPserialPort.Handshake = Handshake.RequestToSend; } else if (SerialPortModel.XOnXOffEnable) { SPserialPort.Handshake = Handshake.XOnXOff; } else if (SerialPortModel.RequestToSendXOnXOffEnable) { SPserialPort.Handshake = Handshake.RequestToSendXOnXOff; } /* 数据接收事件 */ SPserialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived); /* 信号状态事件 */ SPserialPort.PinChanged += new SerialPinChangedEventHandler(SerialPortModel.SerialPort_PinChanged); SPserialPort.Open(); if (SPserialPort.IsOpen) { SerialPortModel.SPBrush = Brushes.GreenYellow; SerialPortModel.OpenCloseSP = "关闭串口"; DepictInfo = string.Format("成功打开串行端口{0}、波特率{1}、数据位{2}、停止位{3}、校验位{4}", SPserialPort.PortName, SPserialPort.BaudRate.ToString(), SPserialPort.DataBits.ToString(), SPserialPort.StopBits.ToString(), SPserialPort.Parity.ToString()); SerialPortModel.SPPortEnable = false; SerialPortModel.SPBaudRateEnable = false; SerialPortModel.SPDataBitsEnable = false; SerialPortModel.SPStopBitsEnable = false; SerialPortModel.SPParityEnable = false; return(true); } else { DepictInfo = "串行端口打开失败"; return(false); } } catch (UnauthorizedAccessException e) { DepictInfo = string.Format("[{0}]端口访问被拒绝", e.HResult.ToString("X")); return(false); } catch (ArgumentOutOfRangeException e) { DepictInfo = string.Format("串口属性{0}是非法的", e.ParamName); return(false); } catch (ArgumentException e) { DepictInfo = string.Format("串口{0}不支持", e.ParamName); return(false); } catch (IOException e) { DepictInfo = string.Format("[{0}]端口处于无效状态", e.HResult.ToString("X")); return(false); } catch (InvalidOperationException e) { DepictInfo = string.Format("[{0}]指定端口已经打开", e.HResult.ToString("X")); return(false); } }