日期:2014-05-18 浏览次数:21177 次
public class RevSerialPort { SerialPort serialPort; StringBuilder sb; public RevSerialPort() { sb = new StringBuilder(); serialPort = new SerialPort(); serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); } public RevSerialPort(string portName) { sb = new StringBuilder(); serialPort = new SerialPort(portName); serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); } public RevSerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) { sb = new StringBuilder(); serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits); serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); } void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { sb.Append(serialPort.ReadExisting()); int index = sb.ToString().IndexOf((char)0xd); if (index > 0) { try { System.Windows.Forms.SendKeys.SendWait(sb.ToString(0, index) + "~"); } finally { sb.Remove(0, index + 1); } } } public void Start() { serialPort.Open(); } public void Stop() { if (serialPort.IsOpen) serialPort.Close(); } }