我正在将RS485适配器与MAX485配合使用。仅在将PC与树莓派通信时才会发生此问题。一切正常,然后将树莓与树莓进行通信。由于某种原因,PC会发回我发送给它的命令,然后才返回我需要的数据。
我尝试使用以下方法清除USB com端口的进出缓冲区:
glbl._serialPort3.DiscardOutBuffer();
glbl._serialPort3.DiscardInBuffer();
但是问题仍然存在。以下是我编写的代码,带注释的行仅用于测试,因此请不要介意。
public static void LLSread()
{
byte adress;
int result = glbl._serialPort3.ReadByte();
//Console.WriteLine("Received Data:" + result); //b.ToString("X2") to print out as hex
if (result == 49)
{
result = glbl._serialPort3.ReadByte();
if (result == 3 || result == 1 || result == 2 || result == 4)
{
adress = (byte)result;
result = glbl._serialPort3.ReadByte();
if (result == 6)
{
result = glbl._serialPort3.ReadByte();
if (result == 253 || result == 108 || result == 57 || result == 147)
{
glbl._serialPort3.DiscardOutBuffer();
glbl._serialPort3.DiscardInBuffer();
sendLLS(adress);
Console.WriteLine("data sent");
}
}
}
}
}
public static void sendLLS(byte adress)
{
byte[] data = { 0x3e, adress, 0x06, 0x17, 0x0b, 0xb8, 0x11, 0x30 };
byte crc = ComputeChecksum(data);
byte[] aftercrc = { 0x3e, adress, 0x06, 0x17, 0x0b, 0xb8, 0x01, 0x30, crc };
Thread.Sleep(10);
glbl._serialPort3.Write(aftercrc, 0, aftercrc.Length);
for(int i = 0; i < 9; i++)
{
// int result2 = glbl._serialPort2.ReadByte();
// Console.WriteLine("sent data on rs485 line:" + result2);
}
}
Command is [49, 3, 6, 253] and for some reason response i get is [49, 3, 6, 253, 62, 3, 6, 23, 11, 184, 1, 48, 6]. I should only get [62, 3, 6, 23, 11, 184, 1, 48, 6], which i do if i communicate raspberry to raspberry, but not when communicating raspberry to PC. Can someone help me?
答案 0 :(得分:-1)
要回答第二部分,将值显示为十六进制->
Console.WriteLine(“在rs485线上发送数据:0x” + result2.ToString(“ X2”));