| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 | 
							- using Aitex.Core.Utilities;
 
- using log4net.Core;
 
- using MECF.Framework.Common.Net;
 
- using MECF.Framework.Common.Utilities;
 
- using System;
 
- using System.Collections.Generic;
 
- using System.Linq;
 
- using System.Runtime.Remoting.Messaging;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- using System.Windows.Forms;
 
- namespace MECF.Framework.Common.Device.PowerSupplier
 
- {
 
-     public class PowerSupplierMessage : INetMessage<PowerSupplierCommand>
 
-     {
 
-         #region 属性
 
-         public int ProtocolHeadBytesLength { get; set; } = 3;
 
-         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()
 
-         {
 
-             if (ProtocolHeadBytesLength > 1)
 
-             {
 
-                 //第二个字节高位为0x80,设备报错
 
-                 if ((HeadBytes[1] & 0x80) == 0x80)
 
-                 {
 
-                     ErrorCode = (int)NetErrorCode.DeviceError;
 
-                     if (HeadBytes.Length > 2)
 
-                     {
 
-                         ErrorMsg = GetDeviceMessage(HeadBytes[2]);
 
-                     }
 
-                     else
 
-                     {
 
-                         ErrorMsg = GetDeviceMessage(ContentBytes[0]);
 
-                     }
 
-                     return false;
 
-                 }
 
-                 //数据长度不一致
 
-                 if (HeadBytes[1] == 0x03)
 
-                 {
 
-                     byte contentLength = HeadBytes[2];
 
-                     if (ContentBytes.Length - 2 != contentLength)
 
-                     {
 
-                         ErrorCode = (int)NetErrorCode.InvalidData;
 
-                         ErrorMsg = EnumUtil.GetEnumDescription(NetErrorCode.InvalidData);
 
-                         return false;
 
-                     }
 
-                 }
 
-                 //CRC检验
 
-                 byte[] data = new byte[HeadBytes.Length + ContentBytes.Length - 2];
 
-                 Array.Copy(HeadBytes, 0, data, 0, HeadBytes.Length);
 
-                 Array.Copy(ContentBytes, 0, data, HeadBytes.Length, ContentBytes.Length - 2);
 
-                 byte[] crc16 = ModbusUtility.CalculateCrc(data);
 
-                 if (crc16.Length < 2 || (crc16[0] != ContentBytes[ContentBytes.Length - 2] || crc16[1] != ContentBytes[ContentBytes.Length - 1]))
 
-                 {
 
-                     ErrorCode = (int)NetErrorCode.CRCCheckError;
 
-                     ErrorMsg = EnumUtil.GetEnumDescription(NetErrorCode.CRCCheckError);
 
-                     return false;
 
-                 }
 
-                 return true;
 
-             }
 
-             else
 
-             {
 
-                 //第一个字节高位为0x80,设备报错
 
-                 if ((HeadBytes[0] & 0x80) == 0x90)
 
-                 {
 
-                     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[1]==(byte)(0x80|SendBytes[1]));
 
-         }
 
-         public bool ConfirmResponseResult()
 
-         {
 
-             //指令码与发送指令码一致
 
-             if (HeadBytes[1] == SendBytes[1])
 
-             {
 
-                 return true;
 
-             }
 
-             return false;
 
-         }
 
-         public PowerSupplierCommand Decode() 
 
-         {
 
-             PowerSupplierCommand command = new PowerSupplierCommand();
 
-             if (HeadBytes[1] == 0x03)
 
-             {
 
-                 int registerCount = HeadBytes[2] / 2;
 
-                 command.Datas = byteTransform.TransUInt16(ContentBytes, 0, registerCount);
 
-             }
 
-             return command;
 
-         }
 
-         public int GetContentLengthByHeadBytes()
 
-         {
 
-             if (HeadBytes[1] == 0x03)
 
-             {
 
-                 //2*寄存器+CRC 2个字节
 
-                 return HeadBytes[2] + 2;
 
-             }
 
-             else if (HeadBytes[1] == 0x06)
 
-             {
 
-                 //寄存器地址2个字节+寄存器数值2个字节+CRC 2个字节
 
-                 return 6;
 
-             }
 
-             else if (HeadBytes[1] == 0x10)
 
-             {
 
-                 //寄存器地址2个字节+寄存器数量2个字节+CRC 2个字节
 
-                 return 6;
 
-             }
 
-             //第二个字节高位为0x80,设备报错
 
-             else if ((HeadBytes[1] & 0x80) == 0x80)
 
-             {
 
-                 //仅CRC校验码
 
-                 if ((HeadBytes[1] & 0x0F) == 0x03)
 
-                 {
 
-                     return 2;
 
-                 }
 
-                 else
 
-                 {
 
-                     //异常码1个字节 CRC 2个字节
 
-                     return 3;
 
-                 }
 
-             }
 
-             else
 
-             {
 
-                 return 0;
 
-             }
 
-         }
 
-         public int GetHeadBytesIdentity()
 
-         {
 
-             return 0;
 
-         }
 
-         public byte[] Code(PowerSupplierCommand data) 
 
-         {
 
-             byte[] byt = null; 
 
-             //读取指令
 
-             if(data.CommandCode==0x03)
 
-             {
 
-                 byt= new byte[8];
 
-                 byt[0] = data.Channel;
 
-                 byt[1] = data.CommandCode;
 
-                 Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 2, 2);
 
-                 Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 4, 2);
 
-                 //ushort crc16= Crc16.CRC16_Modbus(byt);
 
-                 byte[] crcSourceByt = new byte[byt.Length - 2];
 
-                 Array.Copy(byt,0,crcSourceByt,0,byt.Length-2);
 
-                 byte[] crc = ModbusUtility.CalculateCrc(crcSourceByt);
 
-                 Array.Copy(crc, 0, byt, 6, 2);
 
-             }
 
-             //写单个寄存器地址
 
-             else if(data.CommandCode==0x06)
 
-             {
 
-                 byt = new byte[8];
 
-                 byt[0] = data.Channel;
 
-                 byt[1] = data.CommandCode;
 
-                 Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 2, 2);
 
-                 Array.Copy(byteTransform.GetBytes(data.Datas[0]), 0, byt, 4, 2);
 
-                 byte[] crcSourceByt = new byte[byt.Length - 2];
 
-                 Array.Copy(byt, 0, crcSourceByt, 0, byt.Length - 2);
 
-                 byte[] crc = ModbusUtility.CalculateCrc(crcSourceByt);
 
-                 Array.Copy(crc, 0, byt, 6, 2);
 
-             }
 
-             else if(data.CommandCode==0x10)
 
-             {
 
-                 byt = new byte[2 * data.Datas.Length + 9];
 
-                 byt[0] = data.Channel;
 
-                 byt[1] = data.CommandCode;
 
-                 Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 2, 2);
 
-                 Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 4, 2);
 
-                 byt[6] = (byte)(2 * data.Datas.Length);
 
-                 for (int i = 0; i < data.Datas.Length; i++)
 
-                 {
 
-                     Array.Copy(byteTransform.GetBytes(data.Datas[i]), 0, byt, 7 + i * 2, 2);
 
-                 }
 
-                 byte[] crcSourceByt = new byte[byt.Length - 2];
 
-                 Array.Copy(byt, 0, crcSourceByt, 0, byt.Length - 2);
 
-                 byte[] crc = ModbusUtility.CalculateCrc(crcSourceByt);
 
-                 Array.Copy(crc, 0, byt, byt.Length - 2, 2);
 
-                 //byt = new byte[2 * data.Datas.Length + 6];
 
-                 //byt[0] = data.CommandCode;
 
-                 //Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 1, 2);
 
-                 //Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 3, 2);
 
-                 //byt[5] = (byte)(2 * data.Datas.Length);
 
-                 //for (int i = 0; i < data.Datas.Length; i++)
 
-                 //{
 
-                 //    Array.Copy(byteTransform.GetBytes(data.Datas[i]), 0, byt, 6 + i * 2, 2);
 
-                 //}
 
-             }
 
-             return byt;
 
-         }
 
-         public void SetProtocolHeadBytesLength()
 
-         {
 
-             if (SendBytes[1] == 0x03)
 
-             {
 
-                 ProtocolHeadBytesLength = 3;
 
-             }
 
-             else if (SendBytes[1] == 0x06)
 
-             {
 
-                 ProtocolHeadBytesLength = 2;
 
-             }
 
-             else if (SendBytes[1] == 0x10)
 
-             {
 
-                 ProtocolHeadBytesLength = 2;
 
-             }
 
-             else
 
-             {
 
-                 ProtocolHeadBytesLength = 0;
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |