Esempio n. 1
0
        /// <summary>
        /// Reads multiple vars in a single request.
        /// You have to create and pass a list of DataItems and you obtain in response the same list with the values.
        /// Values are stored in the property "Value" of the dataItem and are already converted.
        /// If you don't want the conversion, just create a dataItem of bytes.
        /// The number of DataItems as well as the total size of the requested data can not exceed a certain limit (protocol restriction).
        /// </summary>
        /// <param name="dataItems">List of dataitems that contains the list of variables that must be read.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is None.
        /// Please note that cancellation is advisory/cooperative and will not lead to immediate cancellation in all cases.</param>
        public async Task <List <DataItem> > ReadMultipleVarsAsync(List <DataItem> dataItems, CancellationToken cancellationToken = default)
        {
            //Snap7 seems to choke on PDU sizes above 256 even if snap7
            //replies with bigger PDU size in connection setup.
            AssertPduSizeForRead(dataItems);

            try
            {
                var dataToSend = BuildReadRequestPackage(dataItems.Select(d => DataItem.GetDataItemAddress(d)).ToList());
                var s7data     = await RequestTsduAsync(dataToSend, cancellationToken);

                ValidateResponseCode((ReadWriteErrorCode)s7data[14]);

                ParseDataIntoDataItems(s7data, dataItems);
            }
            catch (SocketException socketException)
            {
                throw new PlcException(ErrorCode.ReadData, socketException);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception exc)
            {
                throw new PlcException(ErrorCode.ReadData, exc);
            }
            return(dataItems);
        }