コード例 #1
0
ファイル: GaoxiaoUtility.cs プロジェクト: zgren/dp2
        public static byte[] EncodeGaoxiapEpc(GaoxiaoEpcInfo info)
        {
            List <byte> result = new List <byte>();

            // *** 第一字节
            byte first = 0;

            // 安全位
            if (info.Lending)
            {
                first = 0x80;
            }
            // 预留位
            first |= (byte)((info.Reserve & 0x03) << 4);

            // 分拣信息
            first |= (byte)(info.Picking & 0xf);

            result.Add(first);

            // *** 第二字节
            byte second = 0;

            // 探测 PII 可用的编码方式
            second = (byte)((DetectEncodingType(info.PII) << 6) & 0xc0);

            // 数据模型版本
            // 值 0 代表第一版
            if (info.Version == 0)
            {
                throw new Exception($"版本号应该从 1 开始");
            }

            second |= (byte)((info.Version - 1) & 0x3f);
            result.Add(second);

            // *** EPC 的第 3-4 字节
            var two_bytes = EncodeContentParameter(info.ContentParameters);

            if (two_bytes.Length != 2)
            {
                throw new Exception($"EncodeContentParameter() 编码的结果应该是 2 字节(但现在是 {two_bytes.Length} 字节)");
            }
            result.AddRange(two_bytes);

            // *** EPC 的第 5-12/16/18 字节

            byte[] pii_bytes = null;
            if (info.EncodingType == 0)
            {
                pii_bytes = EncodeIpc96bit(info.PII);
            }
            else if (info.EncodingType == 1)
            {
                pii_bytes = EncodeIpc128bit(info.PII);
            }
            else if (info.EncodingType == 2)
            {
                pii_bytes = Encoding.UTF8.GetBytes(info.PII);
            }

            result.AddRange(pii_bytes);

            // TODO: 检查 result 的字节数是否为偶数,如果必要补齐偶数

            return(result.ToArray());
        }
コード例 #2
0
ファイル: GaoxiaoUtility.cs プロジェクト: zgren/dp2
        // 解码
        public static GaoxiaoEpcInfo DecodeGaoxiaoEpc(byte[] data)
        {
            GaoxiaoEpcInfo result = new GaoxiaoEpcInfo();

            if (data.Length < 1)
            {
                throw new ArgumentException("data 的字节数不应小于 1");
            }
            byte first = data[0];

            // 安全位
            result.Lending = (first & 0x80) != 0;

            // 预留位
            result.Reserve = (first >> 4) & 0x03;

            // 分拣信息
            result.Picking = first & 0x0f;

            if (data.Length < 2)
            {
                throw new ArgumentException("data 的字节数不应小于 2");
            }
            byte second = data[1];

            // 编码方式
            result.EncodingType = (second >> 6) & 0x03;

            // 数据模型版本
            // 值 0 代表第一版
            result.Version = (second & 0x3f) + 1;

            byte[] content_parameter = new byte[2];
            content_parameter[0] = data[2];
            content_parameter[1] = data[3];

            // bit 高 --> bit 低
            // 31 30 29 28 27 26 24 16 , 15 14 12 11 6 5 4 3
            result.ContentParameters = DecodeContentParameter(content_parameter);

            // *** EPC 的第 5-12/16/18 字节
            byte[] rest = new byte[data.Length - 4];
            Array.Copy(data, 4, rest, 0, data.Length - 4);
            if (result.EncodingType == 0)
            {
                result.PII = DecodeIpc96bit(rest);
            }
            else if (result.EncodingType == 1)
            {
                result.PII = DecodeIpc128bit(rest);
            }
            else if (result.EncodingType == 2)
            {
                result.PII = Encoding.UTF8.GetString(rest);
            }
            else
            {
                throw new Exception("目前暂不支持第四种 PII 编码方式");
            }

            return(result);
        }