diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs index 9f0ea79..44388bd 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocalLinkerBytesExtend.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Text; namespace Modbus.Net.Modbus @@ -60,7 +61,7 @@ namespace Modbus.Net.Modbus newContent.AddRange(Encoding.ASCII.GetBytes(":")); foreach (var number in content) { - newContent.AddRange(Encoding.ASCII.GetBytes(number.ToString())); + newContent.AddRange(Encoding.ASCII.GetBytes(number.ToString("X2"))); } newContent.AddRange(Encoding.ASCII.GetBytes(Crc16.GetInstance().GetLRC(content))); newContent.Add(0x0d); @@ -76,7 +77,7 @@ namespace Modbus.Net.Modbus ans = ans.Substring(1, index - 1); for (int i = 0; i < ans.Length; i += 2) { - var number = byte.Parse(ans.Substring(i, 2)); + var number = byte.Parse(ans.Substring(i, 2), NumberStyles.HexNumber); newContent.Add(number); } newContent.RemoveAt(newContent.Count-1); diff --git a/Modbus.Net/Modbus.Net/CRC16.cs b/Modbus.Net/Modbus.Net/CRC16.cs index 1ef6e21..7fed232 100644 --- a/Modbus.Net/Modbus.Net/CRC16.cs +++ b/Modbus.Net/Modbus.Net/CRC16.cs @@ -95,8 +95,8 @@ namespace Modbus.Net public bool LrcEfficacy(string message) { var index = message.IndexOf(Environment.NewLine, StringComparison.InvariantCulture); - var writeUncheck = message.Substring(1, index - 3); - var checkString = message.Substring(index - 3, 2); + var writeUncheck = message.Substring(1, index - 2); + var checkString = message.Substring(index - 2, 2); char[] hexArray = new char[writeUncheck.Length]; hexArray = writeUncheck.ToCharArray(); int decNum = 0, decNumMSB = 0, decNumLSB = 0; @@ -176,14 +176,13 @@ namespace Modbus.Net public string GetLRC(byte[] code) { - int sum = 0; + byte sum = 0; foreach (byte b in code) { sum += b; } - sum = sum % 255;//取模FF(255) - sum = ~sum + 1;//取反+1 - string lrc = Convert.ToString(sum, 16); + sum = (byte)(~sum + 1);//取反+1 + string lrc = sum.ToString("X2"); return lrc; } } diff --git a/Modbus.Net/Modbus.Net/ComConnector.cs b/Modbus.Net/Modbus.Net/ComConnector.cs index 61504d3..bc48f10 100644 --- a/Modbus.Net/Modbus.Net/ComConnector.cs +++ b/Modbus.Net/Modbus.Net/ComConnector.cs @@ -81,7 +81,7 @@ namespace Modbus.Net SerialPort1.Open(); return true; } - catch + catch (Exception e) { return false; } diff --git a/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs b/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs index 84658d9..fbc7445 100644 --- a/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs +++ b/Modbus.Net/NA200H.UI.WPF/MainWindow.xaml.cs @@ -46,26 +46,26 @@ namespace NA200H.UI.WPF { if (machine == null) { - //machine = new ModbusMachine(ModbusType.Tcp, "192.168.3.12", new List() - //{ - //new AddressUnit() {Id = "1", Area = "MW", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - //new AddressUnit() {Id = "2", Area = "MW", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - //new AddressUnit() {Id = "3", Area = "MW", Address = 3, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - //new AddressUnit() {Id = "4", Area = "MW", Address = 4, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - //}, 2, 0); + machine = new ModbusMachine(ModbusType.Ascii, "COM3", new List() + { + new AddressUnit() {Id = "1", Area = "4X", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = "2", Area = "4X", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = "3", Area = "4X", Address = 3, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + new AddressUnit() {Id = "4", Area = "4X", Address = 4, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + }, 2, 0); //machine.AddressFormater = new AddressFormaterNA200H(); //machine.AddressTranslator = new AddressTranslatorNA200H(); - //machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator); - //machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator); - machine = new SiemensMachine(SiemensType.Tcp, "192.168.3.11", SiemensMachineModel.S7_300, new List() - { - new AddressUnit() {Id = "1", Area = "V", Address = 0, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - new AddressUnit() {Id = "2", Area = "V", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - new AddressUnit() {Id = "3", Area = "V", Address = 4, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, - new AddressUnit() {Id = "4", Area = "V", Address = 6, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0} - }, 2, 0); machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator); machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator); + //machine = new SiemensMachine(SiemensType.Tcp, "192.168.3.11", SiemensMachineModel.S7_300, new List() + //{ + //new AddressUnit() {Id = "1", Area = "V", Address = 0, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + //new AddressUnit() {Id = "2", Area = "V", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + //new AddressUnit() {Id = "3", Area = "V", Address = 4, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, + //new AddressUnit() {Id = "4", Area = "V", Address = 6, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0} + //}, 2, 0); + //machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator); + //machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator); } var result = machine.GetDatas(MachineGetDataType.CommunicationTag); var resultFormat = BaseMachine.MapGetValuesToSetValues(result);