C# 对串口的操作
初始化 串口
SerialPort sp = new SerialPort(); sp.PortName = BasicParameters.IniReadValue(strPath, "Config", "Port"); sp.BaudRate = 9600; sp.DataBits = 8; sp.Parity = Parity.None; sp.StopBits = StopBits.One; sp.DtrEnable = true; //启用控制终端就续信号 sp.RtsEnable = true; //启用请求发送信号 sp.ReadTimeout = 300; sp.WriteTimeout = -1; // sp.ReceivedBytesThreshold = 1; sp.Open();
向串口发送数据
serialPort.Write("0026\r\n");
接收数据
int n = serialPort.BytesToRead; byte[] buf = new byte[n]; serialPort.Read(buf, 0, n); string receivedata = System.Text.Encoding.ASCII.GetString(buf); if (receivedata.Contains("0026")) { }