public void SerialPortOpen(string portName, String baudRate, string dataBits, string stopBits, string parity, string handshake) { if (serialPort.IsOpen) { Close(); } serialPort.PortName = portName; serialPort.BaudRate = Convert.ToInt32(baudRate); serialPort.DataBits = Convert.ToInt16(dataBits); if (handshake == "None") { //Never delete this property serialPort.RtsEnable = true; serialPort.DtrEnable = true; } SerialPortEventArgs args = new SerialPortEventArgs(); try { serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits); serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity); serialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), handshake); serialPort.WriteTimeout = 1000; /*Write time out*/ serialPort.Open(); serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceived); args.isOpend = true; } catch (System.Exception) { args.isOpend = false; } ComOpenEvent?.Invoke(this, args); }
/// <summary> /// Open Serial port /// </summary> /// <param name="portName"></param> /// <param name="baudRate"></param> public void Open(string portName, string baudRate) { if (_sp.IsOpen) { Close(); } _sp.PortName = portName; _sp.BaudRate = Convert.ToInt32(baudRate); _sp.DataBits = 8; /** * If the Handshake property is set to None the DTR and RTS pins * are then freed up for the common use of Power, the PC on which * this is being typed gives +10.99 volts on the DTR pin & +10.99 * volts again on the RTS pin if set to true. If set to false * it gives -9.95 volts on the DTR, -9.94 volts on the RTS. * These values are between +3 to +25 and -3 to -25 volts this * give a dead zone to allow for noise immunity. * http://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners */ //Never delete this property _sp.RtsEnable = true; _sp.DtrEnable = true; var args = new SerialPortEventArgs(); try { _sp.StopBits = StopBits.One; _sp.Parity = Parity.None; _sp.Handshake = Handshake.None; _sp.WriteTimeout = 1000; /*Write time out*/ _sp.Open(); _sp.DataReceived += DataReceived; args.IsOpend = true; } catch (Exception) { args.IsOpend = false; } ComOpenEvent?.Invoke(this, args); }
/// <summary> /// 串口打开PLUS /// 端口号,波特率,数据位,停止位,校验,handshake /// </summary> /// <param name="portName">端口号</param> /// <param name="baudRate">波特率</param> /// <param name="dataBits">数据位</param> /// <param name="stopBits">停止位</param> /// <param name="parity">校验</param> /// <param name="handshake"></param> public void Open(string portName, String baudRate, string dataBits, string stopBits, string parity, string handshake) { SerialPortEventArgs Args = new SerialPortEventArgs(); try { _serialPort.PortName = portName; _serialPort.BaudRate = Convert.ToInt32(baudRate); _serialPort.DataBits = Convert.ToInt32(dataBits); _serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits); _serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity); _serialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), handshake); _serialPort.WriteTimeout = 1000; /*Write time out*/ if (handshake == "None") { //Never delete this property _serialPort.RtsEnable = true; _serialPort.DtrEnable = true; } _serialPort.Open(); _serialPort.DataReceived += SerialPortDataReceived; Args.isOpened = true; } catch (Exception) { //Console.WriteLine(ex); Args.message = $"{portName} open failed"; Args.isOpened = false; } if (ComOpenEvent != null) { ComOpenEvent.Invoke(this, Args); } }