public static S7WriteJobDatagram BuildWrite(SiemensPlcProtocolContext context, int id, IEnumerable <WriteItem> vars)
        {
            var numberOfItems = 0;
            var result        = new S7WriteJobDatagram();

            result.Header.ProtocolDataUnitReference = (ushort)id;
            if (vars != null)
            {
                foreach (var item in vars)
                {
                    numberOfItems++;
                    result.Items.Add(new S7AddressItemSpecificationDatagram
                    {
                        TransportSize  = S7AddressItemSpecificationDatagram.GetTransportSize(item.Area, item.VarType),
                        ItemSpecLength = item.NumberOfItems,
                        DbNumber       = item.DbNumber,
                        Area           = (byte)item.Area,
                        Address        = S7AddressItemSpecificationDatagram.GetAddress(item.Offset, item.VarType)
                    });
                }

                foreach (var item in vars)
                {
                    numberOfItems--;
                    result.Data.Add(new S7DataItemSpecification
                    {
                        ReturnCode    = 0x00,
                        TransportSize = S7DataItemSpecification.GetTransportSize(item.Area, item.VarType),
                        Length        = item.NumberOfItems,
                        Data          = item.Data,
                        FillByte      = numberOfItems == 0 || item.NumberOfItems % 2 == 0 ? Array.Empty <byte>() : new byte[1],
                        ElementSize   = item.ElementSize
                    });
                }
            }
            result.Header.ParamLength = (ushort)(2 + result.Items.Count * 12);
            result.Header.DataLength  = (ushort)(S7DataItemSpecification.GetDataLength(vars) + result.Items.Count * 4);
            result.ItemCount          = (byte)result.Items.Count;
            return(result);
        }
Beispiel #2
0
        public static S7ReadJobDatagram BuildRead(SiemensPlcProtocolContext context, int id, IEnumerable <ReadItem> vars)
        {
            var result = new S7ReadJobDatagram();

            result.Header.ProtocolDataUnitReference = (ushort)id;
            if (vars != null)
            {
                foreach (var item in vars)
                {
                    result.Items.Add(new S7AddressItemSpecificationDatagram
                    {
                        TransportSize  = S7AddressItemSpecificationDatagram.GetTransportSize(item.Area, item.VarType),
                        ItemSpecLength = item.Length,
                        DbNumber       = item.DbNumber,
                        Area           = (byte)item.Area,
                        Address        = S7AddressItemSpecificationDatagram.GetAddress(item.Offset, item.VarType)
                    });
                }
            }
            result.Header.ParamLength = (ushort)(2 + result.Items.Count * 12);
            result.ItemCount          = (byte)result.Items.Count;
            return(result);
        }