8086单板机VSCode集成编译下载文件包资源-CSDN下载
1.Powershell脚本
bash
# combined_serial.ps1 - 串口监视 + 文件发送工具
# 功能:先监视串口数据,按任意键退出监视后自动发送文件到同一串口
Clear-Host
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 串口工具 - 先接收后发送" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
# ==================== 公共函数 ====================
function Get-AvailablePorts {
try {
$ports = [System.IO.Ports.SerialPort]::GetPortNames()
if ($ports.Count -eq 0) {
$ports = Get-WmiObject -Class Win32_SerialPort | Where-Object { $_.Name -like "*COM*" } | ForEach-Object { $_.DeviceID }
}
return $ports | Sort-Object
}
catch {
Write-Error "获取串口列表失败: $_"
return @()
}
}
function Show-AvailablePorts {
$ports = Get-AvailablePorts
if ($ports.Count -eq 0) {
Write-Host "`n[!] 未找到任何可用串口!" -ForegroundColor Red
return $null
}
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " 可用串口列表" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan
for ($i = 0; $i -lt $ports.Count; $i++) {
Write-Host " [$($i+1)] $($ports[$i])" -ForegroundColor Green
}
Write-Host "`n [0] 退出" -ForegroundColor Red
Write-Host "========================================" -ForegroundColor Cyan
return $ports
}
function Select-Port {
$ports = Show-AvailablePorts
if ($ports -eq $null -or $ports.Count -eq 0) {
return $null
}
do {
$selection = Read-Host "`n请选择串口号 [输入数字]"
if ($selection -eq "0") {
Write-Host "`n[!] 用户取消操作" -ForegroundColor Yellow
return $null
}
$index = [int]$selection - 1
if ($index -ge 0 -and $index -lt $ports.Count) {
$selectedPort = $ports[$index]
Write-Host "`n[✓] 已选择串口: $selectedPort" -ForegroundColor Green
return $selectedPort
}
else {
Write-Host "[!] 无效选择,请输入有效数字" -ForegroundColor Red
}
} while ($true)
}
# ==================== 功能1:串口监视接收 ====================
function Start-SerialMonitor {
param(
[Parameter(Mandatory=$true)]
[string]$PortName,
[int]$BaudRate = 9600
)
$serialPort = New-Object System.IO.Ports.SerialPort $PortName, $BaudRate
$serialPort.ReadTimeout = 2000
$serialPort.DtrEnable = $true
$serialPort.RtsEnable = $true
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " 串口监视模式" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan
Write-Host "端口: $PortName"
Write-Host "波特率: $BaudRate"
Write-Host "数据位: $($serialPort.DataBits)"
Write-Host "停止位: $($serialPort.StopBits)"
try {
$serialPort.Open()
Write-Host "`n[成功] 串口已打开" -ForegroundColor Green
Write-Host "[提示] 按任意键停止监视并进入发送模式`n" -ForegroundColor Yellow
Write-Host "等待接收数据...`n" -ForegroundColor Gray
$counter = 0
$lastDotTime = (Get-Date).Second
$stopMonitoring = $false
# 创建一个后台线程来检测按键
$powershell = [PowerShell]::Create()
$null = $powershell.AddScript({
param($syncHash)
$null = $syncHash.Console.KeyAvailable
$key = $syncHash.Console.ReadKey($true)
$syncHash.KeyPressed = $true
}).AddParameter('syncHash', $PSCmdlet.SessionState.PSVariable.GetValue('syncHash'))
# 使用非阻塞方式检测按键
while (-not $stopMonitoring) {
# 检查是否有按键按下
if ([Console]::KeyAvailable) {
$key = [Console]::ReadKey($true)
Write-Host "`n`n[!] 用户按任意键,停止监视" -ForegroundColor Yellow
$stopMonitoring = $true
break
}
try {
if ($serialPort.BytesToRead -gt 0) {
$bytesToRead = $serialPort.BytesToRead
$buffer = New-Object byte[] $bytesToRead
$bytesRead = $serialPort.Read($buffer, 0, $bytesToRead)
if ($bytesRead -gt 0) {
$counter++
$time = Get-Date -Format "HH:mm:ss.fff"
Write-Host "[$time] 收到 $bytesRead 字节:" -ForegroundColor Green
# 十六进制显示
$hex = ($buffer | ForEach-Object { $_.ToString('X2') }) -join ' '
Write-Host " HEX: $hex" -ForegroundColor Cyan
# ASCII显示
$ascii = ($buffer | ForEach-Object {
if ($_ -ge 32 -and $_ -le 126) { [char]$_ } else { '.' }
}) -join ''
Write-Host " ASCII: $ascii" -ForegroundColor Gray
Write-Host ""
}
}
else {
Start-Sleep -Milliseconds 100
$currentSecond = (Get-Date).Second
if ($currentSecond % 5 -eq 0 -and $currentSecond -ne $lastDotTime) {
Write-Host "." -NoNewline
$lastDotTime = $currentSecond
}
}
Start-Sleep -Milliseconds 10
}
catch {
if ($_.Exception.Message -notlike "*Timeout*") {
# 忽略超时错误
}
}
}
}
catch {
Write-Host "`n[错误] $_" -ForegroundColor Red
return $false
}
finally {
if ($serialPort.IsOpen) {
$serialPort.Close()
Write-Host "`n[信息] 串口已关闭" -ForegroundColor Gray
}
$serialPort.Dispose()
}
return $true
}
# ==================== 功能2:发送文件 ====================
function Send-FileToSerialPort {
param(
[Parameter(Mandatory=$true)]
[string]$PortName,
[Parameter(Mandatory=$true)]
[string]$FilePath,
[int]$BaudRate = 9600
)
if (-not (Test-Path $FilePath)) {
Write-Error "文件不存在: $FilePath"
return $false
}
$serialPort = New-Object System.IO.Ports.SerialPort($PortName, $BaudRate, [System.IO.Ports.Parity]::None, 8, [System.IO.Ports.StopBits]::One)
$serialPort.ReadTimeout = 5000
$serialPort.WriteTimeout = 5000
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " 串口文件发送模式" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan
Write-Host "串口号: $PortName"
Write-Host "波特率: $BaudRate"
Write-Host "文件: $FilePath"
try {
Write-Host "[→] 正在打开串口 $PortName ..." -ForegroundColor Yellow
$serialPort.Open()
Write-Host "[✓] 串口已打开" -ForegroundColor Green
Start-Sleep -Milliseconds 500
Write-Host "[→] 正在读取文件 ..." -ForegroundColor Yellow
$bytes = [System.IO.File]::ReadAllBytes($FilePath)
$fileSizeKB = [math]::Round($bytes.Length / 1KB, 2)
Write-Host "[✓] 文件大小: $($bytes.Length) 字节 ($fileSizeKB KB)" -ForegroundColor Green
$confirm = Read-Host "`n是否开始发送?[Y/N]"
if ($confirm -ne "Y" -and $confirm -ne "y") {
Write-Host "[!] 用户取消发送" -ForegroundColor Yellow
return $false
}
Write-Host "`n[→] 正在发送数据 ..." -ForegroundColor Yellow
$chunkSize = 4096
$totalBytes = $bytes.Length
$sentBytes = 0
for ($i = 0; $i -lt $totalBytes; $i += $chunkSize) {
$remaining = $totalBytes - $i
$currentChunkSize = if ($remaining -lt $chunkSize) { $remaining } else { $chunkSize }
$chunk = $bytes[$i..($i + $currentChunkSize - 1)]
$serialPort.Write($chunk, 0, $chunk.Length)
$sentBytes += $chunk.Length
$percent = [math]::Round(($sentBytes / $totalBytes) * 100, 1)
Write-Progress -Activity "发送数据" -Status "进度: $percent%" -PercentComplete $percent
}
Write-Progress -Activity "发送数据" -Completed
Write-Host "`n[✓] 数据发送完成!共发送 $sentBytes 字节" -ForegroundColor Green
Start-Sleep -Seconds 2
if ($serialPort.BytesToRead -gt 0) {
Write-Host "[→] 收到响应数据:" -ForegroundColor Cyan
$response = $serialPort.ReadExisting()
Write-Host $response -ForegroundColor White
}
return $true
}
catch [System.UnauthorizedAccessException] {
Write-Error "错误: 无法访问串口 $PortName(可能已被占用)"
return $false
}
catch {
Write-Error "错误: $_"
return $false
}
finally {
if ($serialPort.IsOpen) {
$serialPort.Close()
Write-Host "[信息] 串口已关闭" -ForegroundColor Gray
}
$serialPort.Dispose()
}
}
# ==================== 主程序 ====================
# 选择串口
$selectedPort = Select-Port
if ($selectedPort -eq $null) {
Write-Host "`n[!] 程序退出" -ForegroundColor Yellow
exit 0
}
# 获取波特率
$baudRate = Read-Host "`n请输入波特率 [默认: 9600]"
if ($baudRate -eq "") { $baudRate = 9600 } else { $baudRate = [int]$baudRate }
# 第一阶段:串口监视
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " 第一阶段:串口接收监视" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Start-SerialMonitor -PortName $selectedPort -BaudRate $baudRate
# 第二阶段:发送文件
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host " 第二阶段:发送文件" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
$filePath = Read-Host "`n请输入要发送的文件路径 [默认: .\8088.bin]"
if ($filePath -eq "") { $filePath = ".\8088.bin" }
$result = Send-FileToSerialPort -PortName $selectedPort -FilePath $filePath -BaudRate $baudRate
# 完成
Write-Host "`n========================================" -ForegroundColor $(if ($result) { "Green" } else { "Red" })
if ($result) {
Write-Host " 全部操作完成!" -ForegroundColor Green
} else {
Write-Host " 文件发送失败!" -ForegroundColor Red
}
Write-Host "========================================" -ForegroundColor $(if ($result) { "Green" } else { "Red" })
Write-Host "`n按任意键退出..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
2.运行记录
bash
**********************
Windows PowerShell 脚本开始
开始时间: 20260510152236
用户名: YY\cxhus
RunAs 用户: YY\cxhus
配置名称:
计算机: YY (Microsoft Windows NT 10.0.26200.0)
主机应用程序: C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command try { . "c:\Users\cxhus\AppData\Local\Programs\Microsoft VS Code\8b640eef5a\resources\app\out\vs\workbench\contrib\terminal\common\scripts\shellIntegration.ps1" } catch {}
进程 ID: 41368
PSVersion: 5.1.26100.8115
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.26100.8115
BuildVersion: 10.0.26100.8115
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
已启动脚本,输出文件为 output1000.txt
========================================
串口工具 - 先接收后发送
========================================
========================================
可用串口列表
========================================
[1] COM1
[2] COM2
[3] COM3
[4] COM4
[5] COM9
[0] 退出
========================================
[✓] 已选择串口: COM9
========================================
第一阶段:串口接收监视
========================================
========================================
串口监视模式
========================================
端口: COM9
波特率: 9600
数据位: 8
停止位: One
[成功] 串口已打开
[提示] 按任意键停止监视并进入发送模式
等待接收数据...
.
[15:22:47.396] 收到 21 字节:
HEX: 00 20 4F 4B 2E 2E 2E 77 77 77 2E 69 38 30 38 38 2E 63 6E 0D 0A
ASCII: . OK...www.i8088.cn..
[15:22:48.537] 收到 5 字节:
HEX: 4F 4B 2E 2E 2E
ASCII: OK...
[15:22:49.560] 收到 5 字节:
HEX: 4F 4B 2E 2E 2E
ASCII: OK...
.
[15:22:50.693] 收到 19 字节:
HEX: 4F 4B 2E 2E 2E 77 77 77 2E 69 38 30 38 38 2E 63 6E 0D 0A
ASCII: OK...www.i8088.cn..
[!] 用户按任意键,停止监视
[信息] 串口已关闭
True
========================================
第二阶段:发送文件
========================================
========================================
串口文件发送模式
========================================
串口号: COM9
波特率: 9600
文件: .\8088.bin
[→] 正在打开串口 COM9 ...
[✓] 串口已打开
[→] 正在读取文件 ...
[✓] 文件大小: 59 字节 (0.06 KB)
[→] 正在发送数据 ...
[✓] 数据发送完成!共发送 59 字节
[→] 收到响应数据:
? ???????? ???? ? ? ?? ??????QS???
????Ku?[Y?UUUU
[信息] 串口已关闭
========================================
全部操作完成!
========================================
按任意键退出...
**********************
Windows PowerShell 脚本结束
结束时间: 20260510152300
**********************

'