DLT645编码解码基本类

 2023-09-05 阅读 82 评论 0

摘要:C# 基本样例 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DLT645D { class DLT645 { public DLT645() { } enum RTNDATA { DATA_OK = 0, ERR_DATALEN, ERR_ST

C#

基本样例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLT645D
{
    class DLT645
    {
        public DLT645()
        {

        }

        enum RTNDATA
        {
            DATA_OK = 0,
            ERR_DATALEN,
            ERR_STARTPOS,
            ERR_CHECKSUM,
            DATA_NULL,//无标识符
        }


        int packet(DataPacketFrame inBuf, ushort len, out byte[] outBuf)
        {
            ushort i = 0;
            ushort pos = 0;
            ushort j = 0;
            byte checkSum = 0;

            outBuf = new byte[256];

            outBuf[i++] = 0xfe;
            outBuf[i++] = 0xfe;
            outBuf[i++] = 0xfe;
            outBuf[i++] = 0xfe;

            pos = i;
            //起始符
            outBuf[i++] = 0x68;
            //地址域
            for (j = 0; j < 6; j++)
            {
                outBuf[i++] = inBuf.addr[j];
            }
            //起始符
            outBuf[i++] = 0x68;
            //控制码
            outBuf[i++] = inBuf.ctr_Code;
            //数据长度
            outBuf[i++] = inBuf.DataLen;
            //数据
            for (j = 0; j < len; j++)
            {
                outBuf[i++] = (byte)(inBuf.Data[j] + 0x33);
            }
            //检验和
            for (j = pos; j < i; j++)
            {

                checkSum += outBuf[j];
            }

            outBuf[i++] = checkSum;

            outBuf[i] = 0x16;

            return 0;
        }


        RTNDATA UnPacket(byte[] inBuf, ushort len, out DataPacketFrame outBuf)
        {

            ushort i = 0;
            ushort pos = 0;
            ushort j = 0;
            byte checkSum = 0;
            outBuf = new DataPacketFrame();
            if (len < 14)
            //if (len < 6)
            {

                return RTNDATA.ERR_DATALEN;
            }
            for (j = 0; j < 4; j++)
            {
                if (inBuf[j] == 0xfe)
                {
                    i++;
                }
            }
            pos = i;

            //前导码
            if (inBuf[i] != 0x68 || inBuf[i + 7] != 0x68)
            {
                return RTNDATA.ERR_STARTPOS;
            }
            i++;

            /*addr*/
            //地址域
            for (j = 0; j < 6; j++)
            {
                outBuf.addr[j] = inBuf[i++];
            }


            i++;
            //控制码
            outBuf.ctr_Code = inBuf[i++];
            //数据长度
            outBuf.DataLen = inBuf[i++];


            //数据
            for (j = 0; j < 4; j++)
            {
                outBuf.DATA_ID[j] = (byte)(inBuf[i++] + 0x33);
            }
            for (j = 0; j < 4; j++)
            {
                outBuf.CIPHER[j] = (byte)(inBuf[i++] + 0x33);
            }

            for (j = 0; j < 4; j++)
            {
                outBuf.operator_Code[j] = (byte)(inBuf[i++] + 0x33);
            }
            for (j = 0; j < len - 12; j++)
            {
                outBuf.Data[j] = (byte)(inBuf[i++] + 0x33);
            }

            //校验和
            for (j = pos; j < i; j++)
            {
                checkSum += (byte)(inBuf[j++] + 0x33);
            }
            outBuf.CheckSum = inBuf[i];

            if(checkSum!=inBuf[i])
            {
                return RTNDATA.ERR_CHECKSUM;
            }else
            {
                outBuf.CheckSum=inBuf[i];
            }

            return RTNDATA.DATA_OK;
        }

        public int testPACKET()
        {

            ushort i = 0;
            ushort len = 0;
            /*FE FE FE FE 68 18 00 00 00 00 00 68 14 10 01 01 00 04 01 00 00 00 78 56 34 12 05 07 08 15 81 16*/
            DataPacketFrame inBuf = new DataPacketFrame();
            byte[] outBuf = new byte[256];

            byte[] addr = { 0x18, 0x00, 0x00, 0x00, 0x00, 0x00 };
            byte[] data = { 0x01, 0x01, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12, 0x05, 0x07, 0x08, 0x15 };

            for (i = 0; i < 6; i++)
            {
                inBuf.addr[i] = addr[i];
            }
            inBuf.ctr_Code = 0x14;
            inBuf.DataLen = 0x10;

            len = (ushort)data.Length;

            for (i = 0; i < len; i++)
            {
                inBuf.Data[i] = data[i];
            }

            packet(inBuf, len, out outBuf);

            for (i = 0; i < 32; i++)
            {
                Console.Write(outBuf[i].ToString("X") + " ");
            }
            Console.WriteLine();
            return 0;
        }

        //测试解帧
        public int testUNPACKET()
        {

            DataPacketFrame outBuf;
            byte[] inBuf = { 0xFE, 0xFE, 0xFE, 0xFE, 0x68, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x14, 0x10, 0x01, 0x01, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12, 0x05, 0x07, 0x08, 0x15, 0x81, 0x16 };
            //byte[] inBuf = { 0xFE, 0xFE, 0xFE, 0xFE, 0x68, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x68, 0x91, 0x09, 0x33, 0x33, 0x34, 0x33, 0x9A, 0x4C, 0x44, 0x33, 0x36, 0x60, 0x16 };
            ushort i = 0;
            while (inBuf[i] != 0x68)
            {
                i++;
            }
            ushort DataLenPos = (ushort)(i + 9);
            ushort datalen_INT = (ushort)inBuf[DataLenPos];
            UnPacket(inBuf, datalen_INT, out outBuf);

            Console.Write("地址域为:");
            for (int j = 0; j < 6; j++)
            {
                Console.Write(outBuf.addr[j].ToString("X2") + " ");
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("控制码为:" + outBuf.ctr_Code.ToString("X"));
            Console.WriteLine(Environment.NewLine);

            Console.Write("数据长度为:");
            Console.Write(outBuf.DataLen.ToString("X2") + " ");
            Console.WriteLine(Environment.NewLine);

            Console.Write("数据标识为:");
            for (int j = 0; j < 4; j++)
            {
                Console.Write(outBuf.DATA_ID[j].ToString("X2"));
            }
            Console.WriteLine(Environment.NewLine);
            Console.Write("密码权限为:");
            for (int j = 0; j < 4; j++)
            {
                Console.Write(outBuf.CIPHER[j].ToString("X2"));
            }
            Console.WriteLine(Environment.NewLine);
            Console.Write("操作者代码:");
            for (int j = 0; j < 4; j++)
            {
                Console.Write(outBuf.operator_Code[j].ToString("X2"));
            }
            Console.WriteLine(Environment.NewLine);
            Console.Write("数据:");
            for (int j = 0; j < datalen_INT - 12; j++)
            {
                Console.Write(outBuf.Data[j].ToString("X2"));
            }
            Console.WriteLine(Environment.NewLine);
            Console.Write("数据校验码:");
            Console.Write(outBuf.CheckSum.ToString("X2"));
            Console.WriteLine(Environment.NewLine);

            return 0;
        }

        class DataPacketFrame
        {
            public byte[] addr = new byte[6];
            public byte ctr_Code;
            public byte DataLen;
            public byte[] Data = new byte[256];
            public byte CheckSum;
            public byte[] CIPHER = new byte[4];//密码权限
            public byte[] DATA_ID = new byte[4];//数据标识
            public byte[] operator_Code = new byte[4];
        }
    }
}
 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/705.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息