簡體   English   中英

Modbus TCP 通訊 C#

[英]Modbus TCP Communication C#

我正在尋找一些幫助。 我有一個程序可以通過 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;
        }

暫無
暫無

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

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