private void OnSingleRegWrite(object sender, EventArgs e) { byte[] requestArr; byte[] responseArr; string errorMess; int code = 0; var con = DataControlViewModel.GetMode(); try { //0-ushort,1-uint,2-ulong,3-hex,4-bin,5-float,6-double,7-coil if (con == 0) { code = ModBusDriver.SetDataSingle(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, Convert.ToUInt16(DataControlViewModel.EditRegister.NumericData), out errorMess); } else if (con == 4) { ushort reg = 0; foreach (var val in DataControlViewModel.EditRegister.BinData) { reg += (ushort)((val.Value ? 1 : 0) * Math.Pow(2, val.Index)); } code = ModBusDriver.SetDataSingle(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, reg, out errorMess); } else if (con == 3) { ushort reg; if (!UInt16.TryParse(DataControlViewModel.EditRegister.NumericData, System.Globalization.NumberStyles.HexNumber, null, out reg)) { WriteStatusString = "Неправильный формат значения..."; return; } code = ModBusDriver.SetDataSingle(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, reg, out errorMess); } else { code = ModBusDriver.SetDataSingle(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, (ushort)(DataControlViewModel.EditRegister.CoilData?1:0), out errorMess); } } catch (FormatException ex) { WriteStatusString = "Введите корректное значение (возможно, в качестве разделителя использована точка вместо запятой"; return; } catch (InvalidOperationException ex) { WriteStatusString = "Порт недоступен"; return; } catch (Exception ex) { WriteStatusString = "Неизвестная ошибка"; return; } string rq = "||WR|| "; string rs = "||WR|| "; if (requestArr != null) { foreach (var tmp in requestArr) { rq += tmp.ToString("X2"); rq += " "; } } if (responseArr != null) { foreach (var tmp in responseArr) { rs += tmp.ToString("X2"); rs += " "; } } DataTab.Insert(0, new ModbusDataPoint() { ErrorLevel = code, ErrorMessage = errorMess, Index = Index, RequestData = rq, ResponseData = rs }); var writeData = new MBRequestData(); writeData.ErrorMessage = errorMess; writeData.RequestQuantity = WriteData.RequestQuantity + 1; writeData.ValidResponseQuantity = code == 0 ? WriteData.ValidResponseQuantity + 1 : WriteData.ValidResponseQuantity; writeData.ValidResponseProportion = (double)writeData.ValidResponseQuantity / writeData.RequestQuantity * 100; WriteData = writeData; WriteStatusString = errorMess; Index++; }
private void OnMultipleWrite(object sender, EventArgs e) { byte[] requestArr; byte[] responseArr; string errorMess; int code = 0; var con = DataControlViewModel.GetMode(); try { //0-ushort,1-uint,2-ulong,3-hex,4-bin,5-float,6-double,7-coil switch (con) { case 0: code = ModBusDriver.SetDataMultiple(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, new[] { Convert.ToUInt16(DataControlViewModel.EditRegister.NumericData) }, null, out errorMess); break; case 1: byte[] bytes = BitConverter.GetBytes(Convert.ToUInt32(DataControlViewModel.EditRegister.NumericData)); ushort[] regs = { BitConverter.ToUInt16(bytes, 2), BitConverter.ToUInt16(bytes, 0) }; code = ModBusDriver.SetDataMultiple(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, regs, null, out errorMess); break; case 2: bytes = BitConverter.GetBytes(Convert.ToUInt64(DataControlViewModel.EditRegister.NumericData)); regs = new[] { BitConverter.ToUInt16(bytes, 6), BitConverter.ToUInt16(bytes, 4), BitConverter.ToUInt16(bytes, 2), BitConverter.ToUInt16(bytes, 0) }; code = ModBusDriver.SetDataMultiple(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, regs, null, out errorMess); break; case 3: ushort reg; if (!UInt16.TryParse(DataControlViewModel.EditRegister.NumericData, NumberStyles.HexNumber, null, out reg)) { WriteStatusString = "Неправильный формат значения..."; return; } code = ModBusDriver.SetDataMultiple(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, new[] { reg }, null, out errorMess); break; case 4: reg = 0; foreach (var val in DataControlViewModel.EditRegister.BinData) { reg += (ushort)((val.Value ? 1 : 0) * Math.Pow(2, val.Index)); } code = ModBusDriver.SetDataSingle(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, reg, out errorMess); break; case 5: bytes = BitConverter.GetBytes(Convert.ToSingle(DataControlViewModel.EditRegister.NumericData)); regs = new[] { BitConverter.ToUInt16(bytes, 2), BitConverter.ToUInt16(bytes, 0) }; code = ModBusDriver.SetDataMultiple(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, regs, null, out errorMess); break; case 6: var doubleVal = Convert.ToDouble(DataControlViewModel.EditRegister.NumericData); bytes = BitConverter.GetBytes(doubleVal); regs = new[] { BitConverter.ToUInt16(bytes, 0), BitConverter.ToUInt16(bytes, 2), BitConverter.ToUInt16(bytes, 4), BitConverter.ToUInt16(bytes, 6) }; code = ModBusDriver.SetDataMultiple(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, regs, null, out errorMess); break; case 7: code = ModBusDriver.SetDataMultiple(out requestArr, out responseArr, DataControlViewModel.EditRegister.Address, new ushort[] { (ushort)(DataControlViewModel.EditRegister.CoilData ? 1 : 0) }, null, out errorMess); break; default: requestArr = new byte[1]; responseArr = new byte[1]; errorMess = "Unreconized condition"; break; } } catch (FormatException ex) { WriteStatusString = "Введите корректное значение (возможно, в качестве разделителя использована точка вместо запятой"; return; } catch (InvalidOperationException ex) { WriteStatusString = "Порт недоступен"; return; } catch (Exception ex) { WriteStatusString = "Неизвестная ошибка"; return; } string rq = "||WR|| "; string rs = "||WR|| "; if (requestArr != null) { foreach (var tmp in requestArr) { rq += tmp.ToString("X2"); rq += " "; } } if (responseArr != null) { foreach (var tmp in responseArr) { rs += tmp.ToString("X2"); rs += " "; } } DataTab.Insert(0, new ModbusDataPoint() { ErrorLevel = code, ErrorMessage = errorMess, Index = Index, RequestData = rq, ResponseData = rs }); var writeData = new MBRequestData(); writeData.ErrorMessage = errorMess; writeData.RequestQuantity = WriteData.RequestQuantity + 1; writeData.ValidResponseQuantity = code == 0 ? WriteData.ValidResponseQuantity + 1 : WriteData.ValidResponseQuantity; writeData.ValidResponseProportion = (double)writeData.ValidResponseQuantity / writeData.RequestQuantity * 100; WriteData = writeData; WriteStatusString = errorMess; Index++; }