S7NetPlus / s7netplus

S7.NET+ -- A .NET library to connect to Siemens Step7 devices
MIT License
1.35k stars 596 forks source link

Problem writing Real in recent versions #405

Open blindsq opened 3 years ago

blindsq commented 3 years ago

I find that I cannot use ConvertToUInt()

my code: double AbsVel = double.Parse(textBox3.Text); plc.Write("DB4.DBD18", AbsVel.ConvertToUInt());

I get error: 'double' does not contain a definition for 'ConvertToUInt' and the best extension method overload 'Conversion.ConvertToUInt(int)' requires a receiver of type 'int'

Can you tell me what I am doing wrong or suggest a way to make this work?

Thank you

FalcoGoodbody commented 3 years ago

Hello @blindsq ,

sorry for the late response. I have checked your issue and testet it myself with the following code as descriped in the wiki for value-conversion:

float val = (float)35.687;
plc.Write("DB1.DBD40", val.ConvertToUInt());

This works fine with my plc.

You can also try to use multiple-vars.

private static readonly List<DataItem> dataItemsWrite = new List<DataItem>();

private static readonly DataItem varRealWrite = new DataItem()
     {
        DataType = DataType.DataBlock,
        VarType = VarType.Real,
        DB = 83,
        BitAdr = 0,
        Count = 1,
        StartByteAdr = 158,
        Value = new object()
     };

varRealWrite.Value = (float)78.77;
dataItemsWrite.Add(varRealWrite);

myPLC.Write(dataItemsWrite.ToArray());