namespace CommonClass { /// /// 公共通用类库参数 /// public class PubCommonClass { /// /// /// public static Dictionary cmd3StartAddress = new Dictionary(); //public Dictionary<> public void SetCmd3StartAddress() { cmd3StartAddress.Add(1, 32); cmd3StartAddress.Add(2, 8); cmd3StartAddress.Add(3, 8); cmd3StartAddress.Add(4, 8); cmd3StartAddress.Add(5, 6); cmd3StartAddress.Add(6, 16); cmd3StartAddress.Add(7, 256); cmd3StartAddress.Add(8, 16); cmd3StartAddress.Add(9, 16); cmd3StartAddress.Add(10, 256); cmd3StartAddress.Add(11, 128); cmd3StartAddress.Add(12, 12); cmd3StartAddress.Add(13, 64); cmd3StartAddress.Add(14, 8); cmd3StartAddress.Add(15, 256); cmd3StartAddress.Add(16, 256); cmd3StartAddress.Add(17, 256); cmd3StartAddress.Add(18, 256); cmd3StartAddress.Add(19, 256); cmd3StartAddress.Add(20, 256); } public static byte checksumDomain(byte[] data) { int checksum = 0; for (int i = 8; i < data.Count(); i++)//这里没校验域所以不减一 { checksum += data[i]; } //foreach (byte b in data) //{ // checksum += b; //} // 取校验和的低8位 return (byte)(checksum & 0xFF); } public static bool checksumDomainReceive(byte[] data) { bool bResult = false; int checksum = 0; for (int i = 8; i < data.Count() - 1; i++)//这里有校验域所以减一 { checksum += data[i]; } //foreach (byte b in data) //{ // checksum += b; //} // 取校验和的低8位 if (data[data.Count() - 1] == (byte)(checksum & 0xFF)) bResult = true; return bResult; } public static ushort ComputeHeaderIpChecksum(byte[] header, int start, int length) { ushort word16; long sum = 0; for (int i = start; i < (length + start); i += 2) { word16 = (ushort)(((header[i] << 8) & 0xFF00) + (header[i + 1] & 0xFF)); sum += (long)word16; } while ((sum >> 16) != 0) { sum = (sum & 0xFFFF) + (sum >> 16); } sum = ~sum; return (ushort)sum; } } }