Ejemplo n.º 1
0
        //蓝牙监听服务
        private void Bluetooth_ValueChanged(MsgType type, string str, byte[] data = null)
        {
            if (str.Equals("Disconnected") || str.Equals("Unreachable") || str.Equals("Success"))
            {
                return;
            }

            byte[] b = ConvertHelper.StringToByte(str);

            //仪器基本信息  && 版本号
            if (str.Length == 44 && b[1] == 0x03)
            {
                byte[] bt = new byte[4];
                Array.Copy(b, 3, bt, 0, 4);
                string version = Encoding.ASCII.GetString(bt);

                _setupForm.SetLabelText(version);
                return;
            }

            //读仪器芯片ID
            if (str.Length == 86 && b[1] == 0x03)
            {
                if (!Modbus.ReceiveByteArray(b))
                {
                    return;
                }

                ChipID = System.Text.Encoding.Default.GetString(Modbus.Datas);
                if (ChipID == null || ChipID.Equals(""))
                {
                    return;
                }

                string msg = _setupRepository.VerifyID(_downloadFile, ChipID);

                if (msg != "")
                {
                    _setupForm.ShowToast_InSetup("提示", msg, 2);

                    if (msg.Equals("下载成功"))
                    {
                        _setupForm.ProgressSerChanged();

                        //启动程序升级
                        if (!_setupRepository.StartFirmUpdata())
                        {
                            _setupForm.ShowToast_InSetup("提示", "发送升级命令失败", 2);
                        }
                        return;
                    }
                    return;
                }
            }

            //启动升级返回结果
            if (str.Contains("01-06-11-00"))
            {
                if (Convert.ToInt32(b[5]) != 1)
                {
                    _setupForm.ShowToast_InSetup("提示", "启动固件升级失败", 2);
                    _setupForm.ProgressUpChanged();
                    return;
                }

                _setupForm.ShowToast_InSetup("提示", "正在更新固件设备,请耐心等待。", 2);
                //首次发送固件升级数据
                msg = _firmUpdata.sendDrviceUpdate(false);
                if (msg != "")
                {
                    _setupForm.ShowToast_InSetup("提示", msg, 2);
                }
                _setupForm.ProgressUp(_firmUpdata.percentage);
                return;
            }

            //循环发送更新数据
            if (str.Contains("01-06-11-02"))
            {
                if (Convert.ToInt32(b[10]) == 1)
                {
                    msg = _firmUpdata.sendDrviceUpdate(false);
                    if (msg != "")
                    {
                        _setupForm.ShowToast_InSetup("提示", msg, 2);
                    }
                    _setupForm.ProgressUp(_firmUpdata.percentage);
                }
                else
                {
                    msg = _firmUpdata.sendDrviceUpdate(true);
                    if (msg != "")
                    {
                        _setupForm.ShowToast_InSetup("提示", msg, 2);
                    }
                    _setupForm.ProgressUp(_firmUpdata.percentage);
                }
                return;
            }

            //整体CRC校验
            if (str.Contains("01-06-11-04"))
            {
                if (Convert.ToInt32(b[5]) == 1)
                {
                    _setupForm.ShowToast_InSetup("提示", "固件更新成功!", 2);
                }
                else
                {
                    _setupForm.ShowToast_InSetup("提示", "固件更新失败!", 2);
                }

                if (File.Exists(XmlHelper.getVer("VER")))
                {
                    File.Delete(XmlHelper.getVer("VER"));
                }
                _setupForm.ProgressUp(100);
                _setupForm.ProgressUpChanged();
                SetUpText();
                _setupForm.ShowToast_InSetup("提示", "请重启蓝牙设备,已刷新蓝牙状态!", 7);
                return;
            }
        }