Serialport readbyte 没有得到正确的数据

Serialport readbyte not getting the right data

我正在 VB.net 中编写一个软件来通过 modbus 与外部设备通信 我在读取从设备接收的数据时遇到问题,举个例子,使用 sp1.ReadByte() 从设备发送的数据是这样的:

05-03-04-00-00-01-9F-FE-0B

更新:我使用此代码读取数据

Dim reponse As Byte() = New Byte(5 + (2 * nombre_registrre - 1)) {}

        For i As Integer = 0 To response.Length - 1

         response(i) = CByte(genvision.sp1.ReadByte())

        Next

但收到这个

05-03-04-01-9F-FE-0B

我想逐字节读取它,即使是那些 00 00 ,

非常感谢您的帮助

尝试:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
  byte[] array = new byte[256];
  int i = 0;
  while (serialPort1.BytesToRead > 0)
  {
    array[i]=serialPort1.ReadByte();
    i++;
  }
}

编辑 抱歉,我被 C# 标签带到了这里!下次你必须更准确,P.S 我不确定如何在 VB 中做到这一点,但应该不会有太大的不同

问题已解决,问题是:sp1.DiscardNull = true,

https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.discardnull(v=vs.110).aspx

非常感谢您的帮助