public async Task <bool> EnqueueReadCommand(IReadModbusFunction modbusFunction) { string verboseMessage = $"{baseLogString} entering EnqueueReadCommand, FunctionCode: {modbusFunction.FunctionCode}, StartAddress: {modbusFunction.StartAddress}, Quantity: {modbusFunction.Quantity}."; Logger.LogVerbose(verboseMessage); bool success; while (!ReliableQueuesInitialized) { await Task.Delay(1000); } try { if (!(modbusFunction is IReadModbusFunction readModbusFunction)) { string message = $"{baseLogString} EnqueueReadCommand => trying to enqueue modbus function that does not implement IReadModbusFunction interface."; Logger.LogError(message); throw new ArgumentException(message); } var modelUpdatePeakResult = (await modelUpdateCommandQueue.GetCountAsync()) > 0; var writePeakResult = (await writeCommandQueue.GetCountAsync()) > 0; if (modelUpdatePeakResult || writePeakResult) { if (modelUpdatePeakResult) { verboseMessage = $"{baseLogString} EnqueueReadCommand => '{CloudStorageQueueNames.ModelUpdateCommandQueue}' queue to is not empty. "; Logger.LogDebug(verboseMessage); } if (writePeakResult) { verboseMessage = $"{baseLogString} EnqueueReadCommand => '{CloudStorageQueueNames.WriteCommandQueue}' queue to is not empty."; Logger.LogDebug(verboseMessage); } return(false); } await this.readCommandQueue.EnqueueAsync((ModbusFunction)modbusFunction); success = true; verboseMessage = $"{baseLogString} EnqueueReadCommand => read command SUCCESSFULLY enqueued to '{CloudStorageQueueNames.ReadCommandQueue}' queue. FunctionCode: {readModbusFunction.FunctionCode}, StartAddress: {readModbusFunction.StartAddress}, Quantity: {readModbusFunction.Quantity}"; Logger.LogVerbose(verboseMessage); } catch (Exception e) { success = false; string errorMessage = $"{baseLogString} EnqueueReadCommand => Exception caught: {e.Message}."; Logger.LogError(errorMessage, e); } return(success); }
private async Task ExecuteReadCommand(IReadModbusFunction readCommand) { string verboseMessage = $"{baseLogString} entering ExecuteReadCommand method, FunctionCode: {readCommand.FunctionCode}, StartAddress: {readCommand.StartAddress}, Quantity: {readCommand.Quantity}."; Logger.LogVerbose(verboseMessage); ModbusFunctionCode functionCode = readCommand.FunctionCode; ushort startAddress = readCommand.StartAddress; ushort quantity = readCommand.Quantity; if (quantity <= 0) { string message = $"{baseLogString} ExecuteReadCommand => Reading Quantity: {quantity} does not make sense."; Logger.LogError(message); throw new ArgumentException(message); } if (startAddress + quantity >= ushort.MaxValue || startAddress + quantity == ushort.MinValue || startAddress == ushort.MinValue) { string message = $"{baseLogString} ExecuteReadCommand => Address is out of bound. Start address: {startAddress}, Quantity: {quantity}"; Logger.LogError(message); throw new ArgumentException(message); } if (functionCode == ModbusFunctionCode.READ_COILS || functionCode == ModbusFunctionCode.READ_DISCRETE_INPUTS) { verboseMessage = $"{baseLogString} ExecuteReadCommand => ExecuteDiscreteReadCommand about to be called."; Logger.LogVerbose(verboseMessage); //LOGIC await ExecuteDiscreteReadCommand(functionCode, startAddress, quantity); } else if (functionCode == ModbusFunctionCode.READ_HOLDING_REGISTERS || functionCode == ModbusFunctionCode.READ_INPUT_REGISTERS) { verboseMessage = $"{baseLogString} ExecuteReadCommand => ExecuteAnalogReadCommand about to be called."; Logger.LogVerbose(verboseMessage); //LOGIC await ExecuteAnalogReadCommand(functionCode, startAddress, quantity); } else { string errorMessage = $"{baseLogString} ExecuteWriteSingleCommand => function code hase value: {functionCode}, but one of these was required: {ModbusFunctionCode.READ_COILS}, {ModbusFunctionCode.READ_DISCRETE_INPUTS}, {ModbusFunctionCode.READ_HOLDING_REGISTERS}, {ModbusFunctionCode.READ_INPUT_REGISTERS}."; Logger.LogError(errorMessage); throw new ArgumentException(errorMessage); } }
public bool EnqueueReadCommand(IReadModbusFunction modbusFunction) { bool success; if (!(modbusFunction is IReadAnalogModusFunction || modbusFunction is IReadDiscreteModbusFunction)) { string message = "EnqueueReadCommand => trying to enqueue modbus function that implements neither IReadDiscreteModbusFunction nor IReadDiscreteModbusFunction interface."; Logger.LogError(message); throw new ArgumentException(message); } while (!modelUpdateQueue.IsEmpty || !writeCommandQueue.IsEmpty) { if (!modelUpdateQueue.IsEmpty) { this.modelUpdateQueueEmptyEvent.WaitOne(); } if (!writeCommandQueue.IsEmpty) { this.writeCommandQueueEmptyEvent.WaitOne(); } } try { this.readCommandQueue.Enqueue(modbusFunction); this.commandEvent.Set(); success = true; } catch (Exception e) { success = false; string message = "Exception caught in EnqueueCommand() method."; Logger.LogError(message, e); } return(success); }
private bool TryCreateModbusFunction(KeyValuePair <short, Dictionary <ushort, long> > addressToGidMapKvp, out IReadModbusFunction modbusFunction) { string verboseMessage = $"{baseLogString} entering TryCreateModbusFunction method => addressToGidMapKvp(key: {addressToGidMapKvp.Key}, value count: {addressToGidMapKvp.Value.Count})."; Logger.LogVerbose(verboseMessage); modbusFunction = null; PointType pointType = (PointType)addressToGidMapKvp.Key; Dictionary <ushort, long> addressToGidMap = addressToGidMapKvp.Value; ModbusFunctionCode functionCode; try { functionCode = MapPointTypeToModbusFunctionCode(pointType); verboseMessage = $"{baseLogString} TryCreateModbusFunction => function code mapped: {functionCode}."; Logger.LogVerbose(verboseMessage); } catch (ArgumentException ae) { Logger.LogVerbose(ae.Message); //recomended to be verbose, becouse Acquisition happens very often return(false); } ushort startAddress = 1; ushort quantity = (ushort)addressToGidMap.Count; if (quantity == 0) { return(false); } modbusFunction = new ReadFunction(functionCode, startAddress, quantity); verboseMessage = $"{baseLogString} TryCreateModbusFunction => ReadFunction with code: {modbusFunction.FunctionCode}, stratring address: {modbusFunction.StartAddress} and quantity: {modbusFunction.Quantity} SUCCESSFULLY created."; Logger.LogVerbose(verboseMessage); return(true); }
public Task <bool> EnqueueReadCommand(IReadModbusFunction modbusFunctions) { //return MethodWrapperAsync<bool>("EnqueueReadCommand", new object[1] { modbusFunctions }); return(InvokeWithRetryAsync(client => client.Channel.EnqueueReadCommand(modbusFunctions))); }