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

标签 c# c raspberry-pi rs485

我正在使用带有 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/

相关文章:

c# - 无法始终如一地将表格放在首位

c# - 如何使用 foreach 循环制作多线程应用程序

c - C 中的指针算术

c - 将消息从 C 传递到 erlang

video - Raspberry PI 相机到 Youtube 直播问题

c# - Entity Framework 获取 CurrentContext

C# WinForms 托盘应用程序 - 未捕获 Windows 关闭事件

c - 通过网页获取ini文件的问题

Java:图像缩放速度慢

linux - OpenVPN 的 crontab 问题