public static void openDataPort(string port_name, int baud_rate) { if (bDataPortOpen == true) { return; } try { #if UDP_TRANSE //initial_udp_server(); //IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0); ////The epSender identifies the incoming clients //EndPoint epSender = (EndPoint)ipeSender; ////Start receiving data //serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, // SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender); #endif #if SERIAL_PORT_TRANSE if (!StaticDataPort.getStaticSerialPort(port_name, baud_rate).IsOpen) { StaticDataPort.getStaticSerialPort(port_name, baud_rate).Open(); } #endif } catch (System.Exception ex) { MessageBox.Show(ex.Message + ",请检查后重启本系统", "信息提示", MessageBoxButtons.OK); } bDataPortOpen = true; }
public static SerialPort getStaticSerialPort(string port_name, int baut_rate) { if (StaticDataPort.comport == null) { StaticDataPort.comport = new SerialPort(); comport.DataReceived += StaticDataPort.port_DataReceived; StaticDataPort.resetStaticSerialPort(port_name, baut_rate);//使用统一配置参数 } return(StaticDataPort.comport); }
public static void openDataPort() { if (bDataPortOpen == true) { return; } try { #if UDP_TRANSE initial_udp_server(); IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0); //The epSender identifies the incoming clients EndPoint epSender = (EndPoint)ipeSender; //Start receiving data serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender); #endif #if SERIAL_PORT_TRANSE //serialPortConfig config = serialPortConfig.getDefaultConfig(); //if (config != null && config.portName.Length > 0) //{ // if (!StaticDataPort.getStaticSerialPort(config.portName, int.Parse(config.baudRate)).IsOpen) // { // StaticDataPort.getStaticSerialPort(config.portName, int.Parse(config.baudRate)).Open(); // } //} //else //{ // MessageBox.Show("串口打开失败,请先设置串口!", "信息提示", MessageBoxButtons.OK); //} if (!StaticDataPort.getStaticSerialPort(staticClass.serial_port_name, 19200).IsOpen) { StaticDataPort.getStaticSerialPort(staticClass.serial_port_name, 19200).Open(); } #endif } catch (System.Exception ex) { MessageBox.Show(ex.Message + ",请检查后重启本系统", "信息提示", MessageBoxButtons.OK); } bDataPortOpen = true; }
/// <summary> /// 串口关闭时可能引起线程死锁,因此这里要求首先安全关闭串口 /// </summary> /// <param name="portName"></param> /// <param name="baudRate"></param> /// <param name="parity"></param> /// <param name="dataBits"></param> /// <param name="stopBits"></param> public static void resetStaticSerialPort(string portName, string baudRate, string parity, string dataBits, string stopBits) { SerialPort sp = StaticDataPort.getStaticSerialPort(portName, int.Parse(baudRate)); bool biniOpened = sp.IsOpen; if (biniOpened) { //sp.Close(); MessageBox.Show("请先关闭串口!"); return; } try { sp.PortName = portName; sp.BaudRate = int.Parse(baudRate); sp.DataBits = int.Parse(dataBits); sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits); sp.Parity = (Parity)Enum.Parse(typeof(Parity), parity); } catch (System.Exception ex) { MessageBox.Show("设置串口时出现异常错误!" + ex.Message); } }