Newer
Older
SensorHub / SensorHub.Servers / CasicSender.cs
root on 17 Sep 2021 5 KB first commit
using System;
using System.Collections.Generic;
using SensorHub.Utility;
using SensorHub.Servers.Commands.CASICCommands;
using System.Threading;
using System.Configuration;
using System.IO;
using Microsoft.Win32;

namespace SensorHub.Servers
{
    public class CasicSender
    {
        private CasicServer server;

        public CasicSender(CasicServer server)
        {
            this.server = server;
        }

        public byte[] buildHeaderFrame(String devCode = "000000000000")
        {
            byte[] frame = { 0xA3, 0x20, 
                               0x00, 0x18,                          //长度
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //设备编号
                               0x00,                                //通信方式
                               0x00, 0x00,                          //目标节点地址
                               0x00, 0x00,                          //PDUType
                               0x00,                                //Seq
                           };

            byte[] btDevCode = strToHexByte(devCode);
            btDevCode.CopyTo(frame, 4);
            Array.Copy(btDevCode, 4, frame, 11, 2);

            return frame;
        }

        public byte[] buildConfigFrame(String devCode = "000000000000")
        {
            byte[] frame = { 0xA3, 0x20, 
                               0x00, 0x18,                          //长度
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //设备编号
                               0x00,                                //通信方式
                               0x00, 0x00,                          //目标节点地址
                               0x00, 0x00,                          //PDUType
                               0x00,                                //Seq
                               0x10, 0x00, 0x00, 0x51, 0x00, 0x06,  //时间戳
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                           };

            byte[] btDevCode = strToHexByte(devCode);
            btDevCode.CopyTo(frame, 4);
            Array.Copy(btDevCode, 4, frame, 11, 2);

            SystemTimeConfig sysTimeConfig = new SystemTimeConfig(null);
            byte[] btConfig = sysTimeConfig.getConfig(new byte[0]);
            btConfig.CopyTo(frame, 16);

            return frame;
        }

        public byte[] buildQueryFrame(String devCode)
        {
            byte[] frame = { 0xA3, 0x20, 
                               0x00, 0x10,                          //长度
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //设备编号
                               0x01,                                //通信方式
                               0x00, 0x00,                          //目标节点地址
                               0x00, 0x00,                          //PDUType
                               0x01,                                //Seq
                               0x20, 0x00, 0x00, 0x01  //服务器实时查询数据指令
                           };

            byte[] btDevCode = strToHexByte(devCode);
            btDevCode.CopyTo(frame, 4);
            Array.Copy(btDevCode, 4, frame, 11, 2);

            return frame;
        }

        public byte[] build433RspConfigFrame(String devCode)
        {
            byte[] frame = { 0xA3, 0x20, 
                               0x00, 0x27,                          //长度
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //设备编号
                               0x00,                                //通信方式
                               0x00, 0x00,                          //目标节点地址
                               0x00, 0x00,                          //PDUType
                               0x01,                                //Seq
                               0x10, 0x00, 0x00, 0x51, 0x00, 0x06,  //时间戳
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                               0x60, 0x00, 0x03, 0x00, 0x00, 0x02, //数据接收状态
                               0x00, 0x01,
                               0x60, 0x00, 0x06, 0x00, 0x00, 0x01, //升级状态,0表示不需要升级,1表示需要升级
                               0x00
                           };

            byte[] btDevCode = strToHexByte(devCode);
            btDevCode.CopyTo(frame, 4);
            Array.Copy(btDevCode, 4, frame, 11, 2);

            return frame;
        }

        public byte[] buildUpgradeConfigFrame(String devCode)
        {
            byte[] frame = { 0xA3, 0x20, 
                               0x00, 0x13,                          //长度
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //设备编号
                               0x00,                                //通信方式
                               0x00, 0x00,                          //目标节点地址
                               0x00, 0x00,                          //PDUType
                               0x01,                                //Seq
                               0x60, 0x00, 0x06, 0x00, 0x00, 0x01, //升级状态,0表示不需要升级,1表示需要升级
                               0x00
                           };

            byte[] btDevCode = strToHexByte(devCode);
            btDevCode.CopyTo(frame, 4);
            Array.Copy(btDevCode, 4, frame, 11, 2);

            return frame;
        }

        /// <summary>
        /// 字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        private byte[] strToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += "0";
            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;
        }
    }
}