简体   繁体   English

为什么RS485返回发送的数据?

[英]Why RS485 returns sent data?

I am using RS485 adapters with MAX485. 我正在将RS485适配器与MAX485配合使用。 This problem happens ONLY when communicating PC to raspberry. 仅在将PC与树莓派通信时才会发生此问题。 Everything works fine then communicating raspberry to raspberry. 一切正常,然后将树莓与树莓进行通信。 For some reason, PC sends back command I sent to it, and only then returns the data I need. 由于某种原因,PC会发回我发送给它的命令,然后才返回我需要的数据。

I tried clearing USB com ports in and out buffers using: glbl._serialPort3.DiscardOutBuffer(); 我尝试使用glbl._serialPort3.DiscardOutBuffer();清除USB com端口的进出缓冲区glbl._serialPort3.DiscardOutBuffer(); glbl._serialPort3.DiscardInBuffer(); But the problem persists. 但是问题仍然存在。 Below is the code I have written, commented lines are only for testing, so don't mind them. 以下是我编写的代码,带注释的行仅用于测试,因此请不要介意。

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?

To answer the second part, to display values as hex -> 要回答第二部分,将值显示为十六进制->

Console.WriteLine("sent data on rs485 line: 0x" + result2.ToString("X2")); Console.WriteLine(“在rs485线上发送数据:0x” + result2.ToString(“ X2”));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM