代码拉取完成,页面将自动刷新
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Diagnostics;
namespace ControllerClient
{
class EpcReader
{
//3B9D8CAA1100002E00DD3E45
public void test()
{
readEpc(strToToHexByte("3B9D8CAA1200002E00DD3E45"));
//481F2281114E208C010E7D00
//3B9D8CAA1200002E00DD3E45 真实EPC
//3B9CBC3C110000AD00589800
}
public static TagEpcData readEpc(byte[] epcbyte)
{
try
{
Debug.WriteLine("EPC数据为:" + ToHexString(epcbyte, epcbyte.Length));
TagEpcData ted = new TagEpcData();
BitArray myBA = new BitArray(epcbyte);
BitArray tagidBa = new BitArray(32);
for (int i = 0; i < 32; i++)//0
{
tagidBa[i] = myBA[i];
}
byte[] tagidBaBytes = new byte[4];
//mybyte[0] = 0xAA;
//mybyte[1] = 0x8c;
//mybyte[2] = 0x9D;
//mybyte[3] = 0x3b;
tagidBa.CopyTo(tagidBaBytes, 0);
Array.Reverse(tagidBaBytes);
if (BitConverter.ToInt32(tagidBaBytes, 0) < 0)
{
LogHelper.Debug("读取EPC包号错误,不是本系统支持的电子签封");
return new TagEpcData();
}
Debug.WriteLine("包号为:" + BitConverter.ToInt32(tagidBaBytes, 0));
ted.Tagid = BitConverter.ToInt32(tagidBaBytes, 0);
BitArray versionidBA = new BitArray(4);
for (int i = 0; i < 4; i++)//32
{
versionidBA[i] = myBA[i + 36];
}
byte[] versionidBABytes = new byte[4];
versionidBA.CopyTo(versionidBABytes, 0);
// Array.Reverse(versionidBABytes);
Debug.WriteLine("版本号为:" + BitConverter.ToInt32(versionidBABytes, 0));
ted.Versionid = BitConverter.ToInt32(versionidBABytes, 0);
BitArray pervalueidBA = new BitArray(4);
for (int i = 0; i < 4; i++)//36
{
pervalueidBA[i] = myBA[i + 32];
}
byte[] pervalueidBABytes = new byte[4];
pervalueidBA.CopyTo(pervalueidBABytes, 0);
// Array.Reverse(pervalueidBABytes);
Debug.WriteLine("券别为:" + BitConverter.ToInt32(pervalueidBABytes, 0));
ted.Pervalueid = BitConverter.ToInt32(pervalueidBABytes, 0);
BitArray amountBA = new BitArray(16);
for (int i = 0; i < 16; i++)//40
{
amountBA[i] = myBA[i + 40];
}
byte[] amountBABytes = new byte[2];
amountBA.CopyTo(amountBABytes, 0);
Array.Reverse(amountBABytes);
Debug.WriteLine("张数为:" + BitConverter.ToInt16(amountBABytes, 0));
ted.Amount = BitConverter.ToInt16(amountBABytes, 0);
BitArray randomBA = new BitArray(8);
for (int i = 0; i < 8; i++)//56
{
randomBA[i] = myBA[i + 56];
}
byte[] randomBABytes = new byte[2];
randomBA.CopyTo(randomBABytes, 0);
Array.Reverse(randomBABytes);
Debug.WriteLine("随机数为:" + BitConverter.ToInt16(randomBABytes, 0));
ted.Random = BitConverter.ToInt16(randomBABytes, 0).ToString();
BitArray opAndcheckBA = new BitArray(24);
for (int i = 0; i < 24; i++)
{
opAndcheckBA[i] = myBA[i + 64];
}
byte[] opAndcheckBytes = new byte[3];
opAndcheckBA.CopyTo(opAndcheckBytes, 0);
//Array.Reverse(opAndcheckBytes);
Debug.WriteLine("opAndcheckBytes=" + ToHexString(opAndcheckBytes, opAndcheckBytes.Length));
string operatecountstr = "0" + ToHexString(opAndcheckBytes, opAndcheckBytes.Length).Substring(0, 3);
byte[] operatecountbytes = strToToHexByte(operatecountstr);
Array.Reverse(operatecountbytes);
int operatecount = BitConverter.ToInt16(operatecountbytes, 0);
Debug.WriteLine("操作数为:" + operatecount);
ted.Operatecount = operatecount;
string checkcode = ToHexString(opAndcheckBytes, opAndcheckBytes.Length).Substring(3, 3);
Debug.WriteLine("校验码为:" + checkcode);
ted.Checkcode = checkcode;
//BitArray operatecountBA = new BitArray(12);
//for (int i = 0; i < 12; i++)//64
//{
// operatecountBA[i] = myBA[i + 64];
//}
//byte[] operatecountBABytes = new byte[4];
//operatecountBA.CopyTo(operatecountBABytes, 0);
//Array.Reverse(operatecountBABytes);
//byte[] testb = new byte[4];
//testb[0] = 0x00;
//testb[1] = operatecountBABytes[0];
//testb[2] = operatecountBABytes[1];
//testb[3] = operatecountBABytes[2];
//Array.Reverse(testb);
//Debug.WriteLine("操作数为:" + BitConverter.ToInt32(testb, 0));
//ted.Operatecount = BitConverter.ToInt32(testb, 0);
//BitArray checkcodeBA = new BitArray(12);
//for (int i = 0; i < 12; i++)//76
//{
// checkcodeBA[i] = myBA[i + 76];
//}
//byte[] checkcodeBABytes = new byte[2];
//checkcodeBA.CopyTo(checkcodeBABytes, 0);
////Array.Reverse(checkcodeBABytes);
//Debug.WriteLine("校验码为:" + ToHexString(checkcodeBABytes, checkcodeBABytes.Length));
//ted.Checkcode = ToHexString(checkcodeBABytes, checkcodeBABytes.Length);
BitArray stutsBa = new BitArray(8);
for (int i = 0; i < 8; i++)//88
{
stutsBa[i] = myBA[i + 88];
// Debug.WriteLine(i+"===" + stutsBa[i]);
}
//10000000
Debug.WriteLine("EPC状态字:");
Debug.WriteLine(stutsBa[0] == false ? "开锁" : "关锁");
ted.Lockstuts = stutsBa[0];
Debug.WriteLine(stutsBa[2] || stutsBa[1] == false ? "正常" : "异常");
ted.LockeEx = stutsBa[2] || stutsBa[1];
Debug.WriteLine(stutsBa[3] == false ? "EPC正常" : "EPC异常");
ted.EpcEx = stutsBa[3];
Debug.WriteLine(stutsBa[6] == false ? "未上电" : "上电");
ted.HasElec = stutsBa[6];
Debug.WriteLine(stutsBa[7] == false ? "工作完成/工作未开始" : "工作中");
ted.Jobstuts = stutsBa[7];
return ted;
}
catch (Exception e)
{
LogHelper.Error("标签读取失败!"+e.StackTrace);
return new TagEpcData();
}
}
//private string checkCode(byte[] epcbytes,byte[] tid)
//{
// DESEncrypt des = new DESEncrypt();
// des.Encrypt("1B9DADFBF92E002F",);
// //byte[] Abyte = new byte[8];
// //for (int i = 0; i < 8; i++)
// //{
// // Abyte[i] = epcbytes[i];
// //}
// //byte[] Bbyte = new byte[8];
// //Bbyte[0] = epcbytes[8];
// //Bbyte[1] = (byte)epcbytes[9]>>4;
//}
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString = hexString.Insert(hexString.Length - 1, 0.ToString());
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
public static string ToHexString(byte[] bytes, int lent) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
for (int i = 0; i < lent; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
}
return hexString;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。