S7NetPlus / s7netplus

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

Can't read a single Real #453

Open Wobeam opened 1 year ago

Wobeam commented 1 year ago

Hello,

float plc_data;

plc_data = (float)plc.Read("DB1300.DBD0.0");

In PLC DB shown absolute address is Real type.

used library version is latest.

SmackyPappelroy commented 1 year ago

You write plc_data = (float)plc.Read("DB1300.DBD0");

Wobeam commented 1 year ago

You write plc_data = (float)plc.Read("DB1300.DBD0");

Thanks of course but it doesn't matter. Seems the library can't read/write float directly. It still needs some convertations.

rubend30 commented 1 year ago

Use the Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount, byte bitAdr = 0) method instead if you want the correct type in one call.

As seen in the internal PLCAddressclass a DBDaddress is always converted to a DWord and that translates to a C# uint:

case "DBD": varType = VarType.DWord; return;

ghost commented 1 year ago

Just do this: var floatNumber = BitConverter.ToSingle(BitConverter.GetBytes((uint)plc.Read("DB200.DBD34")), 0)

FalcoGoodbody commented 1 year ago

You can also use the method which is described in the wiki at "value conversion":

float result = ((uint)plc.Read("DB1.DBD40")).ConvertToFloat();

OR you use the method to read multiple vars from a list. The list can also contain only one item. When declaring the items you can define the datatype.

Wobeam commented 1 year ago

You can also use the method which is described in the wiki at "value conversion":

float result = ((uint)plc.Read("DB1.DBD40")).ConvertToFloat();

OR you use the method to read multiple vars from a list. The list can also contain only one item. When declaring the items you can define the datatype.

Yeah, I have found conversion in docs last year. Thanks.