有个C#的问题想请教,在线等!!
我用C#中的serialport类做一个串口通信,遇到个问题,我查看了资料“通过调用一次serialport类中的Read()方法并不能确保所读取的数据就是完整一桢”,我现在就遇到这个问题,我用Read()方法只能得到我数据的前9个字节,后面的数据得不到,百思不得其解,望赐教!Byte[] BSendTemp = { 0x00, 0xff, 0xff, 0xcc, 0x01, 0x01, 0x02, 0x40, 0x00, 0x44 }; //建立临时字节数组对象
  serialPort.Write(BSendTemp, 0, 10);//发送命令  
  Thread.Sleep(4000);
  int count = serialPort.BytesToRead;
//为什么我只能读到9byte的数据,我一共有50几byte,百思不得其解
  byte[] BReceived = new byte[count];  serialPort.Read(BReceived, 0, count);
    
  MessageBox.Show(count.ToString());

解决方案 »

  1.   


     private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                int bytes = 0;
                String strMoney = String.Empty;//钱数
                String strNumber = String.Empty;//币数
                String strMoneyAndNumber = String.Empty;//钱数+币数
                bytes = serialPort.BytesToRead;//读取串口中的数据            byte[] buffer = new byte[bytes];//转换成数组            for (int i = 0; i < bytes; i++)
                {
                    buffer[i] = (byte)(serialPort.ReadByte());//循环遍历每个元素
                }
                if (bytes != 0)//判断数组是否为0
                {............}