private async void QueryDiscretes() { await TaskEx.Run(() => { //compose the Modbus command to be submitted var command = new ModbusCommand(ModbusCommand.FuncReadInputDiscretes); command.Offset = 0; command.Count = InputCount; //execute the command synchronously CommResponse result = App.Modbus .ExecuteGeneric(App.Client, command); if (result.Status == CommResponse.Ack) { //command successfully Deployment.Current.Dispatcher.BeginInvoke(new Action(() => { for (int i = 0; i < command.Count; i++) { var input = this._inputs[i]; input.Value = command.Data[input.Offset] == 0; } })); } else { //some error } this._status = MachineState.CoilRequest; }); }
private async void QueryCoils() { await TaskEx.Run(() => { //compose the Modbus command to be submitted var command = new ModbusCommand(ModbusCommand.FuncForceMultipleCoils); command.Offset = 0; command.Count = CoilCount; command.Data = new ushort[CoilCount]; for (int i = 0; i < CoilCount; i++) { var output = this._outputs[i]; command.Data[output.Offset] = (ushort)(output.Value ? 1 : 0); } //execute the command synchronously CommResponse result = App.Modbus .ExecuteGeneric(App.Client, command); if (result.Status == CommResponse.Ack) { //command successfully } else { //some error } this._status = MachineState.AnalogRequest; }); }