Modbus ASCII Fix

This commit is contained in:
parallelbgls
2016-10-02 18:33:46 +08:00
parent fc693ebd8a
commit 9c894ae5fe
4 changed files with 25 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Text; using System.Text;
namespace Modbus.Net.Modbus namespace Modbus.Net.Modbus
@@ -60,7 +61,7 @@ namespace Modbus.Net.Modbus
newContent.AddRange(Encoding.ASCII.GetBytes(":")); newContent.AddRange(Encoding.ASCII.GetBytes(":"));
foreach (var number in content) 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.AddRange(Encoding.ASCII.GetBytes(Crc16.GetInstance().GetLRC(content)));
newContent.Add(0x0d); newContent.Add(0x0d);
@@ -76,7 +77,7 @@ namespace Modbus.Net.Modbus
ans = ans.Substring(1, index - 1); ans = ans.Substring(1, index - 1);
for (int i = 0; i < ans.Length; i += 2) 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.Add(number);
} }
newContent.RemoveAt(newContent.Count-1); newContent.RemoveAt(newContent.Count-1);

View File

@@ -95,8 +95,8 @@ namespace Modbus.Net
public bool LrcEfficacy(string message) public bool LrcEfficacy(string message)
{ {
var index = message.IndexOf(Environment.NewLine, StringComparison.InvariantCulture); var index = message.IndexOf(Environment.NewLine, StringComparison.InvariantCulture);
var writeUncheck = message.Substring(1, index - 3); var writeUncheck = message.Substring(1, index - 2);
var checkString = message.Substring(index - 3, 2); var checkString = message.Substring(index - 2, 2);
char[] hexArray = new char[writeUncheck.Length]; char[] hexArray = new char[writeUncheck.Length];
hexArray = writeUncheck.ToCharArray(); hexArray = writeUncheck.ToCharArray();
int decNum = 0, decNumMSB = 0, decNumLSB = 0; int decNum = 0, decNumMSB = 0, decNumLSB = 0;
@@ -176,14 +176,13 @@ namespace Modbus.Net
public string GetLRC(byte[] code) public string GetLRC(byte[] code)
{ {
int sum = 0; byte sum = 0;
foreach (byte b in code) foreach (byte b in code)
{ {
sum += b; sum += b;
} }
sum = sum % 255;//取模FF(255) sum = (byte)(~sum + 1);//取反+1
sum = ~sum + 1;//取反+1 string lrc = sum.ToString("X2");
string lrc = Convert.ToString(sum, 16);
return lrc; return lrc;
} }
} }

View File

@@ -81,7 +81,7 @@ namespace Modbus.Net
SerialPort1.Open(); SerialPort1.Open();
return true; return true;
} }
catch catch (Exception e)
{ {
return false; return false;
} }

View File

@@ -46,26 +46,26 @@ namespace NA200H.UI.WPF
{ {
if (machine == null) if (machine == null)
{ {
//machine = new ModbusMachine(ModbusType.Tcp, "192.168.3.12", new List<AddressUnit>() machine = new ModbusMachine(ModbusType.Ascii, "COM3", new List<AddressUnit>()
//{ {
//new AddressUnit() {Id = "1", Area = "MW", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, new AddressUnit() {Id = "1", Area = "4X", 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 = "2", Area = "4X", 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 = "3", Area = "4X", 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}, new AddressUnit() {Id = "4", Area = "4X", Address = 4, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0},
//}, 2, 0); }, 2, 0);
//machine.AddressFormater = new AddressFormaterNA200H(); //machine.AddressFormater = new AddressFormaterNA200H();
//machine.AddressTranslator = new AddressTranslatorNA200H(); //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<AddressUnit>()
{
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.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator);
machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator); machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator);
//machine = new SiemensMachine(SiemensType.Tcp, "192.168.3.11", SiemensMachineModel.S7_300, new List<AddressUnit>()
//{
//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 result = machine.GetDatas(MachineGetDataType.CommunicationTag);
var resultFormat = BaseMachine.MapGetValuesToSetValues(result); var resultFormat = BaseMachine.MapGetValuesToSetValues(result);