123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- 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<WagoCommand>
- {
- #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;
- }
- /// <summary>
- /// 设备错误信息
- /// </summary>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 编码
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 是否为读取指令
- /// </summary>
- /// <param name="command"></param>
- /// <returns></returns>
- 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()
- {
- }
- }
- }
|