gpt4 book ai didi

c# - RS485为什么会返回发送的数据?

转载 作者:行者123 更新时间:2023-11-30 18:10:44 26 4
gpt4 key购买 nike

我正在使用带有 MAX485 的 RS485 适配器。仅当 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?

最佳答案

这是因为串口是非阻塞的,写入的值会被循环回来。

关于c# - RS485为什么会返回发送的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56212929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com