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.

118 lines
3.5 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;
}
/// <summary>
/// 数据包发送-计算校验和
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 数据包接收-判断校验和是否正确-即判断数据是否完整
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
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;
}
}
}