using BatCharging.Model; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BatCharging.Service { public static class PublicProgram { public static string failName01 = null; public static string failName02 = null; public static string failName03 = null; public static string failName04 = null; public static List APCI(APDUModel baseApci) { List lstResult = null; if (baseApci != null) { lstResult = new List(); //起始域 lstResult.Add(baseApci.OriginDomain1); lstResult.Add(baseApci.OriginDomain2); //长度域 byte[] packLens = BitConverter.GetBytes(baseApci.LengthDomain); lstResult.AddRange(packLens); //信息域 lstResult.Add(baseApci.infoDomain); //序列号域 lstResult.Add(baseApci.SerialNumberDomain); //源地址 byte[] packSrcs = BitConverter.GetBytes(baseApci.CMD); lstResult.AddRange(packSrcs); //数据域 lstResult.AddRange(baseApci.dataDomain); baseApci.checksumDomain = checksumDomain(lstResult.ToArray()); //校验和域 lstResult.Add(baseApci.checksumDomain); } return lstResult; } /// /// 数据包发送-计算校验和 /// /// /// public static byte checksumDomain(byte[] data) { int checksum = 0; for (int i = 6; 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; } } }