Beispiel #1
0
        };                                                 //包头字节数组

        /// <summary>
        /// 安全的发送消息,发送时停止巡检,直到返回或者超时
        /// 阻塞发送线程直到收到返回的消息或者达到超时。超时:50毫秒×40=2秒
        /// </summary>
        /// <param name="commandType"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public bool Send_Safe(Protocol_Service.CommandType commandType, byte[] buffer)
        {
            switch (commandType)
            {
            //添加子基站命令
            case Protocol_Service.CommandType.AddSon:
                //暂停巡检
                MainForm.LoopKey     = false;
                Global.Result_AddSon = false;
                this.Send(buffer);
                int tempNumAddSon = 0;
                //循环等待命令的成功返回
                while (!Global.Result_AddSon)
                {
                    Thread.Sleep(50);
                    tempNumAddSon++;
                    if (tempNumAddSon >= 40)
                    {
                        //超时,则把开关变量继续置为False后返回失败
                        Global.Result_AddSon = false;
                        //继续巡检
                        MainForm.LoopKey = true;
                        return(false);
                    }
                }
                //至此,说明成功收到返回,则把开关变量继续置为False后返回成功
                Global.Result_AddSon = false;
                //继续巡检
                MainForm.LoopKey = true;
                break;

            //删除子基站命令
            case Protocol_Service.CommandType.DelSon:
                //暂停巡检
                MainForm.LoopKey     = false;
                Global.Result_DelSon = false;
                this.Send(buffer);
                int tempNumDelSon = 0;
                //循环等待命令的成功返回
                while (!Global.Result_DelSon)
                {
                    Thread.Sleep(50);
                    tempNumDelSon++;
                    if (tempNumDelSon >= 40)
                    {
                        //超时,则把开关变量继续置为False后返回失败
                        Global.Result_DelSon = false;
                        //继续巡检
                        MainForm.LoopKey = true;
                        return(false);
                    }
                }
                //至此,说明成功收到返回,则把开关变量继续置为False后返回成功
                Global.Result_DelSon = false;
                //继续巡检
                MainForm.LoopKey = true;
                break;

            //设置时间命令
            case Protocol_Service.CommandType.SetTime:
                //暂停巡检
                MainForm.LoopKey      = false;
                Global.Result_SetTime = false;
                this.Send(buffer);
                int tempNumSetTime = 0;
                //循环等待命令的成功返回
                while (!Global.Result_SetTime)
                {
                    Thread.Sleep(50);
                    tempNumSetTime++;
                    if (tempNumSetTime >= 40)
                    {
                        //超时,则把开关变量继续置为False后返回失败
                        Global.Result_SetTime = false;
                        //继续巡检
                        MainForm.LoopKey = true;
                        return(false);
                    }
                }
                //至此,说明成功收到返回,则把开关变量继续置为False后返回成功
                Global.Result_SetTime = false;
                //继续巡检
                MainForm.LoopKey = true;
                break;

            //点亮灯命令
            case Protocol_Service.CommandType.LightUp:
                //暂停巡检
                MainForm.LoopKey      = false;
                Global.Result_LightUp = false;
                this.Send(buffer);
                int tempNumLightUp = 0;
                //循环等待命令的成功返回
                while (!Global.Result_LightUp)
                {
                    Thread.Sleep(50);
                    tempNumLightUp++;
                    if (tempNumLightUp >= 40)
                    {
                        //超时,则把开关变量继续置为False后返回失败
                        Global.Result_LightUp = false;
                        //继续巡检
                        MainForm.LoopKey = true;
                        return(false);
                    }
                }
                //至此,说明成功收到返回,则把开关变量继续置为False后返回成功
                Global.Result_LightUp = false;
                //继续巡检
                MainForm.LoopKey = true;
                break;
            }
            return(true);
        }
        private static byte[] MakeCommand(Protocol_Service.CommandType CommandType, int SourceStationID, int AimStationID, byte[] ParameterArray)
        {
            //最终返回的命令
            byte[] ResultCommand = null;
            //帧控制字
            byte control = Convert.ToByte(0x40 + (BagNum & 0x0F));

            //自增包序号
            BagNum++;
            //有效数据长度,为:命令字的长度1 + 参数数组的长度

            byte length = 1;

            if (ParameterArray != null)
            {
                length = Convert.ToByte(1 + ParameterArray.Length);
            }
            //原基站ID
            byte[] source = new byte[2];
            source[0] = Convert.ToByte((SourceStationID >> 8) & 0xFF); //高八位
            source[1] = Convert.ToByte(SourceStationID & 0xFF);        //低八位
            //目标基站ID
            byte[] aim = new byte[2];
            aim[0] = Convert.ToByte((AimStationID >> 8) & 0xFF); //高八位
            aim[1] = Convert.ToByte(AimStationID & 0xFF);        //低八位
            //帧命令字
            byte command = 0;
            //校验字
            int check = 0;

            switch (CommandType)
            {
            //巡检命令
            case Protocol_Service.CommandType.Loop:
                command = 0x50;
                break;

            //添加子节点命令
            case Protocol_Service.CommandType.AddSon:
                command = 0x54;
                break;

            //删除子节点命令
            case Protocol_Service.CommandType.DelSon:
                command = 0x54;
                break;

            //设置基站时间
            case Protocol_Service.CommandType.SetTime:
                command = 0x91;
                break;

            //发送下行短信
            case Protocol_Service.CommandType.DownMessage:
                command = 0x51;
                break;

            //点亮灯命令
            case Protocol_Service.CommandType.LightUp:
                command = 0x74;
                break;

            //得到历史数据命令
            case Protocol_Service.CommandType.GetHistoryData:
                command = 0x60;
                break;

            //清空历史数据命令
            case Protocol_Service.CommandType.DelHistoryData:
                command = 0x5F;
                break;

            //设置考勤基站人数命令
            case Protocol_Service.CommandType.SetInMineNum:
                command = 0x24;
                break;

            //设置显示信息命令
            case Protocol_Service.CommandType.SetInfo:
                command = 0x20;
                break;

            case Protocol_Service.CommandType.cset_s_num:
                command = 0x22;
                break;

            case Protocol_Service.CommandType.cset_w_num:
                command = 0x27;
                break;

            case Protocol_Service.CommandType.cset_w_no:
                command = 0x21;
                break;

            case Protocol_Service.CommandType.cset_space:
                command = 0x23;
                break;

            case Protocol_Service.CommandType.cset_up_ch:
                command = 0x1e;
                break;

            case Protocol_Service.CommandType.cset_down_ch:
                command = 0x1f;
                break;

            case Protocol_Service.CommandType.cset_reset:
                command = 0x26;
                break;
            }

            ResultCommand    = new byte[9 + length];
            ResultCommand[0] = 0xAA;
            ResultCommand[1] = 0x55;
            ResultCommand[2] = control;
            ResultCommand[3] = length;
            ResultCommand[4] = aim[0];
            ResultCommand[5] = aim[1];
            ResultCommand[6] = source[0];
            ResultCommand[7] = source[1];
            ResultCommand[8] = command;
            if (ParameterArray != null)
            {
                for (int i = 0; i < ParameterArray.Length; i++)
                {
                    ResultCommand[9 + i] = ParameterArray[i];
                }
            }
            //计算校验位
            for (int j = 2; j < ResultCommand.Length - 1; j++)
            {
                check = check ^ ResultCommand[j];
            }
            //将校验码放入最后一位
            ResultCommand[9 + length - 1] = Convert.ToByte(check);
            if (Global.Isnew)
            {
                return(ResultCommand);
            }
            else
            {
                byte[] temparr = null;
                temparr = new byte[7 + length];
                for (Int16 i = 0; i < 7 + length; i++)
                {
                    temparr[i] = ResultCommand[i + 2];
                }
                return(temparr);
            }
        }