refactor: 将Framework库和RT、UI放在同一个解决方案中。
This commit is contained in:
@@ -0,0 +1,378 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MECF.Framework.RT.Core.IoProviders
|
||||
{
|
||||
public partial class MCProtocol
|
||||
{
|
||||
////
|
||||
///
|
||||
///
|
||||
/// Melsec Protocol QnA compatible 3E frame Constant and Message Format Definition
|
||||
///
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
public const int CMD_BATCH_READ = 0x0401;
|
||||
public const int CMD_BATCH_WRITE = 0x1401;
|
||||
public const int CMD_RANDOM_READ = 0x0403;
|
||||
public const int CMD_RANDOM_WRITE = 0x1402;
|
||||
public const int SUB_CMD_WORD_UNITS = 0;
|
||||
public const int SUB_CMD_BYTE_UNITS = 1;
|
||||
|
||||
public static readonly int JUNK_SIZE = Marshal.SizeOf(typeof(ushort)); // RESERVED
|
||||
|
||||
static public readonly int PLCDataSize = Marshal.SizeOf(typeof(ushort));
|
||||
static public readonly int PLCDataBits = PLCDataSize * 8;
|
||||
static public readonly int BufDataSize = Marshal.SizeOf(typeof(byte));
|
||||
static public readonly int BufDataBits = BufDataSize * 8;
|
||||
static public readonly int IntSize = Marshal.SizeOf(typeof(int));
|
||||
static public readonly int IntBits = IntSize * 8;
|
||||
|
||||
static public readonly int ShortSize = Marshal.SizeOf(typeof(ushort));
|
||||
|
||||
// List of Commands and Functions for the QnA Compatible 3E Frame Device Memory Read/Write
|
||||
// refer to manual 3-62
|
||||
public const ushort MC_COMMAND_MULTIPLE_BATCH_READ = 0x0406;
|
||||
public const ushort MC_COMMAND_MULTIPLE_BATCH_WRITE = 0x1406;
|
||||
public const ushort MC_COMMAND_BATCH_READ = 0x0401;
|
||||
public const ushort MC_COMMAND_BATCH_WRITE = 0x1401;
|
||||
public const ushort MC_COMMAND_RANDOM_READ = 0x0403;
|
||||
public const ushort MC_COMMAND_RANDOM_WRITE = 0x1402;
|
||||
public const ushort MC_COMMAND_MONITOR_DATA_REGISTRATION = 0x0801;
|
||||
public const ushort MC_COMMAND_MONITOR = 0x0802;
|
||||
|
||||
public const ushort MC_SUBCOMMAND_BIT_UNITS = 0x0001;
|
||||
public const ushort MC_SUBCOMMAND_WORD_UNITS = 0x0000;
|
||||
|
||||
// List of Device code for QnACPU
|
||||
// refer to manual 3-70
|
||||
public const byte MC_DEVICE_CODE_INPUT_BIT = 0x9C;
|
||||
public const byte MC_DEVICE_CODE_OUTPUT_BIT = 0x0D;
|
||||
public const byte MC_DEVICE_CODE_INTERNAL_RELAY_BIT = 0x90;
|
||||
public const byte MC_DEVICE_CODE_LATCH_RELAY_BIT = 0x92;
|
||||
public const byte MC_DEVICE_CODE_ANNUNCIATOR_BIT = 0x93;
|
||||
public const byte MC_DEVICE_CODE_EDGE_RELAY_BIT = 0x94;
|
||||
public const byte MC_DEVICE_CODE_LINK_RELAY_BIT = 0xA0;
|
||||
public const byte MC_DEVICE_CODE_DATA_REGISTER_WORD = 0xA8;
|
||||
public const byte MC_DEVICE_CODE_LINK_REGISTER_WORD = 0xB4;
|
||||
|
||||
// refer to manual 3-4
|
||||
public const ushort MC_SUBHEADER_COMMAND_MESSAGE = 0x0050;
|
||||
public const ushort MC_SUBHEADER_RESPONSE_MESSAGE = 0x00D0;
|
||||
|
||||
// refer to manual 3-14
|
||||
public const ushort MC_REQUEST_MODULE_IO_NUMBER = 0x03FF;
|
||||
public const byte MC_REQUEST_MODULE_STATION_NUMBER = 0x00;
|
||||
public static readonly ushort MC_CPU_MONITOR_TIMER = 0x0008; // Wait time (Unit: 250ms)
|
||||
public const ushort MC_COMPLETE_CODE_SUCCESS = 0x0000;
|
||||
|
||||
// User Defined Constant
|
||||
public static readonly ushort MC_NONE_DESIGNATE_DATA_LENGTH = 0x0000;
|
||||
public static readonly int MC_SUBHEADER_SIZE = Marshal.SizeOf(MC_SUBHEADER_COMMAND_MESSAGE);
|
||||
public static readonly int MC_QHEADER_COMMAND_SIZE = Marshal.SizeOf(typeof(MC_COMMAND_HEADER));
|
||||
public static readonly int MC_QHEADER_RESPONSE_SIZE = Marshal.SizeOf(typeof(MC_RESPONSE_HEADER));
|
||||
public static readonly int MC_BATCH_COMMAND_SIZE = Marshal.SizeOf(typeof(MC_BATCH_COMMAND));
|
||||
//public static readonly int MC_POINT_BITS = sizeof(ushort) * 8;
|
||||
//public const int MC_CA_WRITE_SIZE = 1024;
|
||||
//public const int MC_COMMAND_DATA_SIZE = 1024;
|
||||
//public const int MC_IO_BUFFER_LENGTH = 1024;
|
||||
// Command Message Format
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_DATA_FORMAT
|
||||
//{
|
||||
// public MC_HEADER mc_Header;
|
||||
// public MC_APPLICATION_DATA mc_ApplicationData;
|
||||
//};
|
||||
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_HEADER // Ethernet module adds and deletes the header
|
||||
//{
|
||||
//};
|
||||
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_APPLICATION_DATA
|
||||
//{
|
||||
// public MC_SUBHEADER mc_SubHeader;
|
||||
// public MC_TEXT mc_Text;
|
||||
//};
|
||||
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_SUBHEADER // 2 bytes (3E frame)
|
||||
//{
|
||||
// public enum SUBHEADER_TYPE : ushort
|
||||
// {
|
||||
// MC_COMMAND_MESSAGE = 0x0050,
|
||||
// MC_RESPONSE_MESSAGE = 0x00D0
|
||||
// };
|
||||
|
||||
// public SUBHEADER_TYPE mc_SubHeaderType;
|
||||
//}
|
||||
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_TEXT // Text Command or Text Response
|
||||
//{
|
||||
//}
|
||||
|
||||
//public const byte MC_LOCAL_STATION_MCSocket_NUMBER = 0x00;
|
||||
//public const byte MC_LOCAL_STATION_PC_NUMBER = 0xFF;
|
||||
|
||||
// refer to manual 3-7
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_QHEADER_COMMAND
|
||||
//{
|
||||
// public byte nMCSocketNo;
|
||||
// public byte nStationNo;
|
||||
// public ushort nRequestIONumber;
|
||||
// public byte nRequestStationNumber;
|
||||
// public ushort nRequestDataLength;
|
||||
// public ushort nCPUMonitorTimer;
|
||||
//}
|
||||
|
||||
[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public struct MC_COMMAND_HEADER
|
||||
{
|
||||
public ushort ProtocolID; // MC_COMMAND_MESSAGE or MC_RESPONSE_MESSAGE
|
||||
public byte NetworkID;
|
||||
public byte StationID;
|
||||
public ushort RequestIONumber;
|
||||
public byte RequestStationNumber;
|
||||
public ushort RequestDataLen;
|
||||
public ushort CPUMonitorTimer;
|
||||
};// MC_COMMAND_HEADER;
|
||||
|
||||
[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public struct MC_RESPONSE_HEADER
|
||||
{
|
||||
public ushort ProtocolId; //nSubheader
|
||||
public byte NetworkId; //SocketNumber
|
||||
public byte StationId;
|
||||
public ushort RequestIoNumber;
|
||||
public byte RequestStationNumber;
|
||||
public ushort ResponseDataLen;
|
||||
public ushort CompleteCode;
|
||||
};
|
||||
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_QHEADER_RESPONSE
|
||||
//{
|
||||
// public ushort nSubHeader;
|
||||
// public byte nMCSocketNo;
|
||||
// public byte nStationNo;
|
||||
// public ushort nRequestIONumber;
|
||||
// public byte nRequestStationNumber;
|
||||
// public ushort nResponseDataLength;
|
||||
// public ushort nCompleteCode;
|
||||
//}
|
||||
|
||||
[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public struct MC_BATCH_COMMAND
|
||||
{
|
||||
public ushort Command;
|
||||
public ushort SubCommand;
|
||||
public ushort HeadAddr;
|
||||
public byte Reserved;
|
||||
public byte DeviceCode;
|
||||
public ushort DevicePoints;
|
||||
}
|
||||
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_HEAD_DEVICE
|
||||
//{
|
||||
// public byte bLow;
|
||||
// public byte bMid;
|
||||
// public byte bHig;
|
||||
|
||||
// public int HeadDevice
|
||||
// {
|
||||
// get
|
||||
// {
|
||||
// return (bLow | bMid << sizeof(byte) | bHig << sizeof(byte) * 2);
|
||||
// }
|
||||
// set
|
||||
// {
|
||||
// bLow = (byte)(0x00 | value);
|
||||
// bMid = (byte)(0x00 | value >> sizeof(byte));
|
||||
// bHig = (byte)(0x00 | value >> sizeof(byte) * 2);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
//public struct MC_CA_RANDOM_WRITE_BIT
|
||||
//{
|
||||
// public ushort nCommand;
|
||||
// public ushort nSubCommand;
|
||||
// public byte nBitPoints;
|
||||
|
||||
// public DATA_RANDOM_WRITE_BIT[] data;
|
||||
|
||||
// [Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
// public struct DATA_RANDOM_WRITE_BIT
|
||||
// {
|
||||
// public ushort nHeadDeviceAddr;
|
||||
// public byte nReserved;
|
||||
// public byte nDeviceCode;
|
||||
// public byte nSet;
|
||||
// }
|
||||
|
||||
// byte[] ToByteArray() // can serialize with pointer type?
|
||||
// {
|
||||
// byte[] buffer = new byte[0];
|
||||
|
||||
// return buffer;
|
||||
// }
|
||||
//};
|
||||
|
||||
|
||||
|
||||
public static byte[] TransBoolArrayToByteData(bool[] value)
|
||||
{
|
||||
int length = (value.Length + 1) / 2;
|
||||
byte[] buffer = new byte[length];
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (value[i * 2 + 0])
|
||||
buffer[i] += 0x10;
|
||||
if ((i * 2 + 1) < value.Length)
|
||||
{
|
||||
if (value[i * 2 + 1])
|
||||
buffer[i] += 0x01;
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static bool[] TransByteDataToBoolArray(byte[] value, int offset, int size)
|
||||
{
|
||||
int count = size * 2;
|
||||
bool[] result = new bool[count];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if ((value[i] & 0x10) == 0x10)
|
||||
{
|
||||
result[i * 2 + 0] = true;
|
||||
}
|
||||
|
||||
if ((value[i] & 0x01) == 0x01)
|
||||
{
|
||||
result[i * 2 + 1] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static T ToStruct<T>(byte[] by) where T : struct
|
||||
{
|
||||
int objectSize = Marshal.SizeOf(typeof(T));
|
||||
if (objectSize > by.Length) return default(T);
|
||||
|
||||
// Allocate some unmanaged memory.
|
||||
IntPtr buffer = Marshal.AllocHGlobal(objectSize);
|
||||
|
||||
// Copy the read byte array (byte[]) into the unmanaged memory block.
|
||||
Marshal.Copy(by, 0, buffer, objectSize);
|
||||
|
||||
// Push the memory into a new struct of type (T).
|
||||
T returnStruct = (T)Marshal.PtrToStructure(buffer, typeof(T));
|
||||
|
||||
// Free the unmanaged memory block.
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
|
||||
return returnStruct;
|
||||
}
|
||||
|
||||
public static object ToStruct(byte[] buffer, Type t)
|
||||
{
|
||||
int objectSize = Marshal.SizeOf(t);
|
||||
if (objectSize > buffer.Length) return null;
|
||||
|
||||
// Allocate some unmanaged memory.
|
||||
IntPtr buf = Marshal.AllocHGlobal(objectSize);
|
||||
|
||||
// Copy the read byte array (byte[]) into the unmanaged memory block.
|
||||
Marshal.Copy(buffer, 0, buf, objectSize);
|
||||
|
||||
// Push the memory into a new struct of type (T).
|
||||
object result = Marshal.PtrToStructure(buf, t);
|
||||
|
||||
// Free the unmanaged memory block.
|
||||
Marshal.FreeHGlobal(buf);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] Struct2Bytes(object o)
|
||||
{
|
||||
// create a new byte buffer the size of your struct
|
||||
byte[] buffer = new byte[Marshal.SizeOf(o)];
|
||||
// pin the buffer so we can copy data into it w/o GC collecting it
|
||||
GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
||||
// copy the struct data into the buffer
|
||||
Marshal.StructureToPtr(o, bufferHandle.AddrOfPinnedObject(), false);
|
||||
// free the GC handle
|
||||
bufferHandle.Free();
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static byte[] Ushort2Byte(ushort[] data)
|
||||
{
|
||||
int sizeofT1 = Marshal.SizeOf(typeof(ushort));
|
||||
int sizeofT2 = Marshal.SizeOf(typeof(byte));
|
||||
|
||||
byte[] by = new byte[sizeofT1 * data.Length / sizeofT2];
|
||||
for (int i = data.Length; i-- > 0;)
|
||||
{
|
||||
BitConverter.GetBytes(data[i]).CopyTo(by, i * 2);
|
||||
}
|
||||
return by;
|
||||
}
|
||||
public static byte[] Ushort2Byte(ushort[] data, int offset, int length)
|
||||
{
|
||||
int sizeofT1 = Marshal.SizeOf(typeof(ushort));
|
||||
int sizeofT2 = Marshal.SizeOf(typeof(byte));
|
||||
|
||||
byte[] by = new byte[sizeofT1 * length / sizeofT2];
|
||||
|
||||
for (int i = offset+length; i-- > offset;)
|
||||
{
|
||||
var value = BitConverter.GetBytes(data[i]);
|
||||
value.CopyTo(by, (i-offset) * 2);
|
||||
}
|
||||
return by;
|
||||
}
|
||||
public static ushort[] Byte2Ushort(byte[] data)
|
||||
{
|
||||
return Byte2Ushort(data, 0, data.Length);
|
||||
}
|
||||
|
||||
public static ushort[] Byte2Ushort(byte[] data, int offset, int length)
|
||||
{
|
||||
System.Diagnostics.Debug.Assert(data.Length % 2 == 0);
|
||||
|
||||
int sizeofT1 = Marshal.SizeOf(typeof(byte));
|
||||
int sizeofT2 = Marshal.SizeOf(typeof(ushort));
|
||||
|
||||
ushort[] us = new ushort[sizeofT1 * length / sizeofT2];
|
||||
for (int i = 0; i < us.Length; i++)
|
||||
{
|
||||
us[i] = BitConverter.ToUInt16(data, offset + i * 2);
|
||||
}
|
||||
return us;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,497 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using Aitex.Core.RT.DataCenter;
|
||||
using Aitex.Core.RT.Log;
|
||||
using Aitex.Core.Util;
|
||||
using MECF.Framework.Common.Communications;
|
||||
|
||||
namespace MECF.Framework.RT.Core.IoProviders
|
||||
{
|
||||
public class MCProtocolPlc : IoProvider,IConnection
|
||||
{
|
||||
public string Address
|
||||
{
|
||||
get { return $"{_ip}:{_port}"; }
|
||||
}
|
||||
|
||||
public bool IsConnected
|
||||
{
|
||||
get { return IsOpened; }
|
||||
}
|
||||
|
||||
public bool Connect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Disconnect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private string _ip = "127.0.0.1";
|
||||
private string _localIp = "127.0.0.1";
|
||||
private int _port = 6731;
|
||||
private int _socketId = 101;
|
||||
private int _stationId = 102;
|
||||
|
||||
private byte[] _bufferIn;
|
||||
private byte[] _bufferOut;
|
||||
private int _bufferSize = 2048;
|
||||
|
||||
private MCProtocol.MC_COMMAND_HEADER _header;
|
||||
//private MCProtocol.MC_RESPONSE_HEADER _response;
|
||||
|
||||
private MCProtocol.MC_BATCH_COMMAND _batchCommand;
|
||||
|
||||
private MCSocket _socket;
|
||||
private int perIoLength = 960;
|
||||
|
||||
protected override void SetParameter(XmlElement nodeParameter)
|
||||
{
|
||||
string strIp = nodeParameter.GetAttribute("ip");
|
||||
string strPort = nodeParameter.GetAttribute("port");
|
||||
string networkId = nodeParameter.GetAttribute("network_id");
|
||||
string stationId = nodeParameter.GetAttribute("station_id");
|
||||
if(!string.IsNullOrEmpty(nodeParameter.GetAttribute("buffer_size")))
|
||||
{
|
||||
int.TryParse(nodeParameter.GetAttribute("buffer_size"), out _bufferSize);
|
||||
}
|
||||
|
||||
_port = int.Parse(strPort);
|
||||
_ip = strIp;
|
||||
_socketId = int.Parse(networkId);
|
||||
_stationId = int.Parse(stationId);
|
||||
|
||||
ConnectionManager.Instance.Subscribe(Name, this);
|
||||
DATA.Subscribe($"{Module}.{Name}.IsConnected", () => _socket == null ? false : _socket.Connected);
|
||||
}
|
||||
|
||||
protected override void Open()
|
||||
{
|
||||
_socket = new MCSocket();
|
||||
|
||||
_header = new MCProtocol.MC_COMMAND_HEADER()
|
||||
{
|
||||
ProtocolID = MCProtocol.MC_SUBHEADER_COMMAND_MESSAGE,
|
||||
NetworkID = (byte)_socketId,
|
||||
StationID = (byte)_stationId,
|
||||
RequestIONumber = MCProtocol.MC_REQUEST_MODULE_IO_NUMBER,
|
||||
RequestStationNumber = MCProtocol.MC_REQUEST_MODULE_STATION_NUMBER,
|
||||
RequestDataLen = 0,
|
||||
CPUMonitorTimer = (ushort)(MCProtocol.MC_CPU_MONITOR_TIMER * 2),
|
||||
};
|
||||
|
||||
|
||||
_batchCommand = new MCProtocol.MC_BATCH_COMMAND()
|
||||
{
|
||||
Command = MCProtocol.MC_COMMAND_BATCH_READ,
|
||||
DeviceCode = MCProtocol.MC_DEVICE_CODE_DATA_REGISTER_WORD,
|
||||
DevicePoints = 0,
|
||||
HeadAddr = 0,
|
||||
Reserved = 0,
|
||||
SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS,
|
||||
};
|
||||
|
||||
_bufferOut = new byte[_bufferSize];
|
||||
_bufferIn = new byte[_bufferSize];
|
||||
|
||||
if(_socket.Open(_ip, _port, _localIp))
|
||||
SetState(IoProviderStateEnum.Opened);
|
||||
}
|
||||
|
||||
protected override void Close()
|
||||
{
|
||||
_socket.Close();
|
||||
SetState(IoProviderStateEnum.Closed);
|
||||
}
|
||||
|
||||
Random _rd = new Random();
|
||||
protected override bool[] ReadDi(int offset, int size)
|
||||
{
|
||||
if(_socket == null || !_socket.Connected)
|
||||
{
|
||||
State = IoProviderStateEnum.Error;
|
||||
return null;
|
||||
}
|
||||
bool[] buff = new bool[size];
|
||||
|
||||
int count = size / perIoLength;
|
||||
if (count < 1)
|
||||
{
|
||||
bool[] dibuffer = DoReadDi(offset, size);
|
||||
if (dibuffer != null)
|
||||
Array.Copy(dibuffer, 0, buff, 0, dibuffer.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool[] dibuffer;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
dibuffer = DoReadDi(i * perIoLength + offset, perIoLength);
|
||||
if (dibuffer != null)
|
||||
Array.Copy(dibuffer, 0, buff, i * perIoLength, dibuffer.Length);
|
||||
}
|
||||
|
||||
if (size % perIoLength != 0)
|
||||
{
|
||||
dibuffer = DoReadDi(offset + perIoLength * count, size % perIoLength);
|
||||
if (dibuffer != null)
|
||||
Array.Copy(dibuffer, 0, buff, size - size % perIoLength, dibuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
return buff;
|
||||
}
|
||||
protected bool[] DoReadDi(int offset, int size)
|
||||
{
|
||||
|
||||
_batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
|
||||
_batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_BIT_UNITS;
|
||||
|
||||
_batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
|
||||
_batchCommand.Reserved = (byte)(offset >> 16);
|
||||
|
||||
_batchCommand.DevicePoints = (ushort)size;
|
||||
|
||||
_header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
|
||||
|
||||
byte[] buffer = MCProtocol.Struct2Bytes(_header);
|
||||
byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
|
||||
|
||||
Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
|
||||
Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
|
||||
|
||||
if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
MCProtocol.MC_RESPONSE_HEADER responseHeader =
|
||||
MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
|
||||
|
||||
if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
|
||||
if (!ReceiveData(_bufferIn, dataLength))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] result = new byte[dataLength * 2];
|
||||
for (int i = 0; i < dataLength; i++)
|
||||
{
|
||||
if ((_bufferIn[i] & 0x10) == 0x10)
|
||||
{
|
||||
result[i * 2 + 0] = 0x01;
|
||||
}
|
||||
|
||||
if ((_bufferIn[i] & 0x01) == 0x01)
|
||||
{
|
||||
result[i * 2 + 1] = 0x01;
|
||||
}
|
||||
}
|
||||
|
||||
return result.Select(x => x == 0x01).Take(size).ToArray();
|
||||
}
|
||||
|
||||
protected override short[] ReadAi(int offset, int size)
|
||||
{
|
||||
if (_socket == null || !_socket.Connected)
|
||||
{
|
||||
State = IoProviderStateEnum.Error;
|
||||
return null;
|
||||
}
|
||||
short[] buff = new short[size];
|
||||
|
||||
int count = size / perIoLength;
|
||||
if (count < 1)
|
||||
{
|
||||
short[] aibuffer = DoReadAi(offset, size);
|
||||
if (aibuffer != null)
|
||||
Array.Copy(aibuffer, 0, buff, 0, aibuffer.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
short[] aibuffer;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
aibuffer = DoReadAi(i * perIoLength + offset, perIoLength);
|
||||
if (aibuffer != null)
|
||||
Array.Copy(aibuffer, 0, buff, i * perIoLength, aibuffer.Length);
|
||||
}
|
||||
|
||||
if(size % perIoLength != 0)
|
||||
{
|
||||
aibuffer = DoReadAi(offset + perIoLength * count, size % perIoLength);
|
||||
if (aibuffer != null)
|
||||
Array.Copy(aibuffer, 0, buff, size - size % perIoLength, aibuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
protected override void WriteDo(int offset, bool[] data)
|
||||
{
|
||||
if (_socket == null || !_socket.Connected)
|
||||
{
|
||||
State = IoProviderStateEnum.Error;
|
||||
return;
|
||||
}
|
||||
bool[] databuffer = new bool[perIoLength];
|
||||
int count = data.Length / perIoLength;
|
||||
if (count < 1)
|
||||
{
|
||||
Array.Copy(data, 0, databuffer, 0, data.Length);
|
||||
Array.Resize(ref databuffer, data.Length);
|
||||
|
||||
DoWriteDo(offset, databuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
|
||||
DoWriteDo(offset + perIoLength * i, databuffer);
|
||||
}
|
||||
|
||||
if (data.Length % perIoLength != 0)
|
||||
{
|
||||
Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
|
||||
Array.Resize(ref databuffer, data.Length % perIoLength);
|
||||
DoWriteDo(offset + perIoLength * count, databuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected short[] DoReadAi(int offset, int size)
|
||||
{
|
||||
_batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
|
||||
_batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
|
||||
|
||||
_batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
|
||||
_batchCommand.Reserved = (byte)(offset >> 16);
|
||||
|
||||
_batchCommand.DevicePoints = (ushort)size;
|
||||
|
||||
_header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
|
||||
|
||||
byte[] buffer = MCProtocol.Struct2Bytes(_header);
|
||||
byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
|
||||
|
||||
Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
|
||||
Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
|
||||
|
||||
if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
MCProtocol.MC_RESPONSE_HEADER responseHeader =
|
||||
MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
|
||||
|
||||
if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
|
||||
if (!ReceiveData(_bufferIn, dataLength))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int sizeofT1 = Marshal.SizeOf(typeof(byte));
|
||||
int sizeofT2 = Marshal.SizeOf(typeof(ushort));
|
||||
|
||||
short[] result = new short[sizeofT1 * 2 / sizeofT2];
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
result[i] = (short)BitConverter.ToUInt16(_bufferIn, i * 2);
|
||||
}
|
||||
|
||||
return Array.ConvertAll(MCProtocol.Byte2Ushort(_bufferIn, 0, dataLength), x => (short)x);
|
||||
}
|
||||
|
||||
protected void DoWriteDo(int offset, bool[] data)
|
||||
{
|
||||
if (_socket == null || !_socket.Connected)
|
||||
{
|
||||
State = IoProviderStateEnum.Error;
|
||||
return;
|
||||
}
|
||||
// bool[] boolData = new bool[data.Length];
|
||||
// boolData = data.Select(x => x==(byte)1).ToArray();
|
||||
// WriteDo(offset, boolData);
|
||||
//}
|
||||
|
||||
//protected void WriteDo(int offset, bool[] data)
|
||||
//{
|
||||
_batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
|
||||
_batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_BIT_UNITS;
|
||||
|
||||
byte[] byteData = MCProtocol.TransBoolArrayToByteData(data);
|
||||
|
||||
_batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
|
||||
_batchCommand.Reserved = (byte)(offset >> 16);
|
||||
|
||||
_batchCommand.DevicePoints = (ushort)data.Length;
|
||||
|
||||
_header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + byteData.Length);
|
||||
|
||||
byte[] header = MCProtocol.Struct2Bytes(_header);
|
||||
byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
|
||||
|
||||
Array.Copy(header, 0, _bufferOut, 0, header.Length);
|
||||
Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
|
||||
Array.Copy(byteData, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, byteData.Length);
|
||||
|
||||
if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + byteData.Length))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MCProtocol.MC_RESPONSE_HEADER responseHeader =
|
||||
MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
|
||||
|
||||
if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
|
||||
{
|
||||
LOG.Write("Write PLC failed with code," + responseHeader.CompleteCode);
|
||||
return;
|
||||
}
|
||||
|
||||
int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
|
||||
if (dataLength > 0 && !ReceiveData(_bufferIn, dataLength))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void WriteAo(int offset, short[] data)
|
||||
{
|
||||
short[] databuffer = new short[perIoLength];
|
||||
|
||||
int count = data.Length / perIoLength;
|
||||
if (count < 1)
|
||||
{
|
||||
Array.Copy(data, 0, databuffer, 0, data.Length);
|
||||
Array.Resize(ref databuffer, data.Length);
|
||||
|
||||
DoWriteAo(offset, databuffer.Select(x => (ushort)x).ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
|
||||
DoWriteAo(offset + perIoLength * i, databuffer.Select(x => (ushort)x).ToArray());
|
||||
}
|
||||
|
||||
if(data.Length % perIoLength != 0)
|
||||
{
|
||||
Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
|
||||
Array.Resize(ref databuffer, data.Length % perIoLength);
|
||||
DoWriteAo(offset + perIoLength * count, databuffer.Select(x => (ushort)x).ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void ReadDo(int offset, bool[] buffer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override short[] ReadAo(int offset, int size)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
//protected override void WriteAo(int offset, short[] data)
|
||||
//{
|
||||
// ushort[] uData = new ushort[data.Length];
|
||||
// uData = data.Select(x => (ushort)x).ToArray();
|
||||
// WriteAo(offset, uData);
|
||||
//}
|
||||
protected void DoWriteAo(int offset, ushort[] data)
|
||||
{
|
||||
_batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
|
||||
_batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
|
||||
|
||||
_batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
|
||||
_batchCommand.Reserved = (byte)(offset >> 16);
|
||||
|
||||
_batchCommand.DevicePoints = (ushort)data.Length;
|
||||
|
||||
_header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + MCProtocol.ShortSize * data.Length);
|
||||
|
||||
byte[] header = MCProtocol.Struct2Bytes(_header);
|
||||
byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
|
||||
byte[] byteData = MCProtocol.Ushort2Byte(data);
|
||||
|
||||
Array.Copy(header, 0, _bufferOut, 0, header.Length);
|
||||
Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
|
||||
Array.Copy(byteData, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, byteData.Length);
|
||||
|
||||
if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.ShortSize * data.Length))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MCProtocol.MC_RESPONSE_HEADER responseHeader =
|
||||
MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
|
||||
|
||||
if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
|
||||
{
|
||||
LOG.Write("Write PLC failed with code," + responseHeader.CompleteCode);
|
||||
return;
|
||||
}
|
||||
|
||||
int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
|
||||
if (dataLength > 0 && !ReceiveData(_bufferIn, dataLength))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private bool WriteData(byte[] data, int length)
|
||||
{
|
||||
return _socket.Write(data, length);
|
||||
}
|
||||
|
||||
private bool ReceiveData(byte[] data, int length)
|
||||
{
|
||||
return _socket.Read(data, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Aitex.Core.RT.Log;
|
||||
|
||||
namespace MECF.Framework.RT.Core.IoProviders
|
||||
{
|
||||
class MCSocket
|
||||
{
|
||||
private int m_nPort;
|
||||
private string m_strAddress;
|
||||
private Socket m_socket;
|
||||
private int m_nTimeOut;
|
||||
|
||||
public MCSocket()
|
||||
{
|
||||
m_nTimeOut = 30000;
|
||||
//m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
}
|
||||
|
||||
~MCSocket()
|
||||
{
|
||||
}
|
||||
|
||||
public bool Connected
|
||||
{
|
||||
get { return (m_socket != null && m_socket.Connected); }
|
||||
}
|
||||
|
||||
public bool Open(string strAddress, int nPort, string strLocalAddress)
|
||||
{
|
||||
if (Connected)
|
||||
return true;
|
||||
|
||||
Close();
|
||||
|
||||
m_strAddress = strAddress;
|
||||
m_nPort = nPort;
|
||||
|
||||
m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
try
|
||||
{
|
||||
// Bind to specific local endpoint
|
||||
IPAddress localAddress = IPAddress.Parse(strLocalAddress);
|
||||
IPEndPoint localEndPoint = new IPEndPoint(localAddress, 0);
|
||||
m_socket.Bind(localEndPoint);
|
||||
|
||||
IPAddress ipAddress = IPAddress.Parse(m_strAddress);
|
||||
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, m_nPort);
|
||||
//m_socket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||
m_socket.SendTimeout = m_nTimeOut;
|
||||
m_socket.ReceiveTimeout = m_nTimeOut;
|
||||
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
|
||||
m_socket.Connect(ipEndPoint);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Close()
|
||||
{
|
||||
if (m_socket == null) return true;
|
||||
|
||||
try
|
||||
{
|
||||
m_socket.Shutdown(SocketShutdown.Both);
|
||||
m_socket.Close();
|
||||
m_socket = null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Read(byte[] buffer)
|
||||
{
|
||||
if (!Connected) return false;
|
||||
|
||||
try
|
||||
{
|
||||
m_socket.Receive(buffer);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Read(byte[] buffer, int length)
|
||||
{
|
||||
if (!Connected) return false;
|
||||
|
||||
try
|
||||
{
|
||||
if (length < 0) return false;
|
||||
|
||||
m_socket.Receive(buffer, length, SocketFlags.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write($"Receive data failed, {ex.Message}, from {m_strAddress}:{m_nPort}");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Write(byte[] buffer)
|
||||
{
|
||||
if (!Connected) return false;
|
||||
|
||||
try
|
||||
{
|
||||
m_socket.Send(buffer, SocketFlags.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Write(byte[] buffer, int length)
|
||||
{
|
||||
if (!Connected) return false;
|
||||
|
||||
try
|
||||
{
|
||||
m_socket.Send(buffer, length, SocketFlags.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,471 @@
|
||||
using Aitex.Core.RT.DataCenter;
|
||||
using Aitex.Core.RT.Log;
|
||||
using MECF.Framework.Common.Communications;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
|
||||
namespace MECF.Framework.RT.Core.IoProviders
|
||||
{
|
||||
public class MYMCProtocolPlc : IoProvider, IConnection
|
||||
{
|
||||
private string _ip = "127.0.0.1";
|
||||
private string _localIp = "127.0.0.1";
|
||||
private int _port = 6731;
|
||||
private int _socketId = 101;
|
||||
private int _stationId = 102;
|
||||
|
||||
private byte[] _bufferIn;
|
||||
private byte[] _bufferOut;
|
||||
private int _bufferSize = 2048;
|
||||
|
||||
private MCProtocol.MC_COMMAND_HEADER _header;
|
||||
//private MCProtocol.MC_RESPONSE_HEADER _response;
|
||||
|
||||
private MCProtocol.MC_BATCH_COMMAND _batchCommand;
|
||||
|
||||
private MCSocket _socket;
|
||||
private int perIoLength = 960;
|
||||
public string Address
|
||||
{
|
||||
get { return $"{_ip}:{_port}"; }
|
||||
}
|
||||
|
||||
public bool IsConnected
|
||||
{
|
||||
get { return IsOpened; }
|
||||
}
|
||||
|
||||
public bool Connect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Disconnect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override void Close()
|
||||
{
|
||||
_socket.Close();
|
||||
SetState(IoProviderStateEnum.Closed);
|
||||
}
|
||||
|
||||
|
||||
protected override void Open()
|
||||
{
|
||||
_socket = new MCSocket();
|
||||
|
||||
_header = new MCProtocol.MC_COMMAND_HEADER()
|
||||
{
|
||||
ProtocolID = MCProtocol.MC_SUBHEADER_COMMAND_MESSAGE,
|
||||
NetworkID = (byte)_socketId,
|
||||
StationID = (byte)_stationId,
|
||||
RequestIONumber = MCProtocol.MC_REQUEST_MODULE_IO_NUMBER,
|
||||
RequestStationNumber = MCProtocol.MC_REQUEST_MODULE_STATION_NUMBER,
|
||||
RequestDataLen = 0,
|
||||
CPUMonitorTimer = (ushort)(MCProtocol.MC_CPU_MONITOR_TIMER * 2),
|
||||
};
|
||||
|
||||
|
||||
_batchCommand = new MCProtocol.MC_BATCH_COMMAND()
|
||||
{
|
||||
Command = MCProtocol.MC_COMMAND_BATCH_READ,
|
||||
DeviceCode = MCProtocol.MC_DEVICE_CODE_DATA_REGISTER_WORD,
|
||||
DevicePoints = 0,
|
||||
HeadAddr = 0,
|
||||
Reserved = 0,
|
||||
SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS,
|
||||
};
|
||||
|
||||
_bufferOut = new byte[_bufferSize];
|
||||
_bufferIn = new byte[_bufferSize];
|
||||
|
||||
_socket.Open(_ip, _port, _localIp);
|
||||
|
||||
SetState(IoProviderStateEnum.Opened);
|
||||
}
|
||||
|
||||
protected override short[] ReadAi(int offset, int size)
|
||||
{
|
||||
short[] buff = new short[size];
|
||||
|
||||
int count = size / perIoLength;
|
||||
if (count < 1)
|
||||
{
|
||||
short[] aibuffer = DoReadAi(offset, size);
|
||||
if (aibuffer != null)
|
||||
Array.Copy(aibuffer, 0, buff, 0, aibuffer.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
short[] aibuffer;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
aibuffer = DoReadAi(i * perIoLength + offset, perIoLength);
|
||||
if (aibuffer != null)
|
||||
Array.Copy(aibuffer, 0, buff, i * perIoLength, aibuffer.Length);
|
||||
}
|
||||
|
||||
if (size % perIoLength != 0)
|
||||
{
|
||||
aibuffer = DoReadAi(offset + perIoLength * count, size % perIoLength);
|
||||
if (aibuffer != null)
|
||||
Array.Copy(aibuffer, 0, buff, size - size % perIoLength, aibuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
protected short[] DoReadAi(int offset, int size)
|
||||
{
|
||||
_batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
|
||||
_batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
|
||||
|
||||
_batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
|
||||
_batchCommand.Reserved = (byte)(offset >> 16);
|
||||
|
||||
_batchCommand.DevicePoints = (ushort)size;
|
||||
|
||||
_header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
|
||||
|
||||
byte[] buffer = MCProtocol.Struct2Bytes(_header);
|
||||
byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
|
||||
|
||||
Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
|
||||
Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
|
||||
|
||||
if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
MCProtocol.MC_RESPONSE_HEADER responseHeader =
|
||||
MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
|
||||
|
||||
if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
|
||||
if (!ReceiveData(_bufferIn, dataLength))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int sizeofT1 = Marshal.SizeOf(typeof(byte));
|
||||
int sizeofT2 = Marshal.SizeOf(typeof(ushort));
|
||||
|
||||
short[] result = new short[sizeofT1 * 2 / sizeofT2];
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
result[i] = (short)BitConverter.ToUInt16(_bufferIn, i * 2);
|
||||
}
|
||||
|
||||
return Array.ConvertAll(MCProtocol.Byte2Ushort(_bufferIn, 0, dataLength), x => (short)x);
|
||||
}
|
||||
|
||||
protected override bool[] ReadDi(int offset, int size)
|
||||
{
|
||||
bool[] buff = new bool[size];
|
||||
|
||||
int count = size / perIoLength;
|
||||
if (count < 1)
|
||||
{
|
||||
bool[] dibuffer = DoReadDi(offset, size);
|
||||
if (dibuffer != null)
|
||||
Array.Copy(dibuffer, 0, buff, 0, dibuffer.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool[] dibuffer;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
dibuffer = DoReadDi(i * perIoLength + offset, perIoLength);
|
||||
if (dibuffer != null)
|
||||
Array.Copy(dibuffer, 0, buff, i * perIoLength, dibuffer.Length);
|
||||
}
|
||||
|
||||
if (size % perIoLength != 0)
|
||||
{
|
||||
dibuffer = DoReadDi(offset + perIoLength * count, size % perIoLength);
|
||||
if (dibuffer != null)
|
||||
Array.Copy(dibuffer, 0, buff, size - size % perIoLength, dibuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
protected bool[] DoReadDi(int offset, int size)
|
||||
{
|
||||
|
||||
_batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
|
||||
_batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_BIT_UNITS;
|
||||
|
||||
_batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
|
||||
_batchCommand.Reserved = (byte)(offset >> 16);
|
||||
|
||||
_batchCommand.DevicePoints = (ushort)size;
|
||||
|
||||
_header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
|
||||
|
||||
byte[] buffer = MCProtocol.Struct2Bytes(_header);
|
||||
byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
|
||||
|
||||
Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
|
||||
Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
|
||||
|
||||
if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
MCProtocol.MC_RESPONSE_HEADER responseHeader =
|
||||
MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
|
||||
|
||||
if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
|
||||
if (!ReceiveData(_bufferIn, dataLength))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] result = new byte[dataLength * 2];
|
||||
for (int i = 0; i < dataLength; i++)
|
||||
{
|
||||
if ((_bufferIn[i] & 0x10) == 0x10)
|
||||
{
|
||||
result[i * 2 + 0] = 0x01;
|
||||
}
|
||||
|
||||
if ((_bufferIn[i] & 0x01) == 0x01)
|
||||
{
|
||||
result[i * 2 + 1] = 0x01;
|
||||
}
|
||||
}
|
||||
|
||||
return result.Select(x => x == 0x01).Take(size).ToArray();
|
||||
}
|
||||
|
||||
protected override void SetParameter(XmlElement nodeParameter)
|
||||
{
|
||||
string strIp = nodeParameter.GetAttribute("ip");
|
||||
string strPort = nodeParameter.GetAttribute("port");
|
||||
string networkId = nodeParameter.GetAttribute("network_id");
|
||||
string stationId = nodeParameter.GetAttribute("station_id");
|
||||
if (!string.IsNullOrEmpty(nodeParameter.GetAttribute("buffer_size")))
|
||||
{
|
||||
int.TryParse(nodeParameter.GetAttribute("buffer_size"), out _bufferSize);
|
||||
}
|
||||
|
||||
_port = int.Parse(strPort);
|
||||
_ip = strIp;
|
||||
_socketId = int.Parse(networkId);
|
||||
_stationId = int.Parse(stationId);
|
||||
|
||||
ConnectionManager.Instance.Subscribe(Name, this);
|
||||
DATA.Subscribe($"{Module}.{Name}.IsConnected", () => _socket == null ? false : _socket.Connected);
|
||||
}
|
||||
|
||||
protected override void WriteAo(int offset, short[] data)
|
||||
{
|
||||
short[] databuffer = new short[perIoLength];
|
||||
|
||||
int count = data.Length / perIoLength;
|
||||
if (count < 1)
|
||||
{
|
||||
Array.Copy(data, 0, databuffer, 0, data.Length);
|
||||
Array.Resize(ref databuffer, data.Length);
|
||||
|
||||
DoWriteAo(offset, databuffer.Select(x => (ushort)x).ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
|
||||
DoWriteAo(offset + perIoLength * i, databuffer.Select(x => (ushort)x).ToArray());
|
||||
}
|
||||
|
||||
if (data.Length % perIoLength != 0)
|
||||
{
|
||||
Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
|
||||
Array.Resize(ref databuffer, data.Length % perIoLength);
|
||||
DoWriteAo(offset + perIoLength * count, databuffer.Select(x => (ushort)x).ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void ReadDo(int offset, bool[] buffer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override short[] ReadAo(int offset, int size)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected void DoWriteAo(int offset, ushort[] data)
|
||||
{
|
||||
_batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
|
||||
_batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
|
||||
|
||||
_batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
|
||||
_batchCommand.Reserved = (byte)(offset >> 16);
|
||||
|
||||
_batchCommand.DevicePoints = (ushort)data.Length;
|
||||
|
||||
_header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + MCProtocol.ShortSize * data.Length);
|
||||
|
||||
byte[] header = MCProtocol.Struct2Bytes(_header);
|
||||
byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
|
||||
byte[] byteData = MCProtocol.Ushort2Byte(data);
|
||||
|
||||
Array.Copy(header, 0, _bufferOut, 0, header.Length);
|
||||
Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
|
||||
Array.Copy(byteData, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, byteData.Length);
|
||||
|
||||
if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.ShortSize * data.Length))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MCProtocol.MC_RESPONSE_HEADER responseHeader =
|
||||
MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
|
||||
|
||||
if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
|
||||
{
|
||||
LOG.Write("Write PLC failed with code," + responseHeader.CompleteCode);
|
||||
return;
|
||||
}
|
||||
|
||||
int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
|
||||
if (dataLength > 0 && !ReceiveData(_bufferIn, dataLength))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void WriteDo(int offset, bool[] data)
|
||||
{
|
||||
bool[] databuffer = new bool[perIoLength];
|
||||
int count = data.Length / perIoLength;
|
||||
if (count < 1)
|
||||
{
|
||||
Array.Copy(data, 0, databuffer, 0, data.Length);
|
||||
Array.Resize(ref databuffer, data.Length);
|
||||
|
||||
DoWriteDo(offset, databuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
|
||||
DoWriteDo(offset + perIoLength * i, databuffer);
|
||||
}
|
||||
|
||||
if (data.Length % perIoLength != 0)
|
||||
{
|
||||
Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
|
||||
Array.Resize(ref databuffer, data.Length % perIoLength);
|
||||
DoWriteDo(offset + perIoLength * count, databuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void DoWriteDo(int offset, bool[] data)
|
||||
{
|
||||
// bool[] boolData = new bool[data.Length];
|
||||
// boolData = data.Select(x => x==(byte)1).ToArray();
|
||||
// WriteDo(offset, boolData);
|
||||
//}
|
||||
|
||||
//protected void WriteDo(int offset, bool[] data)
|
||||
//{
|
||||
_batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
|
||||
_batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_BIT_UNITS;
|
||||
|
||||
byte[] byteData = MCProtocol.TransBoolArrayToByteData(data);
|
||||
|
||||
_batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
|
||||
_batchCommand.Reserved = (byte)(offset >> 16);
|
||||
|
||||
_batchCommand.DevicePoints = (ushort)data.Length;
|
||||
|
||||
_header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + byteData.Length);
|
||||
|
||||
byte[] header = MCProtocol.Struct2Bytes(_header);
|
||||
byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
|
||||
|
||||
Array.Copy(header, 0, _bufferOut, 0, header.Length);
|
||||
Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
|
||||
Array.Copy(byteData, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, byteData.Length);
|
||||
|
||||
if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + byteData.Length))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MCProtocol.MC_RESPONSE_HEADER responseHeader =
|
||||
MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
|
||||
|
||||
if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
|
||||
{
|
||||
LOG.Write("Write PLC failed with code," + responseHeader.CompleteCode);
|
||||
return;
|
||||
}
|
||||
|
||||
int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
|
||||
if (dataLength > 0 && !ReceiveData(_bufferIn, dataLength))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private bool WriteData(byte[] data, int length)
|
||||
{
|
||||
return _socket.Write(data, length);
|
||||
}
|
||||
|
||||
private bool ReceiveData(byte[] data, int length)
|
||||
{
|
||||
return _socket.Read(data, length);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MECF.Framework.RT.Core.IoProviders.Mitsubishis
|
||||
{
|
||||
public class MelsecElement
|
||||
{
|
||||
public static MelsecElement relaySpecial =
|
||||
new MelsecElement("特殊继电器", "SM", 0x91, 0x000000, 0x002047, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement storageSpecial =
|
||||
new MelsecElement("特殊寄存器", "SD", 0xA9, 0x000000, 0x002047, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public static MelsecElement relayInput =
|
||||
new MelsecElement("输入继电器", "X", 0x9C, 0x000000, 0x001FFF, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relayOutPut =
|
||||
new MelsecElement("输出继电器", "Y", 0x9D, 0x000000, 0x001FFF, MelsecElement.EnumType.Bit, 7168);
|
||||
|
||||
public static MelsecElement relayInner =
|
||||
new MelsecElement("内部继电器", "M", 0x90, 0x000000, 0x008191, MelsecElement.EnumType.Bit, 7904);
|
||||
|
||||
public static MelsecElement relayLock =
|
||||
new MelsecElement("锁存继电器", "L", 0x92, 0x000000, 0x008191, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relayAlarm =
|
||||
new MelsecElement("报警继电器", "F", 0x93, 0x000000, 0x002047, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relayEdge =
|
||||
new MelsecElement("边沿继电器", "V", 0x94, 0x000000, 0x002047, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relayLink =
|
||||
new MelsecElement("链接继电器", "B", 0xA0, 0x000000, 0x001FFF, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement storageData =
|
||||
new MelsecElement("数据寄存器", "D", 0xA8, 0x000000, 0x012287, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public static MelsecElement storageLink =
|
||||
new MelsecElement("链接寄存器", "W", 0xB4, 0x000000, 0x001FFFF, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public static MelsecElement relayTS =
|
||||
new MelsecElement("定时器触点", "TS", 0xC1, 0x000000, 0x002047, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relayTC =
|
||||
new MelsecElement("定时器线圈", "TN", 0xC0, 0x000000, 0x002047, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement storageTN =
|
||||
new MelsecElement("定时器当前值", "TC", 0xC2, 0x000000, 0x002047, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public static MelsecElement relaySS =
|
||||
new MelsecElement("累计定时器触点", "SS", 0xC7, 0x000000, 0x002047, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relaySC =
|
||||
new MelsecElement("累计定时器线圈", "SC", 0xC6, 0x000000, 0x002047, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement storageSN =
|
||||
new MelsecElement("累计定时器当前值", "SN", 0xC8, 0x000000, 0x002047, MelsecElement.EnumType.Word);
|
||||
|
||||
public static MelsecElement relayCS =
|
||||
new MelsecElement("计数器触点", "CS", 0xC4, 0x000000, 0x001023, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relayCC =
|
||||
new MelsecElement("计数器线圈", "CC", 0xC3, 0x000000, 0x001023, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement storageCN =
|
||||
new MelsecElement("计数器当前值", "CN", 0xC5, 0x000000, 0x001023, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public static MelsecElement relaySpecialLink =
|
||||
new MelsecElement("链接特殊继电器", "SB", 0xA1, 0x000000, 0x0007FF, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement storageSpecialLink =
|
||||
new MelsecElement("链接特殊寄存器", "SW", 0xB5, 0x000000, 0x0007FF, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public static MelsecElement relayStep =
|
||||
new MelsecElement("步进继电器", "S", 0x98, 0x000000, 0x008191, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relayInputDir =
|
||||
new MelsecElement("直接输入继电器", "DX", 0xA2, 0x000000, 0x001FFF, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement relayOutPutDir =
|
||||
new MelsecElement("直接输出继电器", "DY", 0xA3, 0x000000, 0x001FFF, MelsecElement.EnumType.Bit);
|
||||
|
||||
public static MelsecElement storageAddr =
|
||||
new MelsecElement("变址寄存器", "Z", 0xCC, 0x000000, 0x000015, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public static MelsecElement storageFile =
|
||||
new MelsecElement("文件寄存器", "R", 0xAF, 0x032767, 0x000015, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public static MelsecElement storageFileZ =
|
||||
new MelsecElement("文件寄存器Z", "RZ", 0xB0, 0x0FE7FF, 0x000015, MelsecElement.EnumType.Word, 960);
|
||||
|
||||
public MelsecElement() { }
|
||||
|
||||
public MelsecElement(string strName, string strAscCode, int nBinCode,
|
||||
int nStartAddr, int nEndAddr, EnumType enumType, int nLen = 3584)
|
||||
{
|
||||
m_strName = strName;
|
||||
m_strAscCode = strAscCode;
|
||||
m_nBinCode = nBinCode;
|
||||
m_nStartAddr = nStartAddr;
|
||||
m_nEndAddr = nEndAddr;
|
||||
m_enumType = enumType;
|
||||
m_nLen = nLen;
|
||||
}
|
||||
|
||||
public static MelsecElement ChooseMelsecElement(string registername)
|
||||
{
|
||||
if (registername == storageFile.m_strName || registername == storageFile.m_strAscCode)
|
||||
return storageFile;
|
||||
if (registername == relayLink.m_strName || registername == relayLink.m_strAscCode)
|
||||
return relayLink;
|
||||
if (registername == relayInner.m_strName || registername == relayInner.m_strAscCode)
|
||||
return relayInner;
|
||||
if (registername == storageData.m_strName || registername == storageData.m_strAscCode)
|
||||
return storageData;
|
||||
if (registername == storageFileZ.m_strName || registername == "ZR" || registername == storageFileZ.m_strAscCode)
|
||||
return storageFileZ;
|
||||
if (registername== relaySpecial.m_strName ||registername==relaySpecial.m_strAscCode)
|
||||
return relaySpecial;
|
||||
if (registername == storageSpecial.m_strName || registername == storageSpecial.m_strAscCode)
|
||||
return storageSpecial;
|
||||
if (registername == relayInput.m_strName || registername == relayInput.m_strAscCode)
|
||||
return relayInput;
|
||||
if (registername == relayOutPut.m_strName || registername == relayOutPut.m_strAscCode)
|
||||
return relayOutPut;
|
||||
if (registername == relayLock.m_strName || registername == relayLock.m_strAscCode)
|
||||
return relayLock;
|
||||
if (registername == relayAlarm.m_strName || registername == relayAlarm.m_strAscCode)
|
||||
return relayAlarm;
|
||||
if (registername == relayEdge.m_strName || registername == relayEdge.m_strAscCode)
|
||||
return relayEdge;
|
||||
if (registername == storageLink.m_strName || registername == storageLink.m_strAscCode)
|
||||
return storageLink;
|
||||
if (registername == relayTS.m_strName || registername == relayTS.m_strAscCode)
|
||||
return relayTS;
|
||||
if (registername == relayTC.m_strName || registername == relayTC.m_strAscCode)
|
||||
return relayTC;
|
||||
if (registername == storageTN.m_strName || registername == storageTN.m_strAscCode)
|
||||
return storageTN;
|
||||
if (registername == relaySS.m_strName || registername == relaySS.m_strAscCode)
|
||||
return relaySS;
|
||||
if (registername == relaySC.m_strName || registername == relaySC.m_strAscCode)
|
||||
return relaySC;
|
||||
if (registername == storageSN.m_strName || registername == storageSN.m_strAscCode)
|
||||
return storageSN;
|
||||
if (registername == relayCS.m_strName || registername == relayCS.m_strAscCode)
|
||||
return relayCS;
|
||||
if (registername == relayCC.m_strName || registername == relayCC.m_strAscCode)
|
||||
return relayCC;
|
||||
if (registername == storageCN.m_strName || registername == storageCN.m_strAscCode)
|
||||
return storageCN;
|
||||
if (registername == relaySpecialLink.m_strName || registername == relaySpecialLink.m_strAscCode)
|
||||
return relaySpecialLink;
|
||||
if (registername == storageSpecialLink.m_strName || registername == storageSpecialLink.m_strAscCode)
|
||||
return storageSpecialLink;
|
||||
if (registername == relayStep.m_strName || registername == relayStep.m_strAscCode)
|
||||
return relayStep;
|
||||
if (registername == relayInputDir.m_strName || registername == relayInputDir.m_strAscCode)
|
||||
return relayInputDir;
|
||||
if (registername == relayOutPutDir.m_strName || registername == relayOutPutDir.m_strAscCode)
|
||||
return relayOutPutDir;
|
||||
if (registername == storageAddr.m_strName || registername == storageAddr.m_strAscCode)
|
||||
return storageAddr;
|
||||
return null;
|
||||
}
|
||||
|
||||
public enum EnumType
|
||||
{
|
||||
Bit = 0,//位类型
|
||||
Word = 1,//字类型
|
||||
}
|
||||
|
||||
public string m_strName { get; set; }
|
||||
public string m_strAscCode { get; set; }
|
||||
public int m_nBinCode { get; set; }
|
||||
public int m_nStartAddr { get; set; }
|
||||
public int m_nEndAddr { get; set; }
|
||||
public EnumType m_enumType { get; set; }
|
||||
public int m_nLen { get; set; }//最大处理长度
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MECF.Framework.RT.Core.IoProviders.Mitsubishis
|
||||
{
|
||||
|
||||
public class MelsecMCBin
|
||||
{
|
||||
public MelsecMCBin()
|
||||
{
|
||||
m_nNetNo = 0x00;//本站
|
||||
m_nPLCNo = 0xFF;
|
||||
m_nIONo = 0xFF03;
|
||||
m_nStationNo = 0x00;
|
||||
m_nTimeOut = 0x1000;
|
||||
m_bytelist = new List<byte>();
|
||||
m_nDataLen = 12;//不包括软元件数据
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查起始地址和读取长度
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool check()
|
||||
{
|
||||
|
||||
if (m_nElementDataLen < 0 || m_nElementDataLen > m_melsecElement.m_nLen)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_nElementStartAddr < m_melsecElement.m_nStartAddr || m_nElementStartAddr > m_melsecElement.m_nEndAddr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*************读协议内容******************************
|
||||
* 副标题(2)50 00|网络编号(1)00|PLC编号(1)FF
|
||||
* IO编号(2)FF 03|站编号(1)00|请求数据长度(2)_12
|
||||
* 应答超时(2)1000|命令(2)_|子命令(2)_|起始地址(3)_
|
||||
* 请求软元件代码(1)|请求点数长度(2)
|
||||
* ***************************************************/
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 构建读取字节
|
||||
/// </summary>
|
||||
public byte[] getReadBytes()
|
||||
{
|
||||
m_bytelist.Clear();
|
||||
|
||||
if (check())
|
||||
{
|
||||
m_enumSubTitle = EnumsubTitle.Request;
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumSubTitle, 2));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nNetNo, 1));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nPLCNo, 1));
|
||||
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nIONo, 2));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nStationNo, 1));
|
||||
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nDataLen, 2, false));
|
||||
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nTimeOut, 2));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumCmd, 2, false));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumSubCmd, 2, false));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementStartAddr, 3, false));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_melsecElement.m_nBinCode, 1));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementDataLen, 2, false));
|
||||
|
||||
}
|
||||
|
||||
return m_bytelist.ToArray();
|
||||
}
|
||||
|
||||
/****************写协议内容**********************************
|
||||
* 副标题(2)50 00|网络编号(1)00|PLC编号(1)FF
|
||||
* IO编号(2)FF 03|站编号(1)00|请求数据长度(2)_12+写入数据长度
|
||||
* 应答超时(2)1000|命令(2)_|子命令(2)_|起始地址(3)_
|
||||
* 请求软元件代码(1)|请求点数长度(2)|写入数据(分按位和按字)
|
||||
* ***********************************************************/
|
||||
|
||||
/// <summary>
|
||||
/// 构建写字节
|
||||
/// </summary>
|
||||
/// <param name="nLen">写入数据长度</param>
|
||||
public byte[] getWriteBytes(int nLen)
|
||||
{
|
||||
m_bytelist.Clear();
|
||||
if (check())
|
||||
{
|
||||
m_enumSubTitle = EnumsubTitle.Request;
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumSubTitle, 2));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nNetNo, 1));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nPLCNo, 1));
|
||||
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nIONo, 2));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nStationNo, 1));
|
||||
|
||||
m_nDataLen += nLen;
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nDataLen, 2, false));
|
||||
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nTimeOut, 2));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumCmd, 2, false));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_enumSubCmd, 2, false));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementStartAddr, 3, false));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_melsecElement.m_nBinCode, 1));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementDataLen, 2, false));
|
||||
m_bytelist.AddRange(StringHelper.ConverterToBytes(m_nElementData, nLen));
|
||||
|
||||
}
|
||||
|
||||
return m_bytelist.ToArray();
|
||||
}
|
||||
//MelsecMCBinSend: 500000FFFF03000E001000011400000000009D0100000F 按字写
|
||||
//MelsecMCBinRece: D00000FFFF030002000000
|
||||
|
||||
/****************读应答正常协议内容**********************************
|
||||
* 副标题(2)D0 00|网络编号(1)00|PLC编号(1)FF
|
||||
* IO编号(2)FF 03|站编号(1)00|应答数据长度(2)_
|
||||
* 结束代码(2)00 00|应答数据部分
|
||||
* ***********************************************************/
|
||||
public byte[] getReadRespond(byte[] byteRespond)
|
||||
{
|
||||
if (byteRespond.Length > 11)
|
||||
{
|
||||
|
||||
int nLen = StringHelper.ConverterToInt(byteRespond[7], byteRespond[8]) - 2;
|
||||
if (nLen == byteRespond.Length - 11)
|
||||
{
|
||||
for (int n = 0; n < nLen; n++)
|
||||
{
|
||||
byteRespond[n] = byteRespond[byteRespond.Length - n - 1];
|
||||
}
|
||||
Array.Resize(ref byteRespond, nLen);
|
||||
}
|
||||
|
||||
return byteRespond;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/****************写应答正常协议内容**********************************
|
||||
* 副标题(2)D0 00|网络编号(1)00|PLC编号(1)FF
|
||||
* IO编号(2)FF 03|站编号(1)00|应答数据长度(2)02 00
|
||||
* 结束代码(2)00 00
|
||||
* D0 00 |00 |FF |FF 03| 00| 02 00 |00 00
|
||||
* ***********************************************************/
|
||||
public bool getWriteRespond(byte[] byteRespond)
|
||||
{
|
||||
string strRespond = StringHelper.GetHexString(byteRespond, byteRespond.Length);
|
||||
string strTemp = "D00000FFFF030002000000";
|
||||
return string.Equals(strTemp, strRespond);
|
||||
}
|
||||
|
||||
/****************应答异常协议内容**********************************
|
||||
* 副标题(2)D0 00|网络编号(1)00|PLC编号(1)FF
|
||||
* IO编号(2)FF 03|站编号(1)00|应答数据长度(2) 0B 00
|
||||
* 结束代码(2)51 C0
|
||||
*
|
||||
* 网络编号(1)00|PLC编号(1)FF
|
||||
* IO编号(2)FF 03|站编号(1)00|命令(2)|子命令(2)
|
||||
* ***********************************************************/
|
||||
public int getRespondCode(byte[] byteRespond)
|
||||
{
|
||||
if (byteRespond.Length > 10)
|
||||
{
|
||||
return StringHelper.ConverterToInt(byteRespond[9], byteRespond[10]);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*副标题*/
|
||||
public EnumsubTitle m_enumSubTitle { get; set; }
|
||||
|
||||
/*网络编号*/
|
||||
public int m_nNetNo { get; set; }
|
||||
|
||||
/*PLC编号*/
|
||||
public int m_nPLCNo { get; set; }
|
||||
|
||||
/*IO编号*/
|
||||
public int m_nIONo { get; set; }
|
||||
|
||||
/*站编号*/
|
||||
public int m_nStationNo { get; set; }
|
||||
|
||||
/*数据长度*/
|
||||
public int m_nDataLen { get; set; }
|
||||
|
||||
/*CPU监视定时器,命令输出到接收应答文件时间*/
|
||||
public int m_nTimeOut { get; set; }
|
||||
|
||||
/*命令*/
|
||||
public EnumCmd m_enumCmd { get; set; }
|
||||
|
||||
/*子命令*/
|
||||
public EnumSubCmd m_enumSubCmd { get; set; }
|
||||
|
||||
|
||||
/*软元件*/
|
||||
public MelsecElement m_melsecElement { get; set; }
|
||||
|
||||
/*起始软元件地址*/
|
||||
public int m_nElementStartAddr { get; set; }
|
||||
|
||||
/*软元件数据长度*/
|
||||
public int m_nElementDataLen { get; set; }
|
||||
|
||||
/*软元件数据*/
|
||||
public int m_nElementData { get; set; }
|
||||
|
||||
/*结束代码*/
|
||||
public EnumEndCode m_nEndCode { get; set; }
|
||||
|
||||
/*临时存放字节数组*/
|
||||
private List<byte> m_bytelist;
|
||||
|
||||
public enum EnumsubTitle
|
||||
{
|
||||
Request = 0x5000,//请求
|
||||
Respond = 0xD000,//应答
|
||||
}
|
||||
|
||||
public enum EnumEndCode
|
||||
{
|
||||
Ok = 0x0000,//正常应答
|
||||
Err = 0x51C0,//异常应答
|
||||
}
|
||||
|
||||
public enum EnumCmd
|
||||
{
|
||||
ReadBatch = 0x0401,//成批读
|
||||
WriteBatch = 0x1401,//成批写
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回byte数组
|
||||
/// </summary>
|
||||
public enum EnumSubCmd
|
||||
{
|
||||
/*有存储扩展模块b7=0,b6=0:随机读出,监视数据注册用外*/
|
||||
Bit = 0x0001,//按位读写
|
||||
Word = 0x0000,//按字读写
|
||||
|
||||
|
||||
/*有存储扩展模块b7=1,b6=0:随机读出,监视数据注册用外*/
|
||||
BitEx = 0x0081,//按位读写
|
||||
WordEx = 0x0080,//按字读写
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static class StringHelper
|
||||
{
|
||||
public static byte[] ConverterToBytes(object value, int length,bool reverse=true)
|
||||
{
|
||||
byte[] src = new byte[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
src[i] = (byte)(((byte)value >> (i*8)) & 0xFF);
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
public static int ConverterToInt(byte value1, byte value2)
|
||||
{
|
||||
int result = value1 << 8 + value2;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string GetHexString(byte[] byteRespond, int length)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
foreach (byte bytes in byteRespond)
|
||||
{
|
||||
result.AppendFormat("{0:x2}", bytes);
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user