public void OpenSp(string cmName, string brulate) { try { if (Sp.IsOpen) { Sp.Close(); Sp.BaudRate = int.Parse(brulate); Sp.PortName = cmName; Sp.DataBits = 8; Sp.Parity = System.IO.Ports.Parity.None; Sp.StopBits = System.IO.Ports.StopBits.One; Sp.WriteTimeout = 50; Sp.ReadBufferSize = 2048; Sp.WriteBufferSize = 2048; Sp.Open(); MessageBox.Show("串口打开成功,端口号:" + cmName + " 波特率:" + brulate); } else { Sp.BaudRate = int.Parse(brulate); Sp.PortName = cmName; Sp.DataBits = 8; Sp.Parity = System.IO.Ports.Parity.None; Sp.StopBits = System.IO.Ports.StopBits.One; Sp.WriteTimeout = 50; Sp.ReadBufferSize = 2048; Sp.WriteBufferSize = 2048; Sp.Open(); MessageBox.Show("串口打开成功,端口号:" + cmName + " 波特率:" + brulate); } } catch (Exception ex) { MessageBox.Show("串口打开失败" + ex.Message); } }