S7NetPlus / s7netplus

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

ReadMultipleVars issue #58

Closed marcosdvpereira closed 6 years ago

marcosdvpereira commented 8 years ago

Reading multiple USInt vars only the first one read ok. Adding the code bellow fix my problem, but not sure if it is the right solution.

PLC.ReadMultipleVars(List):

int offset = 25;
foreach (var dataItem in dataItems)
{
    int byteCnt = VarTypeToByteLength(dataItem.VarType, dataItem.Count);
    byte[] bytes = new byte[byteCnt];

    for (int i = 0; i < byteCnt; i++)
    {
        bytes[i] = bReceive[i + offset];
    }

    offset += byteCnt + 4;

    //this code
    if (byteCnt == 1)
        offset++;

    dataItem.Value = ParseBytes(dataItem.VarType, bytes, dataItem.Count);
}
mesta1 commented 8 years ago

Can you post the code to reproduce the problem also ?

marcosdvpereira commented 8 years ago

On Plc the vars are USInt type.

public Dictionary<uint, byte> GetOptions(ushort db, uint[] offsets)
{
    var result = new Dictionary<uint, byte>();
    var read = new List<S7.Net.Types.DataItem>();

    foreach (var offset in offsets)
    {
        read.Add(new S7.Net.Types.DataItem()
        {
            Count = 1,
            DataType = S7.Net.DataType.DataBlock,
            DB = db,
            StartByteAdr = (int)offset,
            VarType = S7.Net.VarType.Byte
        });
    }

    _cpu.Read(read);

    foreach (var item in read)
        result[(uint)item.StartByteAdr] = item.Value.ToByte();

    return result;
}
mesta1 commented 8 years ago

I can't find USInt type on Step7 or on TIA Portal. Can you upload the plc program or just a small part of it that contains the db ?

marcosdvpereira commented 8 years ago

img_07072016_105419

This should return: 1, 2, 3, 4 but return: 1, 8, 0, 4.

GrayDelacluyse commented 6 years ago

I know this issue is very old, but for others searching, USint is 1 byte with a max value of 255. Therefore, your c# class/struct should actually be Byte

mycroes commented 6 years ago

This is probably fixed by a recently merged pull request. At least reading single bytes works now.

marcosdvpereira commented 6 years ago

Someone who tests this scenario just to be sure, and for me this issue can be closed.

mycroes commented 6 years ago

I tested reading single bit / byte using ReadMultipleVars as mentioned in PR #117. I'd say close this, as the datatype in PLC doesn't seem to matter anyway.