Read 64bit variables from Siemens PLC

1.1k views Asked by At

Good afternoon,

I am trying to read variables from a PLC using the nuGet package S7netplus.

Until I am using up to 32bit variables, everything is fine I can read and convert the result if needed (here the code i use):

 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();           
        }

But when it comes to use 64bit variables such as LReal (long real), the .ConvertToDouble(); is not working right and returns a wrong value.

The only case when I can read correctly the 64bit value is when I write it myself in the PLC like this (the LReal is 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());


        }

Any help is appreciated on how i could handle this conversion from 64bit PLC variables to c# with s7.net

Thanks, regards.

0

There are 0 answers