日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

VS2019 WPF制作OTA上位机(二)获取bin文件路径

發布時間:2023/12/1 asp.net 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VS2019 WPF制作OTA上位机(二)获取bin文件路径 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

OTA升級是通過無線通信遠程把bin文件內容傳輸到單片機,完成升級。
因此上位機需要獲取bin文件的路徑,讀取bin文件內容,將內容分割依次發送(因為單片機的接收緩存不會開得和bin文件一樣大(十幾K甚至幾十K))。

首先在UI上添加控件,一個按鈕控件Button,一個文字顯示控件Lable,一個文字輸入輸出顯示控件Textbox

<Window x:Class="OTA.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:OTA"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Label Height="40" Width="85" Content="bin文件路徑:" VerticalContentAlignment="Center" VerticalAlignment="Top" HorizontalAlignment="Left"></Label><TextBox Height="40" Width="600" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="90,0,0,0" Name="TextBox_BinFilePath"></TextBox><Button Height="40" Width="60" Content="瀏覽Bin" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,30,0" Name="Button_GetBinFile" Click="Button_GetBinFile_Click"></Button></Grid> </Window>

Height是指定控件高度,Width是指定控件寬度
Content是控件的顯示文字
VerticalContentAlignment=“Center” 是控件顯示文字豎直方向居中顯示
HorizontalContentAlignment="Center"是控件顯示文字水平方向居中顯示

VerticalAlignment=“Top” 是控件豎直方向靠頂部顯示
HorizontalAlignment=“Left” 是控件水平方向靠左顯示
HorizontalAlignment=“Right” 是控件水平方向靠右顯示

Name是控件的代號,在程序里就是通過Name來控制各個控件的顯示和功能

Click=“Button_GetBinFile_Click”,Button_GetBinFile_Click是點擊按鈕發生的函數功能名,輸入click后會有 新建事件處理程序,可以直接回車來選擇它,這時候會自動在cs文件里創建名為Button_GetBinFile_Click的函數

Margin是調節位置,四個參數分別是 左,上,右,下的調節
比如Button是頂部右邊顯示,Margin=“0,0,30,0”,將Button和最右邊的距離拉開30

點擊運行看看效果

UI做好后,實現瀏覽按鍵的功能
直接上代碼

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 Microsoft.Win32;namespace OTA {/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{//保存讀取bin文件的數據private byte[] g_read_data;public MainWindow(){InitializeComponent();}private void Button_GetBinFile_Click(object sender, RoutedEventArgs e){//需要using Microsoft.Win32;//文件瀏覽窗口OpenFileDialog openFileDialog = new OpenFileDialog();//打開文件瀏覽窗口失敗if (!(bool)openFileDialog.ShowDialog()){return;}//把選擇的文件絕對路徑顯示到控件TextBox_BinFilePath.Text = openFileDialog.FileName;//需要using System.IO;//文件流打開bin文件FileStream fileStream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.ReadWrite);//二進制方式讀取bin文件數據BinaryReader binaryReader = new BinaryReader(fileStream);//打印bin文件的大小Console.WriteLine("bin file length:{0}", binaryReader.BaseStream.Length);//bin文件的大小int length = Convert.ToInt32(binaryReader.BaseStream.Length);//獲取bin文件數據g_read_data = binaryReader.ReadBytes(length);//關閉數據流和文件流binaryReader.Close();fileStream.Close();}} }

運行程序,點擊瀏覽Bin,出現文件夾彈框

選擇要升級的bin文件,點擊打開

在binaryReader.close();處打個斷點,調試可以看到g_read_data的內容

用VS code看打開的bin文件,內容和讀到的是一致的,讀取成功了

關掉斷點,繼續程序運行,可以看到bin文件的絕對路徑已經顯示在控件上了

接下來就是串口的功能實現了

總結

以上是生活随笔為你收集整理的VS2019 WPF制作OTA上位机(二)获取bin文件路径的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。