首页新闻找找看学习计划

SerialPort 的问题

0
悬赏园豆:10 [已关闭问题] 关闭于 2017-05-27 11:00
 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()超时。这是什么原因?有什么办法解决?        

左眼水星的主页 左眼水星 | 初学一级 | 园豆:41
提问于:2017-05-27 09:52
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册