//确定按钮 private void Determine(object sender, RoutedEventArgs e) { String password = NonPublicInformationPassword.Password; if ("111" == password) { try { //与U盘交互 //1.打包协议到result; byte[] send = null; //获取电脑的uuid字节数组-即加密狗的内容 ProtocolConstant.USB_DOG_CONTENT = Encoding.UTF8.GetBytes(Get_UUID()); new MakerUSBDogFrame().PackData(ref send, new byte[] { 0xF0 }, ProtocolConstant.USB_DOG_CONTENT); //Console.WriteLine("加密后的报文" + ProtocolUtil.ByteToStringOk(send)); //byte[] test = null; //new MakerUSBDogFrame().PackData(ref test, new byte[] { 0xF0 }, Encoding.UTF8.GetBytes("hello")); logger.Debug("激活:" + ProtocolUtil.ByteToStringOk(send)); //2.判断当前是否已经连接过串口 SerialPortUtil.CheckPort(); if (SerialPortUtil.portName == "") { MessageBoxX.Warning(LanguageUtils.ConvertLanguage("请先连接串口", "Please connect the serial port first")); return; } if (SerialPortUtil.SerialPort != null) { SerialPortUtil.SerialPort = null; } if (serialPort == null) { serialPort = SerialPortUtil.ConnectSerialPort(OnPortDataReceived); try { serialPort.Open(); } catch (UnauthorizedAccessException ex) { MessageBoxX.Warning(LanguageUtils.ConvertLanguage("串口被占用", "Serial port is occupied")); return; } catch (IOException ex) { MessageBoxX.Warning(LanguageUtils.ConvertLanguage("串口不存在", "Serial port does not exist")); this.Close(); return; } serialPort.Write(send, 0, send.Length); //发送的定时器 threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ReissueThreeTimes), send, 500, 500); } } catch (Exception ex) { logger.Error("激活异常"); //Console.WriteLine("激活异常"); } //else //{ // if (!serialPort.IsOpen) // { // try // { // serialPort.Open(); // } // catch (UnauthorizedAccessException ex) // { // MessageBoxX.Warning("串口被占用", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Warning); // return; // } // catch (IOException ex) // { // MessageBoxX.Warning("串口不存在", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Warning); // return; // } // } //} } else { MessageBoxX.Warning(LanguageUtils.ConvertLanguage("密码错误,请重新输入", "Password error, please retype")); } }