using Aitex.Core.Utilities; using MECF.Framework.Common.Device.PowerSupplier; using MECF.Framework.Common.Net; using MECF.Framework.Common.Utilities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.Common.Device.Wago { public class WagoMessage : INetMessage { #region 常量 private const int DI_READ_CMD = 1; private const int DO_READ_CMD = 2; private const int AI_READ_CMD = 3; private const int AO_READ_CMD = 4; #endregion #region 属性 public int ProtocolHeadBytesLength { get; set; } = 8; public int ErrorCode { get; set; } public string ErrorMsg { get; set; } public byte[] HeadBytes { get; set; } public byte[] ContentBytes { get; set; } public byte[] SendBytes { get; set; } #endregion #region 内部变量 IByteTransform byteTransform = new BigEndianByteTransformBase(); #endregion public bool CheckDataLegal() { //第二个字节高位为0x80,设备报错 if ((HeadBytes[HeadBytes.Length-1] & 0x80) == 0x80) { ErrorCode = (int)NetErrorCode.DeviceError; ErrorMsg = GetDeviceMessage(ContentBytes[0]); return false; } return true; } /// /// 设备错误信息 /// /// private string GetDeviceMessage(byte deviceErrorCode) { switch (deviceErrorCode) { case 0x01: return "Invalid Function"; case 0x02: return "Invalid Address"; case 0x03: return "Invalid Data"; case 0x04: return "Slave Device Error"; case 0x05: return "Error Confirm"; case 0x06: return "Slave Device Busy"; default: return "Unkown Error Code"; } } public bool CheckHeadBytesLegal() { //地址与指令同时一致 return SendBytes[0] == HeadBytes[0] && SendBytes[1] == HeadBytes[1] && HeadBytes[2] == 0x00 && HeadBytes[3] == 0x00; } public bool ConfirmResponseResult() { //指令码与发送指令码一致 if (HeadBytes[HeadBytes.Length-1] == SendBytes[HeadBytes.Length-1]) { return true; } return false; } public WagoCommand Decode() { WagoCommand command = new WagoCommand(); byte commandCode = HeadBytes[HeadBytes.Length - 1]; //DI或DO if (commandCode==DI_READ_CMD||commandCode==DO_READ_CMD) { byte bytLength = ContentBytes[0]; //byte长度*8 bool[] datas = new bool[bytLength*8]; byte[] bytes = new byte[bytLength]; Array.Copy(ContentBytes,1,bytes, 0, bytes.Length); for (int i = 0; i < bytes.Length; i++) { for (int j = 0; j < 8; j++) { if (j == 0) { datas[i*8 + j] = ((bytes[i] & 0x01) == 0x01); } else { datas[i*8 + j] = (((bytes[i] >> j) & 0x01) == 0x01); } } } command.Datas = datas; } //DI或DO else if (commandCode == AI_READ_CMD || commandCode == AO_READ_CMD) { byte bytLength = ContentBytes[0]; byte[] bytes = new byte[bytLength]; Array.Copy(ContentBytes, 1, bytes, 0, bytes.Length); command.Datas = bytes; } return command; } public int GetContentLengthByHeadBytes() { //第二个字节高位为0x80,设备报错 if ((HeadBytes[HeadBytes.Length - 1] & 0x80) == 0x80) { return 1; } else { //长度减去unit identifier+modbus code return byteTransform.TransInt16(HeadBytes, 4)-2; } } public int GetHeadBytesIdentity() { return byteTransform.TransInt16(HeadBytes, 0); } /// /// 编码 /// /// /// public byte[] Code(WagoCommand data) { byte[] byt = null; //读取DI if (IsReadCommand(data.CommandCode)) { byt = new byte[12]; //事务处理标识 Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2); //固定码0x0000 byt[2] = 0x00; byt[3] = 0x00; //数据长度 byt[4] = 0x00; byt[5] = 0x06; //通道 byt[6] = data.Channel; //指令 byt[7] = data.CommandCode; Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2); Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 10, 2); } //写单个寄存器地址 else if (data.CommandCode == 0x05) { byt = new byte[12]; //事务处理标识 Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2); //固定码0x0000 byt[2] = 0x00; byt[3] = 0x00; //数据长度 byt[4] = 0x00; byt[5] = 0x06; //通道 byt[6] = data.Channel; //指令 byt[7] = data.CommandCode; Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2); byt[10] = (byte)data.WriteValue; byt[11] = 0x00; } else if (data.CommandCode == 0x06) { byt = new byte[12]; //事务处理标识 Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2); //固定码0x0000 byt[2] = 0x00; byt[3] = 0x00; //数据长度 byt[4] = 0x00; byt[5] = 0x06; //通道 byt[6] = data.Channel; //指令 byt[7] = data.CommandCode; Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2); Array.Copy((byte[])data.WriteValue, 0, byt, 10, 2); } return byt; } /// /// 是否为读取指令 /// /// /// private bool IsReadCommand(byte command) { switch (command) { case DI_READ_CMD: case DO_READ_CMD: case AI_READ_CMD: case AO_READ_CMD: return true; default: return false; } } public void SetProtocolHeadBytesLength() { } } }