Newer
Older
CaptureEye / IrisCtrl / USBHID / USBMessage.cs
yxw on 14 Aug 2020 6 KB first commt
using System;

namespace IrisCtrl.USBHID
{
    public class USBMessage
    {
        private static byte[] headStr = { 0x00, 0xA3, 0x01, 0x03 };
        private static byte[] headIdStr = { 0x00, 0xA3, 0x01, 0x08 };
        /// <summary>
        /// 查询可见灯状态
        /// </summary>
        /// <returns></returns>
        public static byte[] SearchVisibleLightMessage()
        {
            byte[] bt = new byte[12];
            headStr.CopyTo(bt,0);
            bt[4] = 0x00;
            bt[5] = 0x00;

            byte code= getCheckCode(bt);
            bt[6] = code;

            return bt;
        }
        /// <summary>
        /// 设置可见灯状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static byte[] SetVisibleLightMessage(String status)
        {
            byte[] bt = new byte[12];
            headStr.CopyTo(bt, 0);
            bt[4] = 0x02;
            if (status == "open")
                bt[5] = 0x00;
            else
                bt[5] = 0x01;

            byte code = getCheckCode(bt);
            bt[6] = code;

            return bt;
        }
        /// <summary>
        /// 查询指示灯状态
        /// </summary>
        /// <returns></returns>
        public static byte[] SearchIndicatorMessage()
        {
            byte[] bt = new byte[12];
            headStr.CopyTo(bt, 0);
            bt[4] = 0x04;
            bt[5] = 0x00;

            byte code = getCheckCode(bt);
            bt[6] = code;
            return bt;
        }
        /// <summary>
        /// 设置指示灯状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static byte[] SetIndicatortMessage(String status)
        {
            byte[] bt = new byte[12];
            headStr.CopyTo(bt, 0);
            bt[4] = 0x06;
            if (status == "red")
                bt[5] = 0x04;
            else if (status == "green")
                bt[5] = 0x02;
            else if (status == "blue")
                bt[5] = 0x01;

            byte code = getCheckCode(bt);
            bt[6] = code;
            return bt;
        }
        /// <summary>
        /// 查询指示灯姿态
        /// </summary>
        /// <returns></returns>
        public static byte[] SearchEqPostureMessage()
        {
            byte[] bt = new byte[12];
            headStr.CopyTo(bt, 0);
            bt[4] = 0xB0;
            bt[5] = 0x00;

            byte code = getCheckCode(bt);
            bt[6] = code;
            return bt;
        }
        /// <summary>
        /// 设置设备ID
        /// </summary>
        /// <param name="equipmentID"></param>
        /// <returns></returns>
        public static byte[] SetEqIDMessage(String equipmentID)
        {
            byte[] bt = new byte[12];
            headIdStr.CopyTo(bt, 0);
            bt[4] = 0xC0;

            byte[] btDevId = new byte[6];
            btDevId[0] = byte.Parse(equipmentID.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            btDevId[1] = byte.Parse(equipmentID.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            btDevId[2] = byte.Parse(equipmentID.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
            btDevId[3] = byte.Parse(equipmentID.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
            btDevId[4] = byte.Parse(equipmentID.Substring(8, 2), System.Globalization.NumberStyles.HexNumber);
            btDevId[5] = byte.Parse(equipmentID.Substring(10, 2), System.Globalization.NumberStyles.HexNumber);
            btDevId.CopyTo(bt,5);

            byte code = getCheckCode(bt);
            byte[] result = new byte[bt.Length + 1];
            bt.CopyTo(result, 0);
            result[result.Length - 1] = code;

            return result;
        }
        /// <summary>
        /// 计算验证码
        /// </summary>
        /// <param name="btArray"></param>
        /// <returns></returns>
        public static byte getCheckCode(byte[] btArray)
        {
            byte result = 0x00;
            foreach(byte bt in btArray)
            {
                result += bt;
            }

            return result;
        }
        /// <summary>
        /// 解析设备返回信息
        /// </summary>
        /// <param name="btArray"></param>
        /// <param name="idBt"></param>
        /// <returns></returns>
        public static int GetMessage(byte[] btArray,out byte[] idBt)
        {
            int result = -1;
            byte bt = btArray[4];
            byte par = btArray[5];
            idBt = new byte[6];

            switch (bt)
            {
                case 0x01:
                    if (par == 0x00)
                        result = 0;
                    else if (par == 0x01)
                        result =1;
                    break;
                case 0x05:
                    if (par == 0x04)
                        result = 2;
                    else if (par == 0x02)
                        result = 3;
                    else if (par == 0x01)
                        result = 4;
                    break;
                case 0xB1:
                    if (par == 0x00)
                        result = 5;
                    else if (par == 0x01)
                        result = 6;
                    break;
                case 0xC1:
                    idBt = GetIdMessage(btArray);
                    result = 7;
                    break;
                default:
                    break;
            }
            return result;
        }
        /// <summary>
        /// 截取设备ID
        /// </summary>
        /// <param name="btArray"></param>
        /// <returns></returns>
        public static byte[] GetIdMessage(byte[] btArray)
        {
            byte[] idBt = new byte[6];
            Array.Copy(btArray, 5, idBt, 0, 6);
            return idBt;
        }

        public enum USBENUM
        {
            VISIBLE_LIGHT_OPEN = 0, //可见灯打开
            VISIBLE_LIGHT_CLOSE = 1, //可见灯关闭

            INDICATOR_LIGHT_RED = 2, //指示灯红灯亮
            INDICATOR_LIGHT_GREEN = 3, //指示灯绿灯亮
            INDICATOR_LIGHT_BULE = 4, //指示灯蓝灯亮

            EQUIPMENT_POSTURE_NORMAL = 5, //设备姿态正常
            EQUIPMENT_POSTURE_INVERT = 6, //设备姿态导致

            EQUIPMENT_ID=7,//返回设备ID
        }


    }
}