尝试使用 snap 7 从 plc s7 1200 读取数据并得到错误的字符串

Try to read data from plc s7 1200 with snap 7 and get the wrong string

我尝试从数据块 (DB60) 读取数据,但我只得到 ?5。所以在数据块中应该是JAMES17

private void button1_Click(object sender, EventArgs e)
      {
            if (button1.Text == "Connect PLC")
            {
                button1.Text = "Disconnect PLC";
                ClassPLCS7Client.PLCClientConnect_Result ConnectResult = new 
ClassPLCS7Client.PLCClientConnect_Result();
                ConnectResult = PLCClient.Connect(("192.168.0.2"), 0, 1);

               if (ConnectResult.State == ClassPLCS7Client.PLCClientConnectState.Connected)
            {
                this.label1.Text = "Connected PLC1 " + ConnectResult.ReSultString;
                label1.ForeColor = Color.Green;
                ClassPLCS7Client.ReadDataBlockString_Result read = new ClassPLCS7Client.ReadDataBlockString_Result();
                read = PLCClient.ReadDataBlockString(60, 0, 7);       
                this.textBox1.Text = read.DataValue[0];
                //this.textBox1.Text = arr4[];// read.ReSultString;
            }
            else
            {
                this.label1.Text = "Fail " + ConnectResult.ReSultString;

                label1.ForeColor = Color.Red;
            }

        }
        else
        {
            button1.Text = "Connect PLC";
            disconnect_plc();
            this.label1.Text = "Disconnect";
            label1.ForeColor = Color.Black;
        }

    }<code>

从我尝试将 DB60 更改为字符串 "JAMES17" 结果是

https://www.dropbox.com/s/awdadp53tuyszpe/2.PNG?dl=1

我在 class ReadDataBlockString_Result 中找到了这段代码。所以我调用了这个函数,但不确定如何使用它。

public ReadDataBlockString_Result ReadDataBlockString(int DataBlockNumber, 
int StartAddress, int LenghtOfRead)
        {
            ReadDataBlockString_Result rt = new ReadDataBlockString_Result();
            rt.MemoryByte = new byte[256];


            //if (this.State == PLCClientConnectState.Connected)
            //{

                rt.DataValue = new string[LenghtOfRead];
                int i = 0;
                int CaptureIndex = StartAddress;
                for (i = 0; i <= LenghtOfRead - 1; i++)
                {
                    rt.ResultCode = PLCClient.DBRead(DataBlockNumber, CaptureIndex, 256, rt.MemoryByte);
                    CaptureIndex = CaptureIndex + 256;

                    if (rt.ResultCode == 0)
                    {
                        string Convertedvalue = null;
                        Convertedvalue = System.Text.Encoding.ASCII.GetString(rt.MemoryByte);
                        rt.DataValue[i] = Convertedvalue;
                    }


                }

            //}
            //else
            //{
            //    rt.ResultCode = -1;
            //}

            rt.ReSultString = PLCClient.ErrorText(rt.ResultCode);

            return rt;
        }

首先: 你将 J 字符串放在字节偏移量 0 和 A​​ 字符串中字节偏移量 256 和字节偏移量 512 中的 M 字符串等等......你没有连续字节的字符串,因为它应该是。

其次: 当 S7 存储字符串时,前两个字节被保留用于存储第一个最大字符串大小(以字节为单位)和第二个字符串的实际大小。因此,在您的情况下,您的内存必须包含以下内容:(假设保留内存大小为 256 字节)

偏移量 0 == 254,偏移量 1 == 7,偏移量 2 到 8 == 'JAMES17'