我写了一个测试方法,涉及为RS232串行通信应用程序编写/读取数组
private void RS232TestCommand()
{
byte[] test = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
for (int i = 0; i < 10; i++)
{
// Send test command
comport.Write(test, 0, test.Length);
LogOutgoing(LogMsgType.Outgoing, "Find Serial Command Sent" + "\n");
// Read response and store in buffer
int bytes = comport.BytesToRead;
byte[] response = new byte[bytes];
comport.Read(response, 0, response.Length);
LogIncoming(LogMsgType.Incoming, ByteArrayToHexString(response) + "\n");
此日志只能读取{AA,BB,CC,DD,EE,FF},但它似乎也包含下面显示的“ComboByte []”。
// Create 4 byte array to hold first 4 bytes
var firstfour = new byte[4];
Array.Copy(test,firstfour,firstfour.Length);
// Convert i to a hex value
byte iHex = (byte)i;
byte[] iHexArray = { iHex };
LogOutgoing(LogMsgType.Outgoing, "Short Address" + i.ToString() + "\n");
// Combine iHex with firstfour to create a new byte[]
byte[] ComboByte = {iHex, firstfour[0], firstfour[1], firstfour[2], firstfour[3] };
comport.Write(ComboByte, 0, ComboByte.Length);
}
}
输出对于i = 0(AA,BB,CC,DD,EE,FF)是正确的,但是对于i = 1 +,响应看起来像“ComboByte”+“响应”......或类似{82 ,AA,BB,CC,DD,E0,01,AA,BB,CC,DD,EE,FF}。
如何确保我只返回“Response”字节数组?