代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.IO.Ports;
using MyGlib;
using Microsoft.Win32;
namespace DeviceUpdata
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string mFile;
private bool mWorking=false;
private OkBootDevice mDev;
public MainWindow()
{
InitializeComponent();
Status.Content = "";
mDev = new OkBootDevice();
mDev.AddCallBack(UpdataChanged);
}
/// <summary>
/// 升级进度有变化,运行在mDev的私有线程,无法直接操作控件
/// </summary>
/// <param name="Total">总字节数</param>
/// <param name="Done">已完成字节数</param>
/// <returns>true 事件不再传递,false 事件继续传递</returns>
private bool UpdataChanged(OkBootUpdater.event_t errorCode, int Total, int Done)
{
// 委托UI线程执行 (应该也可以使用发送信号量/消息队列的方式进行线程同步)
Dispatcher.Invoke(
new Action(
delegate
{
switch (errorCode)
{
case OkBootUpdater.event_t.UPDATA_PROGESS: // 一切正常,更新进度
progress_bar.Maximum = Total;
progress_bar.Value = Done;
Status.Content = "" + Done + "Bytes/" + Total + "Bytes. "+ Done*100/ Total +"%. ";
break;
case OkBootUpdater.event_t.UPDATA_COMPLATED:
Status.Content = "Updata complated.";
Cancel.IsEnabled = true;
Bt_Update.IsEnabled = true;
mWorking = false;
mDev.DisConnect();
break;
case OkBootUpdater.event_t.UPDATA_CHKERR:
Status.Content = "Upgrade failed. Check error. The firmware of the device may be damaged.";
Cancel.IsEnabled = true;
Bt_Update.IsEnabled = true;
mWorking = false;
mDev.DisConnect();
break;
case OkBootUpdater.event_t.UPDATA_DATAERR:
Status.Content = "Upgrade failed. file error.";
Cancel.IsEnabled = true;
Bt_Update.IsEnabled = true;
mWorking = false;
mDev.DisConnect();
break;
case OkBootUpdater.event_t.UPDATA_SIZEERR:
Status.Content = "Upgrade failed. Firmware size error.";
Cancel.IsEnabled = true;
Bt_Update.IsEnabled = true;
mWorking = false;
mDev.DisConnect();
break;
case OkBootUpdater.event_t.UPDATA_TIMEOUT:
Status.Content = "Upgrade failed. Timeout.";
Cancel.IsEnabled = true;
Bt_Update.IsEnabled = true;
mWorking = false;
mDev.DisConnect();
break;
case OkBootUpdater.event_t.UPDATA_UNKNOWCMD:
Status.Content = "Upgrade failed. Unknow cmd.";
Cancel.IsEnabled = true;
Bt_Update.IsEnabled = true;
mWorking = false;
mDev.DisConnect();
break;
case OkBootUpdater.event_t.UPDATA_NODEVICE:
Status.Content = "Upgrade failed. Make sure the device is connected.";
Cancel.IsEnabled = true;
Bt_Update.IsEnabled = true;
mWorking = false;
mDev.DisConnect();
break;
default: break;
}
}
));
return false;
}
// 关闭按钮
private void Button_Cancel_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
PortList.ItemsSource = mDev.GetPortNames();
}
private void Window_Closed(object sender, EventArgs e)
{
mDev.DisConnect();
}
private void Bt_Update_Click(object sender, RoutedEventArgs e)
{
Cancel.IsEnabled = false;
Bt_Update.IsEnabled = false;
mFile = Tb_OupFile.Text;
mDev.SetBaudRate(int.Parse(Tb_BaudRate.Text));
mDev.SetRstCode(Misc.GetBytesFromHexString(Tb_RstCode.Text));
mDev.SetShortCircuitHalfDuplex((bool)Cb_Shd.IsChecked);
mDev.SetPort(PortList.Text);
if (File.Exists(mFile))
{ // 升级文件存在
byte[] filedata = File.ReadAllBytes(mFile);
if (filedata[0] == 0XAA && filedata[1] == 0X55)
{ // 文件类型正确
if (mDev.StartUpDate(filedata))
{ // 启动升级成功
Status.Content = "正在升级(Updating)....";
mWorking = true;
}
else
{
Status.Content = "串口打开失败或者文件错误。(Can't open port, or file error.)";
Cancel.IsEnabled = true; // 使能退出按钮
Bt_Update.IsEnabled = true;
}
}
else
{
Status.Content = "文件错误。(File error.)";
Cancel.IsEnabled = true; // 使能退出按钮
Bt_Update.IsEnabled = true;
}
}
else
{
Status.Content = "找不到升级文件。(No file.)";
Cancel.IsEnabled = true; // 使能退出按钮
Bt_Update.IsEnabled = true;
}
}
private void Bt_OpenFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "oup file(*.oup)|*.oup";
if (openFile.ShowDialog() == true)
{
Tb_OupFile.Text = openFile.FileName; //获取选择的文件名
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(mWorking)
{
MessageBoxResult result = MessageBox.Show("正在升级,确定是退出吗?", "询问", MessageBoxButton.YesNo, MessageBoxImage.Question);
//关闭窗口
if (result == MessageBoxResult.Yes)
e.Cancel = false;
//不关闭窗口
if (result == MessageBoxResult.No)
e.Cancel = true;
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。