You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.1 KiB
109 lines
3.1 KiB
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<byte> APCI(APDUModel baseApci)
|
|
{
|
|
List<byte> lstResult = null;
|
|
if (baseApci != null)
|
|
{
|
|
lstResult = new List<byte>();
|
|
//起始域
|
|
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 = 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;
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|