簡體   English   中英

為什么RS485返回發送的數據?

[英]Why RS485 returns sent data?

我正在將RS485適配器與MAX485配合使用。 僅在將PC與樹莓派通信時才會發生此問題。 一切正常,然后將樹莓與樹莓進行通信。 由於某種原因,PC會發回我發送給它的命令,然后才返回我需要的數據。

我嘗試使用glbl._serialPort3.DiscardOutBuffer();清除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?

要回答第二部分,將值顯示為十六進制->

Console.WriteLine(“在rs485線上發送數據:0x” + result2.ToString(“ X2”));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM