| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 | 
							- 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.Festo
 
- {
 
-     public class FestoMessage : INetMessage<FestoCommand>
 
-     {
 
-         #region 属性
 
-         public int ProtocolHeadBytesLength { get; set; } = 6;
 
-         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 ((ContentBytes[1] & 0x80) == 0x80)
 
-             {
 
-                 ErrorCode = (int)NetErrorCode.DeviceError;
 
-                 ErrorMsg = GetDeviceMessage(ContentBytes[2]);
 
-                 return false;
 
-             }
 
-             //数据长度不一致
 
-             if (ContentBytes[1] == 0x03)
 
-             {
 
-                 byte contentLength = ContentBytes[2];
 
-                 if (ContentBytes.Length - 3 != contentLength)
 
-                 {
 
-                     ErrorCode = (int)NetErrorCode.InvalidData;
 
-                     ErrorMsg = EnumUtil.GetEnumDescription(NetErrorCode.InvalidData);
 
-                     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[1] == SendBytes[1])
 
-             {
 
-                 return true;
 
-             }
 
-             return false;
 
-         }
 
-         public FestoCommand Decode()
 
-         {
 
-             FestoCommand command = new FestoCommand();
 
-             if (ContentBytes[1] == 0x03)
 
-             {
 
-                 command.Datas = new byte[ContentBytes.Length - 3];
 
-                 Array.Copy(ContentBytes,3,command.Datas, 0, command.Datas.Length);
 
-             }
 
-             return command;
 
-         }
 
-         public int GetContentLengthByHeadBytes()
 
-         {
 
-             return byteTransform.TransInt16(HeadBytes, 4);
 
-         }
 
-         public int GetHeadBytesIdentity()
 
-         {
 
-             return byteTransform.TransInt16(HeadBytes, 0);
 
-         }
 
-         public byte[] Code(FestoCommand data)
 
-         {
 
-             byte[] byt = null;
 
-             //读取指令
 
-             if (data.CommandCode == 0x03)
 
-             {
 
-                 byt = new byte[12];
 
-                 //事务处理标识
 
-                 Array.Copy(FestoFlag.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 == 0x06)
 
-             {
 
-                 byt = new byte[12];
 
-                 //事务处理标识
 
-                 Array.Copy(FestoFlag.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] = 0x00;
 
-                 byt[11] = data.Datas[0];
 
-             }
 
-             else if (data.CommandCode == 0x10)
 
-             {
 
-                 byt = new byte[6 + 7 + 2 * data.Datas.Length];
 
-                 //事务处理标识
 
-                 Array.Copy(FestoFlag.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);
 
-                 byt[12] = (byte)(2 * data.Datas.Length);
 
-                 for (int i = 0; i < data.Datas.Length; i++)
 
-                 {
 
-                     byt[13 + i] = 0x00;
 
-                     byt[14+i]=data.Datas[i];
 
-                 }
 
-             }
 
-             return byt;
 
-         }
 
-         public void SetProtocolHeadBytesLength()
 
-         {
 
-         }
 
-     }
 
- }
 
 
  |