diff --git a/Modbus.Net/ModBus.Net/SimenseProtocal.cs b/Modbus.Net/ModBus.Net/SimenseProtocal.cs index fae46cc..35654c2 100644 --- a/Modbus.Net/ModBus.Net/SimenseProtocal.cs +++ b/Modbus.Net/ModBus.Net/SimenseProtocal.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Eventing; using System.Linq; using System.Reflection.Emit; using System.Windows.Forms; @@ -354,138 +355,67 @@ namespace ModBus.Net public class ReadTimeSimenseOutputStruct : OutputStruct { - public ReadTimeSimenseOutputStruct(ushort pduRef, DateTime dateTime) + public ReadTimeSimenseOutputStruct(ushort pduRef, DateTime dateTime, TodClockStatus todClockStatus) { PduRef = pduRef; DateTime = dateTime; + TodClockStatus = todClockStatus; } public ushort PduRef { get; private set; } public DateTime DateTime { get; private set; } + public TodClockStatus TodClockStatus { get; private set; } } public class ReadTimeSimenseProtocal : ProtocalUnit { public override byte[] Format(InputStruct message) { - var r_message = (ReadTimeSimenseInputStruct) message; - const byte protoId = 0x32; - const byte rosctr = 0x07; - const ushort redId = 0x0000; - ushort pduRef = r_message.PduRef; - const ushort parLg = 8; - const ushort datLg = 4; - const byte serviceId = 0x00; - const byte noVar = 0x01; - const byte varSpc = 0x12; - const byte vAddrLg = 0x04; - const byte synId = 0x11; - const byte classP = 0x47; - const byte id1 = 0x01; - const byte id2 = 0x00; - const byte accRslt = 0x0A; - const byte dType = 0x00; - const ushort length = 0x0000; - return Format(new Byte[7], protoId, rosctr, redId, pduRef, parLg, datLg, serviceId, noVar, varSpc, vAddrLg, synId, classP, - id1, id2, accRslt, dType, length); + throw new NotImplementedException(); } public override OutputStruct Unformat(byte[] messageBytes, ref int pos) { - pos = 4; - ushort pduRef = ValueHelper.Instance.GetUShort(messageBytes, ref pos); - pos = 28; - byte year1 = ValueHelper.Instance.GetByte(messageBytes, ref pos); - byte month1 = ValueHelper.Instance.GetByte(messageBytes, ref pos); - byte day1 = ValueHelper.Instance.GetByte(messageBytes, ref pos); - byte hour1 = ValueHelper.Instance.GetByte(messageBytes, ref pos); - byte minute1 = ValueHelper.Instance.GetByte(messageBytes, ref pos); - byte second1 = ValueHelper.Instance.GetByte(messageBytes, ref pos); - byte second1_10_100 = ValueHelper.Instance.GetByte(messageBytes, ref pos); - byte second1_1000_weekday = ValueHelper.Instance.GetByte(messageBytes, ref pos); - int year = year1/16*10 + year1%16; - int month = month1/16*10 + month1%16; - int day = day1/16*10 + day1%16; - int hour = hour1/16*10 + hour1%16; - int minute = minute1/16*10 + minute1%16; - int second = second1/16*10 + second1%16; - int millisecond = second1_10_100 / 16 * 100 + second1_10_100 % 16 * 10 + second1_1000_weekday / 16; - int weekday = second1_1000_weekday%16; - DateTime dateTime = new DateTime(DateTime.Now.Year/100*100 + year, month, day, hour, minute, second, millisecond); - if (dateTime > DateTime.Now.AddDays(1)) dateTime = dateTime.AddYears(-100); - if (weekday == 0) return new ReadTimeSimenseOutputStruct(pduRef, dateTime); - while (dateTime.DayOfWeek != (DayOfWeek) (weekday - 1)) dateTime = dateTime.AddYears(-100); - return new ReadTimeSimenseOutputStruct(pduRef, dateTime); + throw new NotImplementedException(); } } public class WriteTimeSimenseInputStruct : InputStruct { - public WriteTimeSimenseInputStruct(ushort pduRef, DateTime dateTime) + public WriteTimeSimenseInputStruct(ushort pduRef, DateTime dateTime, TodClockStatus todClockStatus) { PduRef = pduRef; DateTime = dateTime; + TodClockStatus = todClockStatus; } public ushort PduRef { get; private set; } public DateTime DateTime { get; private set; } + public TodClockStatus TodClockStatus { get; private set; } } public class WriteTimeSimenseOutputStruct : OutputStruct { - public WriteTimeSimenseOutputStruct(ushort pduRef, byte id2) + public WriteTimeSimenseOutputStruct(ushort pduRef, byte errCod) { PduRef = pduRef; - Id2 = id2; + ErrCod = errCod; } public ushort PduRef { get; private set; } - - public byte Id2 { get; private set; } + public byte ErrCod { get;private set; } } public class WriteTimeSimenseProtocal : ProtocalUnit { public override byte[] Format(InputStruct message) { - var r_message = (WriteTimeSimenseInputStruct) message; - const byte protoId = 0x32; - const byte rosctr = 0x07; - const ushort redId = 0x0000; - ushort pduRef = r_message.PduRef; - const ushort parLg = 0x0008; - const ushort datLg = 0x000e; - const byte serviceId = 0x00; - const byte noVar = 0x01; - const byte varSpc = 0x12; - const byte vAddrLg = 0x04; - const byte synId = 0x11; - const byte classP = 0x47; - const byte id1 = 0x02; - const byte id2 = 0x00; - const byte accRslt = 0xFF; - const byte dType = 0x09; - const ushort length = 0x000A; - const ushort todClockStatus = 0x0018; - byte year = (byte) (r_message.DateTime.Year%100/10*16 + r_message.DateTime.Year%10); - byte month = (byte) (r_message.DateTime.Month/10*16 + r_message.DateTime.Month%10); - byte day = (byte) (r_message.DateTime.Day/10*16 + r_message.DateTime.Day%10); - byte hour = (byte) (r_message.DateTime.Hour/10*16 + r_message.DateTime.Hour%10); - byte minute = (byte) (r_message.DateTime.Minute/10*16 + r_message.DateTime.Minute%10); - byte second = (byte) (r_message.DateTime.Second/10*16 + r_message.DateTime.Second%10); - byte dayOfWeek = (byte) (r_message.DateTime.DayOfWeek + 1); - return Format(new byte[7], protoId, rosctr, redId, pduRef, parLg, datLg, serviceId, noVar, varSpc, vAddrLg, - synId, classP, id1, id2, accRslt, dType, length, todClockStatus, year, month, day, hour, minute, second, - (byte)0, dayOfWeek); + throw new NotImplementedException(); } public override OutputStruct Unformat(byte[] messageBytes, ref int pos) { - pos = 4; - ushort pduRef = ValueHelper.Instance.GetUShort(messageBytes, ref pos); - pos = 17; - byte id2 = ValueHelper.Instance.GetByte(messageBytes, ref pos); - return new WriteTimeSimenseOutputStruct(pduRef, id2); + throw new NotImplementedException(); } } diff --git a/Modbus.Net/ModBus.Net/SimenseTcpProtocal.cs b/Modbus.Net/ModBus.Net/SimenseTcpProtocal.cs index 3c5033b..7d10a20 100644 --- a/Modbus.Net/ModBus.Net/SimenseTcpProtocal.cs +++ b/Modbus.Net/ModBus.Net/SimenseTcpProtocal.cs @@ -13,20 +13,23 @@ namespace ModBus.Net private ushort _maxCalling; private ushort _maxCalled; private ushort _maxPdu; + private byte _tdpuSize; + private string _ip; private int connectTryCount; - public SimenseTcpProtocal(ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, ushort maxPdu) : this(tsapSrc, tsapDst, maxCalling, maxCalled, maxPdu, ConfigurationManager.IP) + public SimenseTcpProtocal(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, ushort maxPdu) : this(tdpuSize, tsapSrc, tsapDst, maxCalling, maxCalled, maxPdu, ConfigurationManager.IP) { } - public SimenseTcpProtocal(ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, ushort maxPdu, string ip) + public SimenseTcpProtocal(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled, ushort maxPdu, string ip) { _taspSrc = tsapSrc; _tsapDst = tsapDst; _maxCalling = maxCalling; _maxCalled = maxCalled; _maxPdu = maxPdu; + _tdpuSize = tdpuSize; _ip = ip; connectTryCount = 0; Connected(); @@ -60,7 +63,7 @@ namespace ModBus.Net { connectTryCount++; ProtocalLinker = new SimenseTcpProtocalLinker(_ip); - var inputStruct = new CreateReferenceSimenseInputStruct(0x1a, _taspSrc, _tsapDst); + var inputStruct = new CreateReferenceSimenseInputStruct(_tdpuSize, _taspSrc, _tsapDst); var outputStruct = (CreateReferenceSimenseOutputStruct)ForceSendReceive(this[typeof(CreateReferenceSimenseProtocal)], inputStruct); if (!ProtocalLinker.IsConnected) return; diff --git a/Modbus.Net/ModBus.Net/SimenseUtility.cs b/Modbus.Net/ModBus.Net/SimenseUtility.cs index dcb612e..4207319 100644 --- a/Modbus.Net/ModBus.Net/SimenseUtility.cs +++ b/Modbus.Net/ModBus.Net/SimenseUtility.cs @@ -27,21 +27,23 @@ namespace ModBus.Net { case "200": { + _tdpuSize = 0x09; _taspSrc = 0x4d57; _tsapDst = 0x4d57; _maxCalling = 0x0001; _maxCalled = 0x0001; - _maxPdu = 0x03c0; + _maxPdu = 0x03c0; break; } case "300": case "400": { + _tdpuSize = 0x1a; _taspSrc = 0x4b54; _tsapDst = 0x0302; _maxCalling = 0x0001; _maxCalled = 0x0001; - _maxPdu = 0x00f0; + _maxPdu = 0x00f0; break; } } @@ -49,12 +51,13 @@ namespace ModBus.Net } } + private byte _tdpuSize; private ushort _taspSrc; private ushort _tsapDst; private ushort _maxCalling; private ushort _maxCalled; private ushort _maxPdu; - + private SimenseType _simenseType; public SimenseType ConnectionType @@ -78,7 +81,7 @@ namespace ModBus.Net } case SimenseType.Tcp: { - Wrapper = ConnectionString == null ? new SimenseTcpProtocal(_taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu) : new SimenseTcpProtocal(_taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu, ConnectionString); + Wrapper = ConnectionString == null ? new SimenseTcpProtocal(_tdpuSize, _taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu) : new SimenseTcpProtocal(_tdpuSize, _taspSrc, _tsapDst, _maxCalling, _maxCalled, _maxPdu, ConnectionString); break; } } diff --git a/Modbus.Net/ModBus.Net/TcpConnector.cs b/Modbus.Net/ModBus.Net/TcpConnector.cs index 22cf8a9..794f44b 100644 --- a/Modbus.Net/ModBus.Net/TcpConnector.cs +++ b/Modbus.Net/ModBus.Net/TcpConnector.cs @@ -45,7 +45,7 @@ namespace ModBus.Net { _host = ipaddress; _port = port; - //TimeoutTime = timeoutTime; + TimeoutTime = timeoutTime; } public override bool IsConnected diff --git a/Modbus.Net/ModBus.Net/ValueHelper.cs b/Modbus.Net/ModBus.Net/ValueHelper.cs index cd03000..8bdf33a 100644 --- a/Modbus.Net/ModBus.Net/ValueHelper.cs +++ b/Modbus.Net/ModBus.Net/ValueHelper.cs @@ -25,14 +25,14 @@ namespace ModBus.Net {"System.Double", 8} }; - protected static bool _littleEndian = true; + protected static bool _littleEndian = false; protected ValueHelper() { } /// - /// 协议中的内容构造是否小端的,默认是大端构造协议。 + /// 协议中的内容构造是否小端的,默认是小端构造协议。 /// public static bool LittleEndian { @@ -40,8 +40,7 @@ namespace ModBus.Net set { _littleEndian = value; - //这里需要重点说明,因为.net默认是小端构造法,所以目标协议是大端的话反而需要调用小端构造协议,把小端反转为大端。 - _Instance = LittleEndian ? new LittleEndianValueHelper() : new ValueHelper(); + _Instance = LittleEndian ? new ValueHelper() : new BigEndianValueHelper(); } } @@ -58,7 +57,7 @@ namespace ModBus.Net { if (_Instance == null) { - _Instance = LittleEndian ? new LittleEndianValueHelper() : new ValueHelper(); + _Instance = LittleEndian ? new ValueHelper() : new BigEndianValueHelper(); } return _Instance; } @@ -566,6 +565,12 @@ namespace ModBus.Net return ans > 0; } + public virtual bool GetBit(byte[] number, ref int pos) + { + var tpos = pos%8; + return GetBit(number[pos++/8], ref tpos); + } + /// /// 设置对应数字中相应位置的bit的值 /// @@ -595,9 +600,16 @@ namespace ModBus.Net return (byte) (number & creation); } } + + public virtual ushort SetBit(byte[] number, int pos, bool setBit) + { + SetBit(number[pos / 8], pos % 8, setBit); + var tpos = 0; + return GetUShort(number, ref tpos); + } } - internal class LittleEndianValueHelper : ValueHelper + internal class BigEndianValueHelper : ValueHelper { public override Byte[] GetBytes(short value) { @@ -703,6 +715,12 @@ namespace ModBus.Net return t; } + public override bool GetBit(byte[] number, ref int pos) + { + var tpos = pos % 8; + return base.GetBit(number[number.Length - 1 - (pos++ / 8)], ref tpos); + } + public override bool[] GetBits(byte[] data, ref int pos) { bool[] t = new bool[8]; @@ -716,6 +734,12 @@ namespace ModBus.Net return t; } + public override ushort SetBit(byte[] number, int pos, bool setBit) + { + Array.Reverse(number); + return base.SetBit(number, pos, setBit); + } + private Byte[] Reverse(Byte[] data) { Array.Reverse(data);