diff --git a/SCADA/Program/CoreTest/obj/x86/Debug/CoreTest_MarkupCompile.i.lref b/SCADA/Program/CoreTest/obj/x86/Debug/CoreTest_MarkupCompile.i.lref new file mode 100644 index 0000000..5e01469 --- /dev/null +++ b/SCADA/Program/CoreTest/obj/x86/Debug/CoreTest_MarkupCompile.i.lref @@ -0,0 +1,8 @@ +C:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\CoreTest\obj\x86\Debug\GeneratedInternalTypeHelper.g.i.cs + +FC:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\CoreTest\AlarmSet.xaml;; +FC:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\CoreTest\Guage.xaml;; +FC:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\CoreTest\MainWindow.xaml;; +FC:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\CoreTest\TagMonitor.xaml;; +FC:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\CoreTest\Trend.xaml;; + diff --git a/SCADA/Program/DataService/ClientReader.cs b/SCADA/Program/DataService/ClientReader.cs deleted file mode 100644 index 1b1c7bb..0000000 --- a/SCADA/Program/DataService/ClientReader.cs +++ /dev/null @@ -1,607 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Sockets; -using System.Net; -using System.Text; -using System.Threading; - -namespace DataService -{ - public class ClientReader : IDevice - { - string _ip; - public string ServerName - { - get { return _ip; } - } - - internal Socket tcpSynCl; - internal Socket tcpASynCl; - - public bool IsClosed - { - get - { - //return tcpASynCl.Poll(-1, SelectMode.SelectRead); - return !tcpSynCl.Connected || !tcpASynCl.Connected; - } - } - - private ushort _timeout = 0; - public int TimeOut - { - get { return _timeout; } - } - - - List _grps = new List(1); - public IEnumerable Groups - { - get { return _grps; } - } - - IDataServer _server; - public IDataServer Parent - { - get { return _server; } - } - - public ClientReader(IDataServer server, string ip) - { - _server = server; - _ip = ip; - } - - public bool Connect() - { - try - { - int port = 1000; - IPAddress ip = IPAddress.Parse(_ip); - tcpASynCl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - tcpASynCl.Connect(new IPEndPoint(ip, port)); - tcpASynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _timeout); - tcpASynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout); - - tcpSynCl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - tcpSynCl.Connect(new IPEndPoint(ip, port)); - tcpSynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _timeout); - tcpSynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout); - return true; - } - catch (SocketException error) - { - if (OnClose != null) - OnClose(this, new ShutdownRequestEventArgs(error.Message)); - return false; - } - } - - public IGroup AddGroup(string name, ushort id, int updateRate, int timeOut = 0, float deadBand = 0f, bool active = false) - { - ClientGroup grp = new ClientGroup(id, name, updateRate, active, this); - _grps.Add(grp); - return grp; - } - - public int RemoveAllGroup() - { - foreach (IGroup grp in _grps) - { - grp.Dispose(); - } - _grps.Clear(); - return 1; - } - - public event ShutdownRequestEventHandler OnClose; - - public void Dispose() - { - if (tcpSynCl != null) - { - if (tcpSynCl.Connected) - { - try - { - tcpASynCl.Shutdown(SocketShutdown.Both); - tcpSynCl.Shutdown(SocketShutdown.Both); - } - catch { } - if (OnClose != null) - OnClose(this, new ShutdownRequestEventArgs("SHUTDOWN")); - tcpSynCl.Close(); - tcpASynCl.Close(); - } - tcpSynCl = null; - tcpASynCl = null; - } - RemoveAllGroup(); - } - } - - public class ClientGroup : IGroup - { - public const byte fctHead = 0xAB; - public const byte fctReadSingle = 1; - public const byte fctReadMultiple = 2; - public const byte fctWriteSingle = 5; - public const byte fctWriteMultiple = 15; - - bool _active = false; - public bool IsActive - { - get - { - return _active; - } - set - { - _active = value; - if (value) - { - ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(ReciveData), _tcpASynCl); - } - } - } - - protected ushort _id; - public ushort ID - { - get - { - return _id; - } - } - - protected int _updateRate; - public int UpdateRate - { - get - { - return _updateRate; - } - set - { - _updateRate = value; - } - } - - protected DeviceAddress _start; - public DeviceAddress Start - { - get - { - return _start; - } - } - - public int Size - { - get - { - return _items == null ? 0 : _items.Length; - } - } - - protected string _name; - public string Name - { - get - { - return _name; - } - } - - protected float _deadband; - public float DeadBand - { - get - { - return _deadband; - } - set - { - _deadband = value; - } - } - - protected ClientReader _plcReader; - public IDevice Parent - { - get - { - return _plcReader; - } - } - - protected ITag[] _items; - public IEnumerable Items - { - get { return _items; } - } - - IDataServer _server; - Socket _tcpSynCl, _tcpASynCl; - - byte[] tcpSynClBuffer; - - public ClientGroup(ushort id, string name, int updateRate, bool active, ClientReader plcReader) - { - this._id = id; - this._name = name; - this._updateRate = updateRate; - this._active = active; - this._plcReader = plcReader; - this._server = plcReader.Parent; - this._tcpASynCl = plcReader.tcpASynCl; - this._tcpSynCl = plcReader.tcpASynCl; - tcpSynClBuffer = new byte[_tcpASynCl.ReceiveBufferSize]; - } - - private byte[] ReadSingleData(DeviceAddress address, DataSource source = DataSource.Cache) - { - short ID = (short)address.Start; - byte type = (byte)address.VarType; - byte[] idbits = BitConverter.GetBytes(ID); - byte[] write_data = new byte[6] { fctHead, fctReadSingle, - source == DataSource.Cache?(byte)0:(byte)1, idbits[0], idbits[1], type }; - byte[] data = new byte[type < 4 ? 1 : type < 6 ? 2 : 4]; - SocketError error; - _tcpSynCl.Send(write_data, 0, 6, SocketFlags.None, out error); - int result = _tcpSynCl.Receive(tcpSynClBuffer, 0, data.Length + 3, SocketFlags.None, out error); - Array.Copy(tcpSynClBuffer, 3, data, 0, data.Length); - if (error == SocketError.Success) - return data; - else - { - throw new SocketException((int)error); - } - } - - - private int WriteSingleData(DeviceAddress address, byte[] value) - { - short ID = (short)address.Start; - byte type = (byte)address.VarType; - byte[] idbits = BitConverter.GetBytes(ID); - byte[] write_data = new byte[6] { fctHead, fctWriteSingle, 1, idbits[0], idbits[1], type }; - byte[] data = new byte[6 + value.Length]; - write_data.CopyTo(data, 0); - value.CopyTo(data, 6); - SocketError error; - _tcpSynCl.Send(data, 0, data.Length, SocketFlags.None, out error); - int result = _tcpSynCl.Receive(tcpSynClBuffer, 0, 2, SocketFlags.None, out error); - if (error == SocketError.Success) - return tcpSynClBuffer[1]; - else - { - throw new SocketException((int)error); - } - } - - - public void Init() - { - if (_items != null) - { - for (int i = 0; i < _items.Length; i++) - { - _items[i].Value = _items[i].Read(DataSource.Cache);//DataSource.Device - } - } - } - - private void ReciveData(object state) - { - if (state == null || !_active) return; - byte[] bytes = new byte[_tcpASynCl.ReceiveBufferSize]; - int result = 0; - SocketError error; - do - { - result = _tcpASynCl.Receive(bytes, 0, bytes.Length, SocketFlags.None, out error); - if (result > 5 && bytes[0] == 0xAB) - { - short len = BitConverter.ToInt16(bytes, 1); - short count = BitConverter.ToInt16(bytes, 3); - int j = 5; - DateTime time = DateTime.UtcNow; - Storage value = Storage.Empty; - for (int i = 0; i < count; i++) - { - short id = BitConverter.ToInt16(bytes, j); - j += 2; - ITag tag = GetItemByID(id); - if (tag != null) - { - DataType type = (DataType)bytes[j++]; - switch (type) - { - case DataType.BOOL: - value.Boolean = BitConverter.ToBoolean(bytes, j++); - break; - case DataType.BYTE: - value.Byte = bytes[j++]; - break; - case DataType.SHORT: - value.Int16 = BitConverter.ToInt16(bytes, j); - j += 2; - break; - case DataType.INT: - value.Int32 = BitConverter.ToInt32(bytes, j); - j += 4; - break; - case DataType.FLOAT: - value.Single = BitConverter.ToSingle(bytes, j); - j += 4; - break; - } - tag.Update(value, time, QUALITIES.QUALITY_GOOD); - } - else - { - byte type = bytes[j]; - j += (type < 4 ? 2 : type < 6 ? 3 : 5); - } - } - //Array.Clear(bytes, 0, count); - } - } - while (result > 0); - } - - public bool AddItems(ItemMetaData[] items) - { - int count = items.Length; - if (_items == null) _items = new ITag[count]; - for (int i = 0; i < count; i++) - { - ITag dataItem = null; - ItemMetaData meta = items[i]; - DeviceAddress addr = new DeviceAddress(0, 0, meta.ID, meta.Size, 0, meta.DataType); - switch (meta.DataType) - { - case DataType.BOOL: - dataItem = new BoolTag(meta.ID, addr, this); - break; - case DataType.BYTE: - dataItem = new ByteTag(meta.ID, addr, this); - break; - case DataType.WORD: - case DataType.SHORT: - dataItem = new ShortTag(meta.ID, addr, this); - break; - case DataType.TIME: - case DataType.INT: - dataItem = new IntTag(meta.ID, addr, this); - break; - case DataType.FLOAT: - dataItem = new FloatTag(meta.ID, addr, this); - break; - case DataType.STR: - dataItem = new StringTag(meta.ID, addr, this); - break; - default: - dataItem = new BoolTag(meta.ID, addr, this); - break; - } - _items[i] = dataItem; - _server.AddItemIndex(meta.Name, dataItem); - } - Array.Sort(_items); - Init(); - return true; - } - - public bool RemoveAll() - { - Array.Clear(_items, 0, _items.Length); - return true; - } - - public bool SetActiveState(bool active, params short[] items) - { - return true; - } - - public int FindItemByAddress(DeviceAddress addr) - { - return Array.BinarySearch(_items, new BoolTag(0, addr, null)); - } - - public ITag GetItemByID(short id) - { - return _server[id]; - } - - public int BatchRead(DataSource source, bool isSync, params ITag[] itemArray) - { - if (itemArray == null) return -1; - int len = itemArray.Length; - byte[] bt = new byte[4]; - byte[] data = new byte[3 + len * 2]; - int j=0; - data[j++] = fctHead; - data[j++] = fctReadMultiple; - data[j++] = source == DataSource.Cache ? (byte)0 : (byte)1; - bt = BitConverter.GetBytes(itemArray.Length); - data[j++] = bt[0]; - data[j++] = bt[1]; - data[j++] = bt[2]; - data[j++] = bt[3]; - for (int i = 0; i < len; i++) - { - ITag tag = itemArray[i]; - bt = BitConverter.GetBytes(tag.ID); - data[j++] = bt[0]; - data[j++] = bt[1]; - data[j++] = (byte)(tag.Address.DataSize >> 3); - } - SocketError error; - _tcpSynCl.Send(data, 0, data.Length, SocketFlags.None, out error); - int result = _tcpSynCl.Receive(tcpSynClBuffer, 0, tcpSynClBuffer.Length, SocketFlags.None, out error); - j = 2; - if (error == SocketError.Success) - { - DateTime time=DateTime.UtcNow; - Storage value=Storage.Empty; - for (int i = 0; i < len; i++) - { - ITag tag = itemArray[i]; - switch (tag.Address.VarType) - { - case DataType.BOOL: - value.Boolean = BitConverter.ToBoolean(tcpSynClBuffer, j++); - break; - case DataType.BYTE: - value.Byte = tcpSynClBuffer[j++]; - break; - case DataType.SHORT: - value.Int16 = BitConverter.ToInt16(tcpSynClBuffer, j); - j += 2; - break; - case DataType.INT: - value.Int32 = BitConverter.ToInt32(tcpSynClBuffer, j); - j += 4; - break; - case DataType.FLOAT: - value.Single = BitConverter.ToSingle(tcpSynClBuffer, j); - j += 4; - break; - } - tag.Update(value, time, QUALITIES.QUALITY_GOOD); - } - return 0; - } - else - { - throw new SocketException((int)error); - } - } - - public int BatchWrite(IDictionary items, bool isSync = true) - { - List list = new List(new byte[] { fctHead, fctWriteMultiple }); - list.AddRange(BitConverter.GetBytes(items.Count)); - foreach (var item in items) - { - ITag tag = item.Key; - list.AddRange(BitConverter.GetBytes(tag.ID)); - switch (tag.Address.VarType) - { - case DataType.BOOL: - list.Add((bool)item.Value ? (byte)1 : (byte)0); - break; - case DataType.BYTE: - list.Add((byte)item.Value); - break; - case DataType.SHORT: - list.AddRange(BitConverter.GetBytes((short)item.Value)); - break; - case DataType.INT: - list.AddRange(BitConverter.GetBytes((int)item.Value)); - break; - case DataType.FLOAT: - list.AddRange(BitConverter.GetBytes((float)item.Value)); - break; - } - } - SocketError error; - _tcpSynCl.Send(list.ToArray(), 0, list.Count, SocketFlags.None, out error); - int result = _tcpSynCl.Receive(tcpSynClBuffer, 0, 2, SocketFlags.None, out error); - if (error == SocketError.Success) - return tcpSynClBuffer[1]; - else - { - throw new SocketException((int)error); - } - } - - - public ItemData ReadInt32(DeviceAddress address, DataSource source = DataSource.Cache) - { - var data = ReadSingleData(address, source); - return data == null ? new ItemData(0, 0, QUALITIES.QUALITY_BAD) : - new ItemData(BitConverter.ToInt32(data, 0), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadInt16(DeviceAddress address, DataSource source = DataSource.Cache) - { - var data = ReadSingleData(address, source); - return data == null ? new ItemData(0, 0, QUALITIES.QUALITY_BAD) : - new ItemData(BitConverter.ToInt16(data, 0), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadByte(DeviceAddress address, DataSource source = DataSource.Cache) - { - var data = ReadSingleData(address, source); - return data == null ? new ItemData(0, 0, QUALITIES.QUALITY_BAD) : - new ItemData(data[0], 0, QUALITIES.QUALITY_GOOD); - } - - public unsafe ItemData ReadFloat(DeviceAddress address, DataSource source = DataSource.Cache) - { - var data = ReadSingleData(address, source); - if (data == null) - return new ItemData(0.0f, 0, QUALITIES.QUALITY_BAD); - else - { - int value = BitConverter.ToInt32(data, 0); - return new ItemData(*(((float*)&value)), 0, QUALITIES.QUALITY_GOOD); - } - } - - public ItemData ReadBool(DeviceAddress address, DataSource source = DataSource.Cache) - { - var data = ReadSingleData(address, source); - return data == null ? new ItemData(false, 0, QUALITIES.QUALITY_BAD) : - new ItemData(BitConverter.ToBoolean(data, address.Bit), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadString(DeviceAddress address, DataSource source = DataSource.Cache) - { - var data = ReadSingleData(address, source); - return data == null ? new ItemData(string.Empty, 0, QUALITIES.QUALITY_BAD) : - new ItemData(Encoding.Default.GetString(data, 0, address.DataSize), 0, QUALITIES.QUALITY_GOOD); - } - - public int WriteInt32(DeviceAddress address, int value) - { - return WriteSingleData(address, BitConverter.GetBytes(value)); - } - - public int WriteInt16(DeviceAddress address, short value) - { - return WriteSingleData(address, BitConverter.GetBytes(value)); - } - - public int WriteFloat(DeviceAddress address, float value) - { - return WriteSingleData(address, BitConverter.GetBytes(value)); - } - - public int WriteString(DeviceAddress address, string value) - { - return WriteSingleData(address, Encoding.ASCII.GetBytes(value)); - } - - public int WriteBit(DeviceAddress address, bool value) - { - return WriteSingleData(address, new byte[] { (byte)(value ? 1 : 0) }); - } - - public int WriteBits(DeviceAddress address, byte value) - { - return WriteSingleData(address, new byte[] { value }); - } - - public event DataChangeEventHandler DataChange; - - public void Dispose() - { - RemoveAll(); - _items = null; - } - } -} diff --git a/SCADA/Program/DataService/FCTCOMMAND.cs b/SCADA/Program/DataService/FCTCOMMAND.cs deleted file mode 100644 index 8d128e8..0000000 --- a/SCADA/Program/DataService/FCTCOMMAND.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace DataService -{ - -} diff --git a/SCADA/Program/DataService/IModbusReader.cs b/SCADA/Program/DataService/IModbusReader.cs deleted file mode 100644 index 93333b3..0000000 --- a/SCADA/Program/DataService/IModbusReader.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System.Collections.Generic; - -namespace DataService -{ - public abstract class IModbusReader: IPLCDevice - { - public const byte fctReadCoil = 1; - public const byte fctReadDiscreteInputs = 2; - public const byte fctReadHoldingRegister = 3; - public const byte fctReadInputRegister = 4; - public const byte fctWriteSingleCoil = 5; - public const byte fctWriteSingleRegister = 6; - public const byte fctWriteMultipleCoils = 15; - public const byte fctWriteMultipleRegister = 16; - public const byte fctReadWriteMultipleRegister = 23; - - public int PDU - { - get { return 0xFF; } - } - - public int GetBlockSize(int area) - { - return area > 2 ? PDU / 2 : PDU; - } - - public DeviceAddress GetDeviceAddress(string address) - { - DeviceAddress dv = DeviceAddress.Empty; - if (address == null || address.Length < 3) - return dv; - else - { - int index = address.IndexOf('.'); - switch (address[0]) - { - case '0': - dv.Area = fctReadCoil; - if (index > 0) - { - dv.Start = 64 * int.Parse(address.Substring(1, index - 1)); - dv.Bit = byte.Parse(address.Substring(index + 1)); - } - break; - case '1': - dv.Area = fctReadDiscreteInputs; - if (index > 0) - { - dv.Start = 64 * int.Parse(address.Substring(1, index - 1)); - dv.Bit = byte.Parse(address.Substring(index + 1)); - } - break; - case '4': - dv.Area = fctReadHoldingRegister; - if (index > 0) - { - dv.Start = int.Parse(address.Substring(1, index - 1)); - dv.Bit = byte.Parse(address.Substring(index + 1)); - } - else - dv.Start = int.Parse(address.Mid(1)); - break; - case '3': - dv.Area = fctReadInputRegister; - if (index > 0) - { - dv.Start = int.Parse(address.Substring(1, index - 1)); - dv.Bit = byte.Parse(address.Substring(index + 1)); - } - else - dv.Start = int.Parse(address.Mid(1)); - break; - } - } - return dv; - } - - public string GetAddress(DeviceAddress address) - { - return string.Empty; - } - - - short _id; - public short ID - { - get - { - return _id; - } - } - - string _server; - public string ServerName - { - get { return _server; } - set { _server = value; } - } - - public abstract DeviceDriver Driver - { - get ; - } - - public abstract bool IsClosed - { - get; - } - - private int _timeout; - public int TimeOut - { - get { return _timeout; } - set { _timeout = value; } - } - - List _grps = new List(20); - public IEnumerable Groups - { - get { return _grps; } - } - - IDataServer _parent; - public IDataServer Parent - { - get { return _parent; } - } - - public abstract bool Connect(); - - public IGroup AddGroup(string name, short id, int updateRate, float deadBand = 0f, bool active = false) - { - ModbusGroup grp = new ModbusGroup(id, name, updateRate, active, this); - _grps.Add(grp); - return grp; - } - - public bool RemoveGroup(IGroup grp) - { - grp.IsActive = false; - return _grps.Remove(grp); - } - - public event ShutdownRequestEventHandler OnClose; - - public abstract void Dispose(); - - public abstract byte[] ReadBytes(DeviceAddress address, ushort size); - - public abstract ItemData ReadInt32(DeviceAddress address); - - public abstract ItemData ReadInt16(DeviceAddress address); - - public abstract ItemData ReadByte(DeviceAddress address); - - public abstract ItemData ReadString(DeviceAddress address, ushort size); - - public abstract ItemData ReadFloat(DeviceAddress address); - - public abstract ItemData ReadBit(DeviceAddress address); - - public abstract ItemData ReadValue(DeviceAddress address); - - public abstract int WriteBytes(DeviceAddress address, byte[] bit); - - public abstract int WriteBit(DeviceAddress address, bool bit); - - public abstract int WriteBits(DeviceAddress address, byte bits); - - public abstract int WriteInt16(DeviceAddress address, short value); - - public abstract int WriteInt32(DeviceAddress address, int value); - - public abstract int WriteFloat(DeviceAddress address, float value); - - public abstract int WriteString(DeviceAddress address, string str); - - public abstract int WriteValue(DeviceAddress address, object value); - } -} diff --git a/SCADA/Program/DataService/Route.cs b/SCADA/Program/DataService/Route.cs deleted file mode 100644 index ea54d71..0000000 --- a/SCADA/Program/DataService/Route.cs +++ /dev/null @@ -1,219 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace DataService -{ - [Flags] - public enum DevStatus - { - None = 0, - RUN = 0x0001, - STOP = 0x0002, - OLS = 0x0004, - CLS = 0x0008, - PAUSE = 0x0010, - READY = 0x0020, - DUMP = 0x0040, - COMPLETE = 0x0080, - ALARM = 0x0100, - } - - [Flags] - public enum DevCommand - { - START = 0x0001, - STOP = 0x0002, - ABORT = 0x0004, - PAUSE = 0x0008, - HOLD = 0x0010, - WET = 0x0020, - DISCH = 0x0040, - DUMP = 0x0080, - } - - public interface IDevice : IComparable - { - string Name { get; } - string Description { get; } - DeviceTypes DeviceType { get; } - int ExtStatus { get; } - DevStatus Status { get; } - int Execute(DevCommand command); - } - - //可以根据订单的源仓目标仓,二分法搜索到仓的列表。得到仓的参数和状态;部分设备是互斥的(如闸门,分配器,三通),马达等是共用的。 - - - public sealed class RouteVex : IComparable, IEqualityComparer - { - string _source; - public string Source - { - get { return _source; } - } - - string _dest; - public string Dest - { - get { return _dest; } - } - - RouteVex _front; - public RouteVex Front - { - get { return _front; } - set { _front = value; } - } - - Func _func; - Func _funcWrite; - public bool IsRun - { - get { return _func == null ? true : _func(); } - } - - public RouteVex(string source, string dest = "") - { - _source = source; - _dest = dest; - } - - public RouteVex(string source, string dest, string startSignal, string runSignal, IDataServer srv) - { - _source = source; - _dest = dest; - var eval = srv.Eval; - if (!string.IsNullOrEmpty(runSignal)) - { - _func = eval.Eval(runSignal) as Func; - } - if (!string.IsNullOrEmpty(startSignal)) - { - _funcWrite = eval.WriteEval(startSignal) as Func; - } - } - - public int Start() - { - return _funcWrite == null ? 0 : _funcWrite(); - } - - public int CompareTo(RouteVex other) - { - if (this._source == null) return 0; - if (other._dest == null) return 1; - int comp = this._source.CompareTo(other._source); - return comp == 0 ? this._dest.CompareTo(other._dest) : comp; - } - - public override string ToString() - { - return string.Concat(_source, ",", _dest); - } - - public bool Equals(RouteVex x, RouteVex y) - { - if (x == null || y == null) return false; - return ((x.Source == null && y.Source == null) || (x.Source == y.Source)) && - ((x.Dest == null && y.Dest == null) || (x.Dest == y.Dest)); - } - - public int GetHashCode(RouteVex obj) - { - return obj.Source.GetHashCode() ^ obj.Dest.GetHashCode(); - } - } - - //正在执行的路径,保存到一个XML文件,包含当前执行的节点 - public sealed class Route : LinkedList, ICloneable - { - LinkedListNode _current; - public LinkedListNode Current - { - get { return _current; } - set { _current = value; } - } - - string _start; - public string Start - { - get { return _start; } - set { _start = value; } - } - - string _end; - public string End - { - get { return _end; } - set { _end = value; } - } - - RouteState _state; - public RouteState State - { - get { return _state; } - set { _state = value; } - } - - string[] _options; - public string[] Options - { - get { return _options; } - set { _options = value; } - } - - - public Route(string source, string dest, string[] options = null) - { - _start = source; - _end = dest; - _options = options; - } - - public object Clone() - { - Route rt = new Route(_start, _end, _options); - foreach (var routeVex in this) - { - rt.AddLast(routeVex); - } - return rt; - } - } - - public enum DeviceTypes - { - Misc, - Motor = 1, - Valve = 2, - Gate = 3, - Divert = 4, - FourWays = 5, - Distribute = 6, - Bin = 7, - Analog = 8, - Scale = 9, - Conveyor = 10, - Elevator = 11, - Mixer = 12, - Grind = 13, - Pellet = 14, - Extruder = 15, - CheckScale = 16, - Liquid = 17, - HandAdd = 18, - Sifter = 19, - Cyclone = 20, - AirHammer = 21 - } - - public enum RouteState - { - Idle, - Run, - Stop, - Suspend, - Complete, - Abort, - } -} diff --git a/SCADA/Program/DataService/TagListGroup.cs b/SCADA/Program/DataService/TagListGroup.cs deleted file mode 100644 index f456dbe..0000000 --- a/SCADA/Program/DataService/TagListGroup.cs +++ /dev/null @@ -1,385 +0,0 @@ -using System.Collections.Generic; -using System.Threading; - -namespace DataService -{ - public class TagListGroup : IGroup - { - bool _isActive, _isDisposed; - public bool IsActive - { - get - { - return _isActive; - } - set - { - _isActive = value; - if (value) - { - ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(this.OnUpdate), 1); - } - } - } - - short _id; - public short ID - { - get - { - return _id; - } - } - - int _updateRate; - public int UpdateRate - { - get - { - return _updateRate; - } - set - { - _updateRate = value; - } - } - - string _name; - public string Name - { - get - { - return _name; - } - set - { - _name = value; - } - } - - float deadband; - public float DeadBand - { - get - { - return deadband; - } - set - { - deadband = value; - } - } - - IPLCDriver _reader; - public IDriver Parent - { - get { return _reader; } - } - - List _items; - public IEnumerable Items - { - get { return _items; } - } - - IDataServer _server; - public IDataServer Server - { - get - { - return _server; - } - } - - HashSet _activeList; - CompareItemByID rc = new CompareItemByID(); - - public TagListGroup(short id, string name, int updateRate, int size, bool active, IPLCDriver plcReader) - { - this._id = id; - this._updateRate = updateRate; - this._isActive = active; - this._reader = plcReader; - this._name = name; - this._server = _reader.Parent; - } - - - public bool AddItems(IList items) - { - int count = items.Count; - if (_items == null) - { - _items = new List(count); - _activeList = new HashSet(); - } - lock (_server.SyncRoot) - { - for (int i = 0; i < count; i++) - { - ITag dataItem = null; - TagMetaData meta = items[i]; - if (meta.GroupID == this._id) - { - switch (meta.DataType) - { - case DataType.BOOL: - dataItem = new BoolTag(meta.ID, _reader.GetDeviceAddress(meta.Address), this); - break; - case DataType.BYTE: - dataItem = new ByteTag(meta.ID, _reader.GetDeviceAddress(meta.Address), this); - break; - case DataType.WORD: - case DataType.SHORT: - dataItem = new ShortTag(meta.ID, _reader.GetDeviceAddress(meta.Address), this); - break; - case DataType.TIME: - case DataType.INT: - dataItem = new IntTag(meta.ID, _reader.GetDeviceAddress(meta.Address), this); - break; - case DataType.FLOAT: - dataItem = new FloatTag(meta.ID, _reader.GetDeviceAddress(meta.Address), this); - break; - case DataType.STR: - dataItem = new StringTag(meta.ID, _reader.GetDeviceAddress(meta.Address), this); - break; - } - if (dataItem != null) - { - _items.Add(dataItem); - _server.AddItemIndex(meta.Name, dataItem); - _activeList.Add(meta.ID); - } - } - } - } - _items.Sort(rc); - _items.TrimExcess(); - return true; - } - - public bool AddTags(IEnumerable tags) - { - if (_items == null) - { - _items = new List(); - } - foreach (ITag tag in tags) - { - if (tag != null) - { - _items.Add(tag); - if (tag.Active) - { - _activeList.Add(tag.ID); - } - } - } - _items.Sort(rc); - _items.TrimExcess(); - return true; - } - - public bool RemoveItems(params ITag[] items) - { - foreach (var item in items) - { - _server.RemoveItemIndex(item.GetTagName()); - _activeList.Remove(item.ID); - _items.Remove(item); - } - return true; - } - - - public bool SetActiveState(bool active, params short[] items) - { - if (active) - { - lock (_activeList) - { - _activeList.UnionWith(items); - }; - } - else - { - lock (_activeList) - { - _activeList.ExceptWith(items); - }; - } - return true; - } - - public ITag FindItemByAddress(DeviceAddress addr) - { - for (int i = 0; i < _items.Count; i++) - { - DeviceAddress address = _items[i].Address; - if (addr.Area == address.Area - && addr.DBNumber == address.DBNumber - && addr.Start == address.Start - && addr.Bit == address.Bit) - return _items[i]; - } - return null; - } - - public HistoryData[] BatchRead(DataSource source, bool isSync, params ITag[] itemArray) - { - return ExtMethods.BatchRead(source, itemArray); - } - - public int BatchWrite(SortedDictionary items, bool isSync = true) - { - int rev = ExtMethods.BatchWrite(items); - if (DataChange != null && rev >= 0) - { - int len = items.Count; - HistoryData[] data = new HistoryData[len]; - int i = 0; - foreach (var item in items) - { - ITag tag = item.Key; - data[i].ID = tag.ID; - data[i].TimeStamp = tag.TimeStamp; - data[i].Quality = tag.Quality; - data[i].Value = item.Key.ToStorage(item.Value); - i++; - } - foreach (DataChangeEventHandler deleg in DataChange.GetInvocationList()) - { - deleg.BeginInvoke(this, new DataChangeEventArgs(1, data), null, null); - } - } - return rev; - } - - public ItemData ReadInt32(DeviceAddress address, DataSource source = DataSource.Cache) - { - return _reader.ReadInt32(address); - } - - public ItemData ReadInt16(DeviceAddress address, DataSource source = DataSource.Cache) - { - return _reader.ReadInt16(address); - } - - public ItemData ReadByte(DeviceAddress address, DataSource source = DataSource.Cache) - { - return _reader.ReadByte(address); - } - - public ItemData ReadFloat(DeviceAddress address, DataSource source = DataSource.Cache) - { - return _reader.ReadFloat(address); - } - - public ItemData ReadBool(DeviceAddress address, DataSource source = DataSource.Cache) - { - return _reader.ReadBit(address); - } - - public ItemData ReadString(DeviceAddress address, DataSource source = DataSource.Cache) - { - return new ItemData(string.Empty, 0, QUALITIES.QUALITY_UNCERTAIN); - } - - public int WriteInt32(DeviceAddress address, int value) - { - return _reader.WriteInt32(address, value); - } - - public int WriteInt16(DeviceAddress address, short value) - { - return _reader.WriteInt16(address, value); - } - - public int WriteFloat(DeviceAddress address, float value) - { - return _reader.WriteFloat(address, value); - } - - public int WriteString(DeviceAddress address, string value) - { - return _reader.WriteString(address, value); - } - - public int WriteBit(DeviceAddress address, bool value) - { - return _reader.WriteBit(address, value); - } - - public int WriteBits(DeviceAddress address, byte value) - { - return _reader.WriteBits(address, value); - } - - public void Dispose() - { - _items.Clear(); - _activeList.Clear(); - _isDisposed = true; - //Dictionary dict = new Dictionary(); - } - - - public void OnUpdate(object stateInfo) - { - while (true) - { - Thread.Sleep(_updateRate); - lock (this) - { - if (!_isActive || _isDisposed) - { - return; - } - List hdalist = new List(); - foreach (short id in _activeList) - { - ITag item = _server[id]; - if (item != null) - { - if (item.Refresh() && DataChange != null) - hdalist.Add(new HistoryData(item.ID, item.Quality, item.Value, item.TimeStamp)); - } - if (DataChange != null) - DataChange.BeginInvoke(this, new DataChangeEventArgs(1, hdalist), null, null); - } - } - } - } - - public event DataChangeEventHandler DataChange; - } - - public class CompareItemByID : IComparer - { - public int Compare(ITag x, ITag y) - { - if (x == null) - { - if (y == null) - { - return 0; - } - else - { - return -1; - } - } - else - { - if (y == null) - { - return 1; - } - else - { - return x.ID.CompareTo(y.ID); - } - } - } - } -} diff --git a/SCADA/Program/DataService/WpfApplication1.sln b/SCADA/Program/DataService/WpfApplication1.sln deleted file mode 100644 index f594be4..0000000 --- a/SCADA/Program/DataService/WpfApplication1.sln +++ /dev/null @@ -1,30 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataService", "DataService.csproj", "{8965E389-6466-4B30-BD43-83C909044637}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|Mixed Platforms = Debug|Mixed Platforms - Debug|x86 = Debug|x86 - Release|Any CPU = Release|Any CPU - Release|Mixed Platforms = Release|Mixed Platforms - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {8965E389-6466-4B30-BD43-83C909044637}.Debug|Any CPU.ActiveCfg = Debug|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Debug|Mixed Platforms.Build.0 = Debug|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Debug|x86.ActiveCfg = Debug|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Debug|x86.Build.0 = Debug|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Release|Any CPU.ActiveCfg = Release|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Release|Mixed Platforms.ActiveCfg = Release|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Release|Mixed Platforms.Build.0 = Release|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Release|x86.ActiveCfg = Release|x86 - {8965E389-6466-4B30-BD43-83C909044637}.Release|x86.Build.0 = Release|x86 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/SCADA/Program/DataService/bin/Debug/DataService.dll b/SCADA/Program/DataService/bin/Debug/DataService.dll index c158fd8..ee1ce68 100644 Binary files a/SCADA/Program/DataService/bin/Debug/DataService.dll and b/SCADA/Program/DataService/bin/Debug/DataService.dll differ diff --git a/SCADA/Program/DataService/codeback.txt b/SCADA/Program/DataService/codeback.txt deleted file mode 100644 index 6e0881a..0000000 --- a/SCADA/Program/DataService/codeback.txt +++ /dev/null @@ -1,1219 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Sockets; -using System.Net; -using System.Text; -using System.Diagnostics; - -namespace DataService -{ - public class ModbusTCPReader : IPLCDevice - { - public const byte fctReadCoil = 1; - public const byte fctReadDiscreteInputs = 2; - public const byte fctReadHoldingRegister = 3; - public const byte fctReadInputRegister = 4; - public const byte fctWriteSingleCoil = 5; - public const byte fctWriteSingleRegister = 6; - public const byte fctWriteMultipleCoils = 15; - public const byte fctWriteMultipleRegister = 16; - public const byte fctReadWriteMultipleRegister = 23; - - /// Constant for exception illegal function. - public const byte excIllegalFunction = 1; - /// Constant for exception illegal data address. - public const byte excIllegalDataAdr = 2; - /// Constant for exception illegal data value. - public const byte excIllegalDataVal = 3; - /// Constant for exception slave device failure. - public const byte excSlaveDeviceFailure = 4; - /// Constant for exception acknowledge. - public const byte excAck = 5; - /// Constant for exception slave is busy/booting up. - public const byte excSlaveIsBusy = 6; - /// Constant for exception gate path unavailable. - public const byte excGatePathUnavailable = 10; - /// Constant for exception not connected. - public const byte excExceptionNotConnected = 253; - /// Constant for exception connection lost. - public const byte excExceptionConnectionLost = 254; - /// Constant for exception response timeout. - public const byte excExceptionTimeout = 255; - /// Constant for exception wrong offset. - private const byte excExceptionOffset = 128; - /// Constant for exception send failt. - private const byte excSendFailt = 100; - - private static int _timeout; - - private Socket tcpSynCl; - private byte[] tcpSynClBuffer = new byte[256]; - - /// Response data event. This event is called when new data arrives - public delegate void ResponseData(ushort id, byte function, byte[] data); - /// Exception data event. This event is called when the data is incorrect - public delegate void ExceptionData(int id, byte function, byte exception); - /// Exception data event. This event is called when the data is incorrect - short _id; - public short ID - { - get - { - return _id; - } - } - - string _ip; - public string ServerName - { - get { return _ip; } - set { _ip = value; } - } - - public bool IsClosed - { - get - { - return tcpSynCl == null || tcpSynCl.Connected == false; - } - } - - public int TimeOut - { - get { return _timeout; } - set{ _timeout = value; } - } - - public DeviceDriver Driver - { - get - { - return DeviceDriver.ModbusTcp; - } - } - - List _grps = new List(20); - public IEnumerable Groups - { - get { return _grps; } - } - - IDataServer _server; - public IDataServer Parent - { - get { return _server; } - } - - public ModbusTCPReader(IDataServer server,short id, string ip, int timeOut = 500) - { - _id = id; - _server = server; - _ip = ip; - _timeout = timeOut; - } - - public bool Connect() - { - try - { - int port = 502; - //IPAddress ip = IPAddress.Parse(_ip); - // ---------------------------------------------------------------- - // Connect synchronous client - tcpSynCl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - tcpSynCl.Connect(_ip, port); - tcpSynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _timeout); - tcpSynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout); - tcpSynCl.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1); - return true; - } - catch (SocketException error) - { - if (OnClose != null) - OnClose(this, new ShutdownRequestEventArgs(error.Message)); - return false; - } - } - - private byte[] CreateReadHeader(int id, int startAddress, ushort length, byte function) - { - byte[] data = new byte[12]; - - byte[] _id = BitConverter.GetBytes((short)id); - data[0] = _id[0]; // Slave id high byte - data[1] = _id[1]; // Slave id low byte - data[5] = 6; // Message size - data[6] = 0; // Slave address - data[7] = function; // Function code - byte[] _adr = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)startAddress)); - data[8] = _adr[0]; // Start address - data[9] = _adr[1]; // Start address - byte[] _length = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)length)); - data[10] = _length[0]; // Number of data to read - data[11] = _length[1]; // Number of data to read - return data; - } - - private byte[] CreateWriteHeader(int id, int startAddress, ushort numData, ushort numBytes, byte function) - { - byte[] data = new byte[numBytes + 11]; - - byte[] _id = BitConverter.GetBytes(id); - data[0] = _id[0]; // Slave id high byte - data[1] = _id[1]; // Slave id low byte+ - byte[] _size = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)(5 + numBytes))); - data[4] = _size[0]; // Complete message size in bytes - data[5] = _size[1]; // Complete message size in bytes - data[6] = 0; // Slave address - data[7] = function; // Function code - byte[] _adr = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)startAddress)); - data[8] = _adr[0]; // Start address - data[9] = _adr[1]; // Start address - if (function >= fctWriteMultipleCoils) - { - byte[] _cnt = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)numData)); - data[10] = _cnt[0]; // Number of bytes - data[11] = _cnt[1]; // Number of bytes - data[12] = (byte)(numBytes - 2); - } - return data; - } - - private byte[] WriteSyncData(byte[] write_data) - { - short id = BitConverter.ToInt16(write_data, 0); - if (IsClosed) CallException(id, write_data[7], excExceptionConnectionLost); - else - { - try - { - tcpSynCl.Send(write_data, 0, write_data.Length, SocketFlags.None); - int result = tcpSynCl.Receive(tcpSynClBuffer, 0, tcpSynClBuffer.Length, SocketFlags.None); - - byte function = tcpSynClBuffer[7]; - byte[] data; - - if (result == 0) CallException(id, write_data[7], excExceptionConnectionLost); - - // ------------------------------------------------------------ - // Response data is slave exception - if (function > excExceptionOffset) - { - function -= excExceptionOffset; - CallException(id, function, tcpSynClBuffer[8]); - return null; - } - // ------------------------------------------------------------ - // Write response data - else if ((function >= fctWriteSingleCoil) && (function != fctReadWriteMultipleRegister)) - { - data = new byte[2]; - Array.Copy(tcpSynClBuffer, 10, data, 0, 2); - } - // ------------------------------------------------------------ - // Read response data - else - { - data = new byte[tcpSynClBuffer[8]]; - Array.Copy(tcpSynClBuffer, 9, data, 0, tcpSynClBuffer[8]); - } - return data; - } - catch (SocketException) - { - CallException(id, write_data[7], excExceptionConnectionLost); - } - } - return null; - } - - public byte[] WriteSingleCoils(int id, int startAddress, bool OnOff) - { - byte[] data; - data = CreateWriteHeader(id, startAddress, 1, 1, fctWriteSingleCoil); - if (OnOff == true) data[10] = 255; - else data[10] = 0; - return WriteSyncData(data); - } - - public byte[] WriteMultipleCoils(int id, int startAddress, ushort numBits, byte[] values) - { - byte numBytes = Convert.ToByte(values.Length); - byte[] data; - data = CreateWriteHeader(id, startAddress, numBits, (byte)(numBytes + 2), fctWriteMultipleCoils); - Array.Copy(values, 0, data, 13, numBytes); - return WriteSyncData(data); - } - - public byte[] WriteSingleRegister(int id, int startAddress, byte[] values) - { - byte[] data; - data = CreateWriteHeader(id, startAddress, 1, 1, fctWriteSingleRegister); - data[10] = values[0]; - data[11] = values[1]; - return WriteSyncData(data); - } - - public byte[] WriteMultipleRegister(int id, int startAddress, byte[] values) - { - ushort numBytes = Convert.ToUInt16(values.Length); - if (numBytes % 2 > 0) numBytes++; - byte[] data; - - data = CreateWriteHeader(id, startAddress, Convert.ToUInt16(numBytes / 2), Convert.ToUInt16(numBytes + 2), fctWriteMultipleRegister); - Array.Copy(values, 0, data, 13, values.Length); - return WriteSyncData(data); - } - - public int PDU - { - get { return 256; } - } - - public int GetBlockSize(int area) - { - return area > 2 ? PDU / 2 : PDU; - } - - public DeviceAddress GetDeviceAddress(string address) - { - DeviceAddress dv = DeviceAddress.Empty; - if (address == null || address.Length < 3) - return dv; - else - { - int index = address.IndexOf('.'); - switch (address[0]) - { - case '0': - dv.Area = fctReadCoil; - if (index > 0) - { - dv.Start = 64 * int.Parse(address.Substring(1, index - 1)); - dv.Bit = byte.Parse(address.Substring(index + 1)); - } - break; - case '1': - dv.Area = fctReadDiscreteInputs; - if (index > 0) - { - dv.Start = 64 * int.Parse(address.Substring(1, index - 1)); - dv.Bit = byte.Parse(address.Substring(index + 1)); - } - break; - case '4': - dv.Area = fctReadHoldingRegister; - if (index > 0) - { - dv.Start = int.Parse(address.Substring(1, index - 1)); - dv.Bit = byte.Parse(address.Substring(index + 1)); - } - else - dv.Start = int.Parse(address.Mid(1)); - break; - case '3': - dv.Area = fctReadInputRegister; - if (index > 0) - { - dv.Start = int.Parse(address.Substring(1, index - 1)); - dv.Bit = byte.Parse(address.Substring(index + 1)); - } - else - dv.Start = int.Parse(address.Mid(1)); - break; - } - } - return dv; - } - - public string GetAddress(DeviceAddress address) - { - return string.Empty; - } - - - public IGroup AddGroup(string name, short id, int updateRate, float deadBand = 0f, bool active = false) - { - ModbusGroup grp = new ModbusGroup(id, name, updateRate, active, this); - _grps.Add(grp); - return grp; - } - - public bool RemoveGroup(IGroup grp) - { - grp.IsActive = false; - return _grps.Remove(grp); - } - - public void Dispose() - { - if (tcpSynCl != null) - { - if (tcpSynCl.Connected) - { - try { tcpSynCl.Shutdown(SocketShutdown.Both); } - catch { } - tcpSynCl.Close(); - } - tcpSynCl = null; - } - foreach (IGroup grp in _grps) - { - grp.Dispose(); - } - _grps.Clear(); - } - - internal string GetErrorString(byte exception) - { - switch (exception) - { - case excIllegalFunction: - return "Constant for exception illegal function."; - case excIllegalDataAdr: - return "Constant for exception illegal data address."; - case excIllegalDataVal: - return "Constant for exception illegal data value."; - case excSlaveDeviceFailure: - return "Constant for exception slave device failure."; - case excAck: - return "Constant for exception acknowledge."; - case excSlaveIsBusy: - return "Constant for exception slave is busy/booting up."; - case excGatePathUnavailable: - return "Constant for exception gate path unavailable."; - case excExceptionNotConnected: - return "Constant for exception not connected."; - case excExceptionConnectionLost: - return "Constant for exception connection lost."; - case excExceptionTimeout: - return "Constant for exception response timeout."; - case excExceptionOffset: - return "Constant for exception wrong offset."; - case excSendFailt: - return "Constant for exception send failt."; - } - return string.Empty; - } - - internal void CallException(int id, byte function, byte exception) - { - if (tcpSynCl == null) return; - if (exception == excExceptionConnectionLost && IsClosed == false) - { - if (OnClose != null) - OnClose(this, new ShutdownRequestEventArgs(GetErrorString(exception))); - } - } - - public byte[] ReadBytes(DeviceAddress address, ushort size) - { - return WriteSyncData(CreateReadHeader(address.Area, address.Start, size, (byte)address.Area)); - } - - public ItemData ReadInt32(DeviceAddress address) - { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 4, (byte)address.Area)); - if (data == null) - return new ItemData(0, 0, QUALITIES.QUALITY_BAD); - else - return new ItemData(IPAddress.HostToNetworkOrder(BitConverter.ToInt32(data, 0)), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadInt16(DeviceAddress address) - { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 2, (byte)address.Area)); - if (data == null) - return new ItemData(0, 0, QUALITIES.QUALITY_BAD); - else - return new ItemData(IPAddress.HostToNetworkOrder(BitConverter.ToInt16(data, 0)), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadByte(DeviceAddress address) - { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 1, (byte)address.Area)); - if (data == null) - return new ItemData(0, 0, QUALITIES.QUALITY_BAD); - else - return new ItemData(data[0], 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadString(DeviceAddress address, ushort size) - { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, size, (byte)address.Area)); - if (data == null) - return new ItemData(string.Empty, 0, QUALITIES.QUALITY_BAD); - else - return new ItemData(Encoding.Default.GetString(data, 0, size), 0, QUALITIES.QUALITY_GOOD);//Ƿֽ⣿ - } - - public unsafe ItemData ReadFloat(DeviceAddress address) - { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 4, (byte)address.Area)); - if (data == null) - return new ItemData(0.0f, 0, QUALITIES.QUALITY_BAD); - else - { - int value = IPAddress.HostToNetworkOrder(BitConverter.ToInt32(data, 0)); - return new ItemData(*(((float*)&value)), 0, QUALITIES.QUALITY_GOOD); - } - } - - public unsafe ItemData ReadBit(DeviceAddress address) - { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 1, (byte)address.Area)); - if (data == null) - return new ItemData(false, 0, QUALITIES.QUALITY_BAD); - else - { - fixed (byte* p = data) - { - int* p1 = (int*)p; - return new ItemData((*p1 & (1 << (address.Bit < 8 ? address.Bit + 8 : address.Bit - 8))) - != 0, 0, QUALITIES.QUALITY_GOOD); - } - } - } - - public ItemData ReadValue(DeviceAddress address) - { - throw new NotImplementedException(); - } - - public int WriteBytes(DeviceAddress address, byte[] bit) - { - var data = WriteMultipleRegister(address.Area, address.Start, bit); - return data == null ? -1 : 0; - } - - public int WriteBit(DeviceAddress address, bool bit) - { - var data = WriteSingleCoils(address.Area, address.Start + address.Bit, bit); - return data == null ? -1 : 0; - } - - public int WriteBits(DeviceAddress address, byte bits) - { - var data = WriteSingleRegister(address.Area, address.Start, new byte[] { bits }); - return data == null ? -1 : 0; - } - - public int WriteInt16(DeviceAddress address, short value) - { - var data = WriteSingleRegister(address.Area, address.Start, BitConverter.GetBytes(value)); - return data == null ? -1 : 0; - } - - public int WriteInt32(DeviceAddress address, int value) - { - var data = WriteSingleRegister(address.Area, address.Start, BitConverter.GetBytes(value)); - return data == null ? -1 : 0; - } - - public int WriteFloat(DeviceAddress address, float value) - { - var data = WriteSingleRegister(address.Area, address.Start, BitConverter.GetBytes(value)); - return data == null ? -1 : 0; - } - - public int WriteString(DeviceAddress address, string str) - { - var data = WriteSingleRegister(address.Area, address.Start, Encoding.ASCII.GetBytes(str)); - return data == null ? -1 : 0; - } - - public int WriteValue(DeviceAddress address, object value) - { - throw new NotImplementedException(); - } - - public override string ToString() - { - return "MODBUS̫"; - } - - public event ShutdownRequestEventHandler OnClose; - } - - public class ModbusGroup : PLCGroup - { - public ModbusGroup(short id, string name, int updateRate, bool active, IPLCDevice plcReader) - { - this._id = id; - this._name = name; - this._updateRate = updateRate; - this._isActive = active; - this._plcReader = plcReader; - this._server = _plcReader.Parent; - this._timer = new MMTimer(); - this._changedList = new List(100); - this._cacheReader = new ModbusCacheReader(); - } - - protected override unsafe int StartScan(DeviceAddress start, ushort len) - { - if (_start.Area > 2) - { - byte[] b1 = _plcReader.ReadBytes(start, len); - if (b1 == null) - return -1; - fixed (byte* p3 = b1) - { - short* p2 = (short*)p3; - int address = start.Start; - fixed (byte* numPtr = _cacheReader.Cache) - { - short* p1 = (short*)numPtr + address - _start.Start; - int i = 0; - ITag other = new BoolTag(0, DeviceAddress.Empty, null); - while (i < len) - { - //start.Start += i; - p2[i] = IPAddress.HostToNetworkOrder(p2[i]); - if (p2[i] != p1[i]) - { - start.Start = address + i; - other.Address = start; - int index = _items.BinarySearch(other); - if (index >= 0) - { - ITag item1 = _items[index]; - int size = item1.Address.DataSize; - if (size >= 16) - { - _changedList.Add(index); - i += (size / 16); - } - else - { - DeviceAddress addr = _items[index].Address; - int tmp = p2[i] ^ p1[i]; - while (addr.Start == start.Start) - { - if ((tmp & (1 << addr.Bit)) > 0) - _changedList.Add(index); - if (++index < _items.Count) - addr = _items[index].Address; - else - break; - } - i++; - } - } - else - { - index = ~index; - if (index < _items.Count) - { - DeviceAddress addr = _items[index].Address; - if (addr.Start == start.Start) - { - int tmp = p2[i] ^ p1[i]; - while (addr.Start == start.Start) - { - if ((tmp & (1 << addr.Bit)) > 0) - _changedList.Add(index); - if (++index < _items.Count) - addr = _items[index].Address; - else - break; - } - } - else - { - if (addr.Start > start.Start && index > 0) - addr = _items[--index].Address; - int size = addr.DataSize / 16; - if (size > start.Start - addr.Start) - { - _changedList.Add(index); - i += size + addr.Start - start.Start; - continue; - } - } - } - i++; - } - } - else - i++; - } - for (int j = 0; j < len; j++) - { - *(p1++) = p2[j]; - } - return 1; - } - } - } - else - { - byte[] b1 = _plcReader.ReadBytes(start, len); - if (b1 == null || _cacheReader.Size < 1) - return -1; - fixed (byte* p3 = b1) - { - long* p2 = (long*)p3; - int address = start.Start; - fixed (byte* numPtr = _cacheReader.Cache) - { - long* p1 = (long*)numPtr + address - _start.Start; - int i = 0; - len <<= 3; - ITag other = new BoolTag(0, DeviceAddress.Empty, null); - while (i < len) - { - p2[i] = IPAddress.HostToNetworkOrder(p2[i]); - if (p2[i] != p1[i]) - { - start.Start = address + i << 6; - other.Address = start; - int index = _items.BinarySearch(other); - if (index > 0) - { - DeviceAddress addr = _items[index].Address; - long tmp = p2[i] ^ p1[i]; - while (addr.Start == start.Start) - { - if ((tmp & (1 << addr.Bit)) > 0) - _changedList.Add(index); - if (++index < _items.Count) - addr = _items[index].Address; - else - break; - } - } - else - { - index = ~index; - if (index < _items.Count) - { - DeviceAddress addr = _items[index].Address; - if (addr.Start == start.Start) - { - long tmp = p2[i] ^ p1[i]; - while (addr.Start == start.Start) - { - if ((tmp & (1 << addr.Bit)) > 0) - _changedList.Add(index); - if (++index < _items.Count) - addr = _items[index].Address; - else - break; - } - } - } - } - p1[i] = p2[i]; - i++; - } - else - i++; - } - return 1; - } - } - } - } - - } -} - - - - -using System; -using System.Net; -using System.Text; - -namespace DataService -{ - public unsafe class CacheReader : ICache - { - byte[] _cache; - public byte[] Cache - { - get - { - return _cache; - } - } - - DeviceAddress _start; - public DeviceAddress Start - { - get - { - return _start; - } - set - { - _start = value; - } - } - - int _size; - public int Size - { - get - { - return _size; - } - set - { - _size = value; - this._cache = new byte[_size]; - } - } - - public CacheReader() - { - } - - public CacheReader(DeviceAddress start, int size) - { - this.Start = start; - this.Size = size; - } - - public int GetOffset(DeviceAddress start, DeviceAddress end) - { - return start.Start - end.Start; - } - - public ItemData ReadInt32(DeviceAddress address) - { - return new ItemData(BitConverter.ToInt32(_cache, address.Start - _start.Start), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadBit(DeviceAddress address) - { - //byte b = _cache[address.Start - _start.Start]; - fixed (byte* p = _cache) - { - int* p1 = (int*)(p + address.Start - _start.Start); - return new ItemData((*p1 & (1 << address.Bit)) != 0, 0, QUALITIES.QUALITY_GOOD); - } - } - - public ItemData ReadInt16(DeviceAddress address) - { - return new ItemData(BitConverter.ToInt16(_cache, address.Start - _start.Start), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadByte(DeviceAddress address) - { - return new ItemData(_cache[address.Start - _start.Start], 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadString(DeviceAddress address, ushort size = 64) - { - return new ItemData(BitConverter.ToString(_cache, address.Start - _start.Start, size), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadFloat(DeviceAddress address) - { - return new ItemData(BitConverter.ToSingle(_cache, address.Start - _start.Start), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadValue(DeviceAddress address) - { - throw new NotImplementedException(); - } - - public int WriteBit(DeviceAddress address, bool bit) - { - byte b = _cache[address.Start - _start.Start]; - b |= (byte)(1 << address.Bit); - return 0; - } - - public int WriteBits(DeviceAddress address, byte bits) - { - _cache[address.Start - _start.Start] = bits; - return 0; - } - - public int WriteInt16(DeviceAddress address, short value) - { - int index = address.Start - _start.Start; - fixed (byte* p1 = _cache) - { - byte* numPtr = p1 + index; - if ((((int)numPtr) & 1) == 0) - { - *((short*)numPtr) = value; - } - else - { - byte* numPtr2 = (byte*)&value; - numPtr[0] = numPtr2[0]; - numPtr[1] = numPtr2[1]; - } - } - return 0; - } - - public int WriteInt32(DeviceAddress address, int value) - { - int index = address.Start - _start.Start; - fixed (byte* p1 = _cache) - { - byte* numPtr = p1 + index; - if ((((int)numPtr) & 3) == 0) - { - *((int*)numPtr) = value; - } - else - { - byte* numPtr2 = (byte*)&value; - numPtr[0] = numPtr2[0]; - numPtr[1] = numPtr2[1]; - numPtr[2] = numPtr2[2]; - numPtr[3] = numPtr2[3]; - } - } - return 0; - } - - public int WriteFloat(DeviceAddress address, float value) - { - return 0; - } - - public int WriteString(DeviceAddress address, string str) - { - byte[] b = System.Text.Encoding.Default.GetBytes(str); - int index = address.Start - _start.Start; - Array.Copy(_cache, index, b, 0, 64); - return 0; - } - - public int WriteValue(DeviceAddress address, object value) - { - throw new NotImplementedException(); - } - - public byte[] ReadBytes(DeviceAddress address, ushort size) - { - byte[] bytes = new byte[size]; - Array.Copy(_cache, address.Start - _start.Start, bytes, 0, size); - return bytes; - } - - public int WriteBytes(DeviceAddress address, byte[] bit) - { - if (bit != null && bit.Length > 0) - { - Array.Copy(bit, 0, _cache, address.Start - _start.Start, bit.Length); - return 1; - } - return -1; - } - - } - - public unsafe class ModbusCacheReader : ICache - { - byte[] _cache; - public byte[] Cache - { - get - { - return _cache; - } - } - - DeviceAddress _start; - public DeviceAddress Start - { - get - { - return _start; - } - set - { - _start = value; - } - } - - int _size; - public int Size - { - get - { - return _size; - } - set - { - _size = value; - this._cache = new byte[_start.Area > 2 ? 2 * _size : _size]; - } - } - - public ModbusCacheReader() - { - } - - public ModbusCacheReader(DeviceAddress start, int size) - { - this.Start = start; - this.Size = size; - } - - public int GetOffset(DeviceAddress start, DeviceAddress end) - { - return start.Area > 2 ? (start.Start - end.Start) << 1 : start.Start - end.Start; - } - - public ItemData ReadInt32(DeviceAddress address) - { - return new ItemData(BitConverter.ToInt32(_cache, GetOffset(address, _start)) - , 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadBit(DeviceAddress address) - { - fixed (byte* p = _cache) - { - int* p1 = (int*)(p + GetOffset(address, _start)); - return new ItemData((*p1 & (1 << address.Bit)) != 0, 0, QUALITIES.QUALITY_GOOD); - } - } - - public ItemData ReadInt16(DeviceAddress address) - { - return new ItemData(BitConverter.ToInt16(_cache, GetOffset(address, _start)), - 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadByte(DeviceAddress address) - { - return new ItemData(_cache[GetOffset(address, _start)], 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadString(DeviceAddress address, ushort size = 64) - { - return new ItemData(BitConverter.ToString(_cache, GetOffset(address, _start), size), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadFloat(DeviceAddress address) - { - int value = BitConverter.ToInt32(_cache, GetOffset(address, _start)); - //value = IPAddress.HostToNetworkOrder(value); - return new ItemData(*(((float*)&value)), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadValue(DeviceAddress address) - { - throw new NotImplementedException(); - } - - public int WriteBit(DeviceAddress address, bool bit) - { - throw new NotImplementedException(); - } - - public int WriteBits(DeviceAddress address, byte bits) - { - throw new NotImplementedException(); - } - - public int WriteInt16(DeviceAddress address, short value) - { - throw new NotImplementedException(); - } - - public int WriteInt32(DeviceAddress address, int value) - { - throw new NotImplementedException(); - } - - public int WriteFloat(DeviceAddress address, float value) - { - throw new NotImplementedException(); - } - - public int WriteString(DeviceAddress address, string str) - { - throw new NotImplementedException(); - } - - public int WriteValue(DeviceAddress address, object value) - { - throw new NotImplementedException(); - } - - public byte[] ReadBytes(DeviceAddress address, ushort size) - { - throw new NotImplementedException(); - } - - public int WriteBytes(DeviceAddress address, byte[] bit) - { - throw new NotImplementedException(); - } - } - - - public unsafe class MXCacheReader : ICache - { - byte[] _cache; - public byte[] Cache - { - get - { - return _cache; - } - } - - DeviceAddress _start; - public DeviceAddress Start - { - get - { - return _start; - } - set - { - _start = value; - } - } - - int _size; - public int Size - { - get - { - return _size; - } - set - { - _size = value; - this._cache = new byte[_size]; - } - } - - public MXCacheReader() - { - } - - public MXCacheReader(DeviceAddress start, int size) - { - this.Start = start; - this.Size = size; - } - - static int GetOffset(DeviceAddress addr1) - { - switch (addr1.Area) - { - case MX_AREA.T: - case MX_AREA.D: - return addr1.Area + 2 * addr1.Start; - case MX_AREA.S: - case MX_AREA.M: - case MX_AREA.X: - case MX_AREA.Y: - return addr1.Area + (addr1.Start >> 3); - default: - return 0; - } - } - - public int GetOffset(DeviceAddress start, DeviceAddress end) - { - return GetOffset(start) - GetOffset(end); - } - - public ItemData ReadInt32(DeviceAddress address) - { - return new ItemData(BitConverter.ToInt32(_cache, GetOffset(address, _start)), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadBit(DeviceAddress address) - { - //if (address.Area == MX_AREA.D || address.Area == MX_AREA.T) - // return new ItemData(false, 0, QUALITIES.QUALITY_BAD); - fixed (byte* p = _cache) - { - short* p1 = (short*)(p + GetOffset(address, _start)); - return new ItemData((*p1 & (1 << address.Bit)) != 0, 0, QUALITIES.QUALITY_GOOD); - } - } - - public ItemData ReadInt16(DeviceAddress address) - { - return new ItemData(BitConverter.ToInt16(_cache, GetOffset(address, _start)), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadByte(DeviceAddress address) - { - return new ItemData(_cache[GetOffset(address, _start)], 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadString(DeviceAddress address, ushort size = 64) - { - byte[] bytes = new byte[size]; - Array.Copy(_cache, GetOffset(address, _start), bytes, 0, size); - return new ItemData(Encoding.Default.GetString(bytes).Trim((char)0), 0, QUALITIES.QUALITY_GOOD); - //return new ItemData(BitConverter.ToString(_cache, GetOffset(address, _start), size>>3), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadFloat(DeviceAddress address) - { - return new ItemData(BitConverter.ToSingle(_cache, GetOffset(address, _start)), 0, QUALITIES.QUALITY_GOOD); - } - - public ItemData ReadValue(DeviceAddress address) - { - throw new NotImplementedException(); - } - - public int WriteBit(DeviceAddress address, bool bit) - { - throw new NotImplementedException(); - } - - public int WriteBits(DeviceAddress address, byte bits) - { - throw new NotImplementedException(); - } - - public int WriteInt16(DeviceAddress address, short value) - { - throw new NotImplementedException(); - } - - public int WriteInt32(DeviceAddress address, int value) - { - throw new NotImplementedException(); - } - - public int WriteFloat(DeviceAddress address, float value) - { - throw new NotImplementedException(); - } - - public int WriteString(DeviceAddress address, string str) - { - throw new NotImplementedException(); - } - - public int WriteValue(DeviceAddress address, object value) - { - throw new NotImplementedException(); - } - - public byte[] ReadBytes(DeviceAddress address, ushort size) - { - throw new NotImplementedException(); - } - - public int WriteBytes(DeviceAddress address, byte[] bit) - { - throw new NotImplementedException(); - } - - } -} diff --git a/SCADA/Program/DataService/obj/Debug/DataService.csproj.FileListAbsolute.txt b/SCADA/Program/DataService/obj/Debug/DataService.csproj.FileListAbsolute.txt index 00493a7..b499287 100644 --- a/SCADA/Program/DataService/obj/Debug/DataService.csproj.FileListAbsolute.txt +++ b/SCADA/Program/DataService/obj/Debug/DataService.csproj.FileListAbsolute.txt @@ -22,3 +22,4 @@ C:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\DataService\bin\Debug\D C:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\DataService\bin\Debug\DataService.pdb C:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\DataService\obj\Debug\DataService.dll C:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\DataService\obj\Debug\DataService.pdb +C:\Users\Yinan\Documents\Github\SharpSCADA\SCADA\Program\DataService\obj\Debug\DataService.csprojResolveAssemblyReference.cache diff --git a/SCADA/Program/DataService/obj/Debug/DataService.dll b/SCADA/Program/DataService/obj/Debug/DataService.dll index c158fd8..ee1ce68 100644 Binary files a/SCADA/Program/DataService/obj/Debug/DataService.dll and b/SCADA/Program/DataService/obj/Debug/DataService.dll differ diff --git a/SCADA/Program/ModbusDriver/ModbusTCPDriver.cs b/SCADA/Program/ModbusDriver/ModbusTCPDriver.cs index 5cdb07b..9f9a234 100644 --- a/SCADA/Program/ModbusDriver/ModbusTCPDriver.cs +++ b/SCADA/Program/ModbusDriver/ModbusTCPDriver.cs @@ -56,6 +56,16 @@ namespace ModbusDriver set { _timeout = value; } } + byte _slaveId;//设备ID 单元号 字节号 + /// + /// 设备ID 单元号 字节号 + /// + public byte SlaveId + { + get { return _slaveId; } + set { _slaveId = value; } + } + List _grps = new List(20); public IEnumerable Groups { @@ -75,6 +85,7 @@ namespace ModbusDriver _server = server; _ip = ip; _timeout = timeOut; + byte.TryParse(spare1, out _slaveId); } public bool Connect() @@ -105,12 +116,10 @@ namespace ModbusDriver private byte[] CreateReadHeader(int id, int startAddress, ushort length, byte function) { byte[] data = new byte[12]; - - byte[] _id = BitConverter.GetBytes((short)id); - data[0] = _id[0]; // Slave id high byte - data[1] = _id[1]; // Slave id low byte + data[0] = 0; // Slave id high byte + data[1] = 0; // Slave id low byte data[5] = 6; // Message size - data[6] = 0; // Slave address + data[6] = (byte)id; // Slave address data[7] = function; // Function code byte[] _adr = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)startAddress)); data[8] = _adr[0]; // Start address @@ -124,14 +133,12 @@ namespace ModbusDriver private byte[] CreateWriteHeader(int id, int startAddress, ushort numData, ushort numBytes, byte function) { byte[] data = new byte[numBytes + 11]; - - byte[] _id = BitConverter.GetBytes(id); - data[0] = _id[0]; // Slave id high byte - data[1] = _id[1]; // Slave id low byte+ + data[0] = 0; // Slave id high byte + data[1] = 0; // Slave id low byte+ byte[] _size = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)(5 + numBytes))); data[4] = _size[0]; // Complete message size in bytes data[5] = _size[1]; // Complete message size in bytes - data[6] = 0; // Slave address + data[6] = (byte)id; // Slave address data[7] = function; // Function code byte[] _adr = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)startAddress)); data[8] = _adr[0]; // Start address @@ -243,11 +250,12 @@ namespace ModbusDriver DeviceAddress dv = DeviceAddress.Empty; if (string.IsNullOrEmpty(address)) return dv; + dv.Area = _slaveId; switch (address[0]) { case '0': { - dv.Area = Modbus.fctReadCoil; + dv.DBNumber = Modbus.fctReadCoil; int st; int.TryParse(address, out st); //dv.Start = (st / 16) * 16;//??????????????????? @@ -258,7 +266,7 @@ namespace ModbusDriver break; case '1': { - dv.Area = Modbus.fctReadDiscreteInputs; + dv.DBNumber = Modbus.fctReadDiscreteInputs; int st; int.TryParse(address.Substring(1), out st); //dv.Start = (st / 16) * 16;//??????????????????? @@ -270,7 +278,7 @@ namespace ModbusDriver case '4': { int index = address.IndexOf('.'); - dv.Area = Modbus.fctReadHoldingRegister; + dv.DBNumber = Modbus.fctReadHoldingRegister; if (index > 0) { dv.Start = int.Parse(address.Substring(1, index - 1)); @@ -284,7 +292,7 @@ namespace ModbusDriver case '3': { int index = address.IndexOf('.'); - dv.Area = Modbus.fctReadInputRegister; + dv.DBNumber = Modbus.fctReadInputRegister; if (index > 0) { dv.Start = int.Parse(address.Substring(1, index - 1)); @@ -381,14 +389,14 @@ namespace ModbusDriver public byte[] ReadBytes(DeviceAddress address, ushort size) { - int area = address.Area; - return area < 2 ? WriteSyncData(CreateReadHeader(area, address.Start * 16, (ushort)(16 * size), (byte)area)) - : WriteSyncData(CreateReadHeader(area, address.Start, size, (byte)area)); + int area = address.DBNumber; + return area < 2 ? WriteSyncData(CreateReadHeader(address.Area, address.Start * 16, (ushort)(16 * size), (byte)area)) + : WriteSyncData(CreateReadHeader(address.Area, address.Start, size, (byte)area)); } public ItemData ReadInt32(DeviceAddress address) { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 2, (byte)address.Area)); + byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 2, (byte)address.DBNumber)); if (data == null) return new ItemData(0, 0, QUALITIES.QUALITY_BAD); else @@ -397,7 +405,7 @@ namespace ModbusDriver public ItemData ReadInt16(DeviceAddress address) { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 1, (byte)address.Area)); + byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 1, (byte)address.DBNumber)); if (data == null) return new ItemData(0, 0, QUALITIES.QUALITY_BAD); else @@ -406,7 +414,7 @@ namespace ModbusDriver public ItemData ReadByte(DeviceAddress address) { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 1, (byte)address.Area)); + byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 1, (byte)address.DBNumber)); if (data == null) return new ItemData(0, 0, QUALITIES.QUALITY_BAD); else @@ -415,7 +423,7 @@ namespace ModbusDriver public ItemData ReadString(DeviceAddress address, ushort size) { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, size, (byte)address.Area)); + byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, size, (byte)address.DBNumber)); if (data == null) return new ItemData(string.Empty, 0, QUALITIES.QUALITY_BAD); else @@ -424,7 +432,7 @@ namespace ModbusDriver public unsafe ItemData ReadFloat(DeviceAddress address) { - byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 2, (byte)address.Area)); + byte[] data = WriteSyncData(CreateReadHeader(address.Area, address.Start, 2, (byte)address.DBNumber)); if (data == null) return new ItemData(0.0f, 0, QUALITIES.QUALITY_BAD); else @@ -436,10 +444,11 @@ namespace ModbusDriver public ItemData ReadBit(DeviceAddress address) { - byte[] data = address.Area > 2 ? WriteSyncData(CreateReadHeader(address.Area, address.Start, 1, (byte)address.Area)) : - WriteSyncData(CreateReadHeader(address.Area, address.Start + address.Bit, 1, (byte)address.Area)); + byte[] data = address.DBNumber > 2 ? WriteSyncData(CreateReadHeader(address.Area, address.Start, 1, (byte)address.DBNumber)) : + WriteSyncData(CreateReadHeader(address.Area, address.Start * 16 + address.Bit, 1, (byte)address.DBNumber)); if (data == null) return new ItemData(false, 0, QUALITIES.QUALITY_BAD); + if (data.Length == 1) return new ItemData(data[0] > 0, 0, QUALITIES.QUALITY_GOOD); unsafe { fixed (byte* p = data) @@ -458,14 +467,14 @@ namespace ModbusDriver public int WriteBytes(DeviceAddress address, byte[] bit) { - var data = address.Area > 2 ? WriteMultipleRegister(address.Area, address.Start, bit) + var data = address.DBNumber > 2 ? WriteMultipleRegister(address.Area, address.Start, bit) : WriteMultipleCoils(address.Area, address.Start, (ushort)(8 * bit.Length), bit);//应考虑到 return data == null ? -1 : 0; } public int WriteBit(DeviceAddress address, bool bit) { - if (address.Area < 3) + if (address.DBNumber < 3) { var data = WriteSingleCoils(address.Area, address.Start + address.Bit, bit); return data == null ? -1 : 0; @@ -574,7 +583,7 @@ namespace ModbusDriver { while (addr.Start == next.Start) { - if ((tmp & (1 << next.Bit.BitSwap())) > 0) _changedList.Add(index); + if ((tmp & (1 << next.Bit)) > 0) _changedList.Add(index); if (++index < count) next = _items[index].Address; else diff --git a/SCADA/Program/ModbusDriver/bin/Debug/DataService.dll b/SCADA/Program/ModbusDriver/bin/Debug/DataService.dll index c158fd8..e33eb42 100644 Binary files a/SCADA/Program/ModbusDriver/bin/Debug/DataService.dll and b/SCADA/Program/ModbusDriver/bin/Debug/DataService.dll differ diff --git a/SCADA/Program/ModbusDriver/bin/Debug/ModbusDriver.dll b/SCADA/Program/ModbusDriver/bin/Debug/ModbusDriver.dll index 4848de0..c22e713 100644 Binary files a/SCADA/Program/ModbusDriver/bin/Debug/ModbusDriver.dll and b/SCADA/Program/ModbusDriver/bin/Debug/ModbusDriver.dll differ diff --git a/SCADA/Program/ModbusDriver/obj/Debug/ModbusDriver.dll b/SCADA/Program/ModbusDriver/obj/Debug/ModbusDriver.dll index 4848de0..c22e713 100644 Binary files a/SCADA/Program/ModbusDriver/obj/Debug/ModbusDriver.dll and b/SCADA/Program/ModbusDriver/obj/Debug/ModbusDriver.dll differ diff --git a/SCADA/dll/ModbusDriver.dll b/SCADA/dll/ModbusDriver.dll index 87f64a8..c22e713 100644 Binary files a/SCADA/dll/ModbusDriver.dll and b/SCADA/dll/ModbusDriver.dll differ