public async Task ModbusReadAsync() { if (Networking.NetworkCheck(IpAddr)) { try { ModbusTcp ModbusDevice = new ModbusTcp(502, IpAddr, NodeId); RegisterValues = await Task.Run(() => ModbusDevice.ReadAsync(StartReg, RegQty, FunctionCode)); int[] RegValInts = new List <int>(RegisterValues).ToArray(); byte[] RegValBytes = IntArray.ToByteArray(RegValInts); Response = BitConverter.ToString(RegValBytes); } catch (Exception ex) { Response = ex.Message; StopCyclicRead(); MessageBox.Show("0x" + ex.HResult.ToString("X") + ": " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { StopCyclicRead(); } }
public async Task ModbusWriteAsync() { if (Networking.NetworkCheck(IpAddr)) { try { ModbusTcp ModbusDevice = new ModbusTcp(502, IpAddr, NodeId); byte[] ModbusResponse = await Task.Run(() => ModbusDevice.WriteAsync(StartReg, RegQty, RegisterValues, FunctionCode)); Response = BitConverter.ToString(ModbusResponse); } catch (Exception ex) { Response = ex.Message; StopCyclicWrite(); MessageBox.Show($"0x{ex.HResult.ToString("X")}: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { StopCyclicWrite(); } }