1

我尝试从数据块(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>
4

2 回答 2

0

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

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

我在 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;
        }
于 2017-08-23T04:17:52.597 回答
0

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

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

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

于 2017-08-27T21:22:57.683 回答