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

时间:2017-08-22 11:33:50

标签: c# plc

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

2 个答案:

答案 0 :(得分: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;
        }

答案 1 :(得分:0)

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

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

偏移0 == 254,偏移1 == 7,偏移2到8 ==&#39; JAMES17&#39;