从西门子PLC中读取64位变量

时间:2017-01-02 14:35:42

标签: c# plc

下午好,

我正在尝试使用nuGet包S7netplus从PLC读取变量。

直到我使用最多32位变量,一切都很好我可以阅读并转换结果,如果需要(这里是我使用的代码):

 private void plcBut_Click(object sender, EventArgs e)
        {
            var plc = new Plc(CpuType.S71200, "192.168.6.1", 0, 1);

            plc.Open();                   

            bool DB1bool1 = (bool)plc.Read("DB1.DBX0.0");
            MessageBox.Show("DB1.DBX0.0: " + DB1bool1);

            bool DB1bool2 = (bool)plc.Read("DB1.DBX0.1");
            MessageBox.Show("DB1.DBX0.1: " + DB1bool2);

            var DB1int = (ushort)plc.Read("DB1.DBW2.0");
            MessageBox.Show("DB1.DBW2.0: " + DB1int);

            var DB1dint = (uint)plc.Read("DB1.DBD4.0");
            MessageBox.Show("DB1.DBD4.0: " + DB1dint);

            var DB1word =(ushort)plc.Read("DB1.DBW8.0");
            MessageBox.Show("DB1.DBW8.0: " + DB1word);

            var DB1dword = (uint)plc.Read("DB1.DBD10.0");
            MessageBox.Show("DB1.DBD10.0: " + DB1dword);

            var DB1real = ((uint)plc.Read("DB1.DBD14.0")).ConvertToDouble();            
            MessageBox.Show("DB1.DBD14.0: " + DB1real);

            var DB1lreal = ((uint)plc.Read("DB1.DBD18.0")).ConvertToDouble();               

            MessageBox.Show("DB1.DBD18.0: " + DB1lreal);           

            this.Close();           
        }

但是当使用诸如LReal(long real)之类的64位变量时,.ConvertToDouble();无法正常工作并返回错误的值。

唯一可以正确读取64位值的情况是我自己在PLC中写这样的(LReal是DBD18):

private void button5_Click(object sender, EventArgs e)
        {
            var plc = new Plc(CpuType.S71200, "192.168.6.1", 0, 1);

            plc.Open();

            bool val = true;
            bool val2 = true;
            short val3 = 25;
            short val4 = 2500;
            short val5 = 123;
            double val6 = 123456;
            double val7 = 5.5;
            double val8 = 8.88;



            plc.Write("DB1.DBX0.0", val);
            plc.Write("DB1.DBX0.1", val2);
            plc.Write("DB1.DBW2.0", val3.ConvertToUshort());
            plc.Write("DB1.DBD4.0", val4.ConvertToUshort());
            plc.Write("DB1.DBW8.0", val5.ConvertToUshort());
            plc.Write("DB1.DBD10.0", val6.ConvertToUInt());
            plc.Write("DB1.DBD14.0", val7.ConvertToUInt());            
            plc.Write("DB1.DBD18.0", val8.ConvertToUInt());


        }

如何处理从64位PLC变量到s #.net的转换的任何帮助表示赞赏

谢谢,问候。

0 个答案:

没有答案