Связь Modbus TCP C# - PullRequest
       44

Связь Modbus TCP C#

0 голосов
/ 26 февраля 2020

Я ищу небольшую помощь. У меня есть программа для связи с контроллером через Modbus TCP. Единственная проблема в том, что я не могу расширить Nop со 125 до 400, потому что я получил сообщение об ошибке «Недопустимый адрес данных». Не могли бы вы помочь мне с этим?

            try
        {
            byte slaveid = 1;
            byte function = 4;
            ushort id = function;

            ushort startAddress = 0;
            uint NoP = 125;

            byte[] frame = ReadInputRegistersMsg(id, slaveid, startAddress, function, NoP);
            this.Write(frame); //data send to controller
            Thread.Sleep(100);
            byte[] buffReceiver = this.Read(); //data recieving from controller
            int SizeByte = buffReceiver[8]; // Data what I got from the controller
            UInt16[] temp = null;

            if (function != buffReceiver[7])
            {
                byte[] byteMsg = new byte[9];
                Array.Copy(buffReceiver, 0, byteMsg, 0, byteMsg.Length);
                byte[] data = new byte[SizeByte];
                textBox2.Text = Display(byteMsg);

                byte[] errorbytes = new byte[3];
                Array.Copy(buffReceiver, 6, errorbytes, 0, errorbytes.Length);
                this.CheckValidate(errorbytes); // check the answer -> error message
            }
            else
            {
                byte[] byteMsg = new byte[9 + SizeByte];
                Array.Copy(buffReceiver, 0, byteMsg, 0, byteMsg.Length);
                byte[] data = new byte[SizeByte];
                textBox2.Text = Display(byteMsg); // Show received messages in windows form app
                Array.Copy(buffReceiver, 9, data, 0, data.Length);
                temp = Word.ConvertByteArrayToWordArray(data); // Convert Byte[]-> Word[]
            }
            // Result
            if (temp == null) return;
            string result = string.Empty;
            //foreach (var item in temp) // show all the data
            for(int i=0;i<100;i++) // show the first 100 data
            {
                //result += string.Format("{0} ", item);
                result += temp[i];
            }
            textBox3.Text = result; // insert the result into the textbox (windows form app)

        }
        catch
        {

        }

Сообщение ReadInputRegister следующее:

private byte[] ReadInputRegistersMsg(ushort id, byte slaveAddress, ushort startAddress, byte function, uint NoP)
        {
            byte[] frame = new byte[12];
            frame[0] = (byte)(id >> 8); // Transaction Identifier High
            frame[1] = (byte)id; // Transaction Identifier Low
            frame[2] = 0; // Protocol Identifier High
            frame[3] = 0; // Protocol Identifier Low
            frame[4] = 0; // Message Length High
            frame[5] = 6; // Message Length Low(6 bytes to follow)
            frame[6] = slaveAddress; // Slave address(Unit Identifier)
            frame[7] = function; // Function             
            frame[8] = (byte)(startAddress >> 8); // Starting Address High
            frame[9] = (byte)startAddress; // Starting Address Low           
            frame[10] = (byte)(NoP >> 8); // Quantity of Registers High
            frame[11] = (byte)NoP; // Quantity of Registers Low
            return frame;
        }

1 Ответ

0 голосов
/ 26 февраля 2020
...