class Program { private static byte[] toread = new byte[12]; private static byte[] FranklinismRe = new byte[8]; static SerialPort com; static byte[] towrite = new byte[7] { 0xAB, 0xBA, 0x03, 0x03, 0x31, 0x01, 0x01 }; static byte[] towrite2 = new byte[7] { 0xAB, 0xBA, 0x03, 0x03, 0x31, 0x05, 0x01 }; static void Main(string[] args) { com = new SerialPort("COM4", 115200, Parity.None, 8, StopBits.One); if (!com.IsOpen) { com.Open(); } com.ReadTimeout = 2000; com.WriteTimeout = 2000; com.Write(towrite2, 0, towrite2.Length); for (int i = 0; i < FranklinismRe.Length; i++) { FranklinismRe[i] = Convert.ToByte(com.ReadByte()); } com.Write(towrite, 0, towrite.Length); com.BytesToRead(); for (int i = 0; i < toread.Length; i++) { toread[i] = Convert.ToByte(com.ReadByte()); } com.Dispose(); Console.ReadKey(); } }
两条命令,只发其中任何一条都是好的,
但是两条依次发,会在第二条的com.BytesToRead() 返回0
导致ReadByte()超时。这是什么原因?有什么办法解决?