/// <summary> /// Initializes a new instance of BulkParElement. /// </summary> /// <param name="parameterId">Device Parameter ID.</param> /// <param name="instance">Specifies the type of the value to be read.</param> /// <param name="type">Will contain the read value.</param> /// <param name="parent">This value is not used by the BulkParRead. /// It may be used after reading the parameters to evaluate the result by the end user code.</param> public BulkParElement(UInt16 parameterId, byte instance, MeParType type, object parent = null) { ParameterId = parameterId; Instance = instance; Type = type; Parent = parent; }
/// <summary> /// Returns a FLOAT32, INT32 or DOUBLE64 value from the device. /// </summary> /// <param name="address">Device Address. Use null to use the DefaultDeviceAddress defined on MeComQuerySet.</param> /// <param name="parameterId">Device Parameter ID.</param> /// <param name="instance">Parameter Instance. (usually 1)</param> /// <param name="type">Specifies the type of the value to be read.</param> /// <returns>Returned value.</returns> /// <exception cref="ComCommandException">when the command fails. Check the inner exception for details.</exception> public dynamic GetValue(byte?address, UInt16 parameterId, byte instance, MeParType type) { try { MeComPacket txFrame = new MeComPacket('#', address); MeComVarConvert.AddString(txFrame.Payload, "?VR"); MeComVarConvert.AddUint16(txFrame.Payload, parameterId); MeComVarConvert.AddUint8(txFrame.Payload, instance); MeComPacket rxFrame = meQuerySet.Query(txFrame); switch (type) { case MeParType.FLOAT32: return(MeComVarConvert.ReadFloat32(rxFrame.Payload)); case MeParType.INT32: return(MeComVarConvert.ReadInt32(rxFrame.Payload)); case MeParType.DOUBLE64: return(MeComVarConvert.ReadDouble64(rxFrame.Payload)); default: throw new ArgumentOutOfRangeException("Unknown Type. Received value: " + type); } } catch (Exception Ex) { throw new ComCommandException(String.Format("Get Value failed: Address: {0}; ID: {1}; Inst: {2}; Detail: {3}", address, parameterId, instance, Ex.Message), Ex); } }
/// <summary> /// Returns the byte length of the type. /// </summary> /// <param name="Type">Value Type.</param> /// <returns>length of the given type.</returns> public byte GetTypeLength(MeParType Type) { switch (Type) { case MeParType.FLOAT32: return(4); case MeParType.INT32: return(4); case MeParType.DOUBLE64: return(8); case MeParType.LATIN1: return(1); default: throw new ArgumentOutOfRangeException("Get Type Length: Unknown EParType: " + Type); } }
/// <summary> /// Writes an array of FLOAT32, INT32, DOUBLE64 or ASCII (char) values to the device. /// The data length is given by the value parameter. /// This method does only return when all data is downloaded. /// The data is being written in an loop with several sub queries. /// During this command is working, it is possible to use other commands with an different thread. /// </summary> /// <param name="address">Device Address. Use null to use the DefaultDeviceAddress defined on MeComQuerySet.</param> /// <param name="parameterId">Device Parameter ID.</param> /// <param name="instance">Parameter Instance. (usually 1)</param> /// <param name="type">Specifies the type of the value to be written.</param> /// <param name="values">Data to be written (can be float[], int[], double[] or string.</param> /// <param name="callback">Is called every time when the progress has changed.</param> /// <exception cref="ComCommandException">when the command fails. Check the inner exception for details.</exception> public void SetBigData(byte?address, UInt16 parameterId, byte instance, MeParType type, dynamic values, ProgressUpdateCallback callback) { int maxDataSize = meQuerySet.MaxTxPayloadSize - 22; //-xx Bytes used for commands int nrOfElementsPerPackage = 0; switch (type) { case MeParType.FLOAT32: nrOfElementsPerPackage = maxDataSize / 8; break; case MeParType.INT32: nrOfElementsPerPackage = maxDataSize / 8; break; case MeParType.DOUBLE64: nrOfElementsPerPackage = maxDataSize / 16; break; case MeParType.LATIN1: values += (char)0; //Add zero terminator nrOfElementsPerPackage = maxDataSize / 2; break; case MeParType.BYTE: nrOfElementsPerPackage = maxDataSize / 2; break; default: throw new ArgumentOutOfRangeException("Unknown EParType: " + type); } int nrOfPackages = (values.Length - 1) / nrOfElementsPerPackage + 1; try { int totalSentElements = 0; MemoryStream totalStream = new MemoryStream(); for (int packageNr = 0; packageNr < nrOfPackages; packageNr++) { bool lastPackage = (packageNr + 1) == nrOfPackages; int nrOfElementsInThisPackage = values.Length - totalSentElements; if (nrOfElementsInThisPackage > nrOfElementsPerPackage) { nrOfElementsInThisPackage = nrOfElementsPerPackage; } MeComPacket txFrame = new MeComPacket('#', address); MeComVarConvert.AddString(txFrame.Payload, "VB"); MeComVarConvert.AddUint16(txFrame.Payload, parameterId); MeComVarConvert.AddUint8(txFrame.Payload, instance); MeComVarConvert.AddUint32(txFrame.Payload, (uint)totalSentElements); //write start position MeComVarConvert.AddUint16(txFrame.Payload, (ushort)nrOfElementsInThisPackage); if (lastPackage) { MeComVarConvert.AddUint8(txFrame.Payload, 1); } else { MeComVarConvert.AddUint8(txFrame.Payload, 0); } switch (type) { case MeParType.FLOAT32: for (int i = 0; i < nrOfElementsInThisPackage; i++) { MeComVarConvert.AddFloat32(txFrame.Payload, values[totalSentElements + i]); } break; case MeParType.INT32: for (int i = 0; i < nrOfElementsInThisPackage; i++) { MeComVarConvert.AddUint32(txFrame.Payload, values[totalSentElements + i]); } break; case MeParType.DOUBLE64: for (int i = 0; i < nrOfElementsInThisPackage; i++) { MeComVarConvert.AddDouble64(txFrame.Payload, values[totalSentElements + i]); } break; case MeParType.LATIN1: MeComVarConvert.AddEncodedString(txFrame.Payload, values.Substring(totalSentElements, nrOfElementsInThisPackage)); break; case MeParType.BYTE: for (int i = 0; i < nrOfElementsInThisPackage; i++) { MeComVarConvert.AddUint8(txFrame.Payload, values[totalSentElements + i]); } break; } int timeout = 0; while (timeout < 50) //Manage device busy { timeout++; try { meQuerySet.Set(txFrame); break; } catch (ServerException ex) { if (ex.ServerErrorCode != 2) { throw; } TraceLog.Verbose("Device busy detected. Timeout {0}", timeout); Thread.Sleep(10); } } totalSentElements += nrOfElementsInThisPackage; callback?.Invoke(100.0 / nrOfPackages * (packageNr + 1)); } } catch (Exception Ex) { throw new ComCommandException(String.Format("Set Value failed: Address: {0}; ID: {1}; Inst: {2}; Detail: {3}", address, parameterId, instance, Ex.Message), Ex); } }
/// <summary> /// Reads an array of FLOAT32, INT32, DOUBLE64 or ASCII (char) values form the device. /// The data length is given by the device. /// This method does only return when the device tells the host, that all data is read. /// The data is read in an loop with several sub queries. /// During this command is working, it is possible to use other commands with an different thread. /// </summary> /// <param name="address">Device Address. Use null to use the DefaultDeviceAddress defined on MeComQuerySet.</param> /// <param name="parameterId">Device Parameter ID.</param> /// <param name="instance">Parameter Instance. (usually 1)</param> /// <param name="type">Specifies the type of the value to be read.</param> /// <param name="callback">Is called every time when the progress has changed.</param> /// <param name="expectedNrOfElements">Defines the expected number of elements to calculate the progress for the callback function.</param> /// <returns>Returned value.</returns> /// <exception cref="ComCommandException">when the command fails. Check the inner exception for details.</exception> public dynamic GetBigData(byte?address, UInt16 parameterId, byte instance, MeParType type, ProgressUpdateCallback callback = null, int expectedNrOfElements = 0) { dynamic value; try { ushort rcvElements; bool hasMoreData; uint totalReadElements = 0; MemoryStream totalStream = new MemoryStream(); do { MeComPacket txFrame = new MeComPacket('#', address); MeComVarConvert.AddString(txFrame.Payload, "?VB"); MeComVarConvert.AddUint16(txFrame.Payload, parameterId); MeComVarConvert.AddUint8(txFrame.Payload, instance); MeComVarConvert.AddUint32(txFrame.Payload, totalReadElements); //Read start position MeComVarConvert.AddUint16(txFrame.Payload, UInt16.MaxValue); //Maximum Elements to read per call. MeComPacket rxFrame = meQuerySet.Query(txFrame); rcvElements = MeComVarConvert.ReadUint16(rxFrame.Payload); hasMoreData = MeComVarConvert.ReadUint8(rxFrame.Payload) == 1; totalReadElements += rcvElements; if (rcvElements > 0) { rxFrame.Payload.CopyTo(totalStream); } callback?.Invoke(100.0 / expectedNrOfElements * totalReadElements); } while (hasMoreData); totalStream.Position = 0; callback?.Invoke(100); switch (type) { case MeParType.FLOAT32: value = new float[totalReadElements]; break; case MeParType.INT32: value = new int[totalReadElements]; break; case MeParType.DOUBLE64: value = new double[totalReadElements]; break; case MeParType.LATIN1: value = ""; break; case MeParType.BYTE: value = new byte[totalReadElements]; break; default: throw new ArgumentOutOfRangeException("Unknown EParType: " + type); } for (int i = 0; i < totalReadElements; i++) { switch (type) { case MeParType.FLOAT32: value[i] = MeComVarConvert.ReadFloat32(totalStream); break; case MeParType.INT32: value[i] = MeComVarConvert.ReadInt32(totalStream); break; case MeParType.DOUBLE64: value[i] = MeComVarConvert.ReadDouble64(totalStream); break; case MeParType.LATIN1: value = MeComVarConvert.ReadEncodedString(totalStream, (int)totalReadElements); return(value); case MeParType.BYTE: value[i] = MeComVarConvert.ReadUint8(totalStream); break; default: throw new ArgumentOutOfRangeException("Unknown EParType: " + type); } } return(value); } catch (Exception Ex) { throw new ComCommandException(String.Format("Get Value failed: Address: {0}; ID: {1}; Inst: {2}; Detail: {3}", address, parameterId, instance, Ex.Message), Ex); } }