using System; using System.IO.Ports; public class SerialPortTest { public static void Main(string[] args) { SerialPortTest myTest = new SerialPortTest(); while (true) { myTest.Test(); } } private SerialPort mySerial; // Constructor public SerialPortTest() { } public void Test() { if (mySerial != null) if (mySerial.IsOpen) mySerial.Close(); mySerial = new SerialPort("/dev/ttyS0", 9600); mySerial.Open(); mySerial.ReadTimeout = 65; //SendData); //#01#03#00#00#00#03#05#CB //01 03 00 00 00 08 44 0C SendData(new byte[] {0x01, 0x03, 0x00, 0x00, 0x00, 0x03, 0x05, 0xCB }); Console.WriteLine(ReadData()); // } public string ReadData() { byte tmpByte; string rxString = ""; tmpByte = (byte) mySerial.ReadByte(); while (tmpByte != 255) { rxString += string.Format( "{0:X2}", tmpByte ); rxString += ", "; tmpByte = (byte) mySerial.ReadByte(); } return rxString; } public bool SendData(byte[] byteData) { mySerial.Write(byteData, 0, byteData.Length); return true; } }