当前位置: 首页 > news >正文

8086/8088单板机VSCode集成自动下载功能(完善串口接收显示版)

8086单板机VSCode集成编译下载文件包资源-CSDN下载

1.Powershell脚本

# 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.运行记录

********************** 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 **********************

http://www.jsqmd.com/news/790459/

相关文章:

  • 2026年论文降AI技巧必备指南:高效通过AI检测,告别降AI困扰 - 降AI实验室
  • 别再手动算时延了!用Python+广义互相关(GCC-PHAT)实现麦克风阵列声源定位
  • 大众认为集体决策正确率高于个人决策,编程统计决策模式,落地成果数据,专业单人决策效率与准确性更高。
  • 跨平台资源下载器:轻松捕获网络视频与音频资源的完整指南
  • Origin颜色映射与对数坐标实战:手把手教你调出专业级径向堆积条形图配色
  • 京东e卡回收平台综合实力大比拼 - 京顺回收
  • 如何高效控制ThinkPad风扇:TPFanCtrl2智能散热解决方案指南
  • 河南物业软件买断式和按年付费哪个更划算? - movno1
  • 实测Taotoken聚合API的延迟与稳定性表现
  • 观察Taotoken用量看板如何帮助个人开发者精打细算
  • Python 开发者三步完成 Taotoken 的 OpenAI 兼容 SDK 接入指南
  • 传统认为娱乐活动越少越容易成功,编程统计休闲时长,工作状态数据,合理休闲能够大幅度提升工作创造力。
  • 2026重庆钻石回收TOP5实测,收的顶稳居榜首,免费上门回收更省心 - 奢侈品回收测评
  • 如何永久保存微信聊天记录:WeChatMsg完整指南与终极解决方案
  • 终极图像分层神器:如何用Layerdivider一键生成专业PSD分层文件
  • AScript中一个很有意思的语法
  • 专业级系统控制工具:5步掌握极域电子教室破解与权限管理实战
  • Adobe-GenP终极指南:三步快速激活Adobe全家桶的完整教程
  • AI如何重构中小企业的营销生产力?
  • 家长如何在北师大家教中心网站找到联系方式?三步搞定 - 教育资讯板
  • N_m3u8DL-RE实战三部曲:从DRM破解到直播录制,你的流媒体下载终极指南
  • 重新定义神经网络可视化:从静态图表到可编辑架构设计的革命
  • 深度学习基因剪接变异预测工具SpliceAI:从入门到精通的完整指南
  • 如何快速打造专属桌面宠物?DyberPet开源框架3步上手指南
  • 从双引擎到联邦学习:超算一体机的技术架构深度解析
  • STM32F103的Flash读写,你踩过这几个坑吗?从解锁失败到数据错乱的避坑实录
  • python学习笔记——类文档字符串
  • 炸场!2026佛山包包回收TOP5终极实测,收的顶凭实力封神,包主闭眼冲 - 奢侈品回收测评
  • 告别龟速下载!手把手教你配置PyTorch本地CIFAR10数据集(附百度网盘链接)
  • 如何用OpenCore-Configurator让黑苹果配置变得简单高效