Skip to content

Instantly share code, notes, and snippets.

@Sucareto
Last active February 7, 2021 10:11
Show Gist options
  • Save Sucareto/f8b8b04ed94f250fd1497b8cd96767b8 to your computer and use it in GitHub Desktop.
Save Sucareto/f8b8b04ed94f250fd1497b8cd96767b8 to your computer and use it in GitHub Desktop.
一种Arduino上传HEX的方式。通过打包刷写程序和HEX,可以在没有IDE和源码的环境更新固件。
@echo off
echo "确保文件夹内包含 avrdude.exe,avrdude.conf,libusb0.dll,32u4Serial.ino.promicro.hex,Upload.py,Upload.spec,icon.ico 这些文件"
pause
pyinstaller -F -i icon.ico Upload.spec
pause
import serial.tools.list_ports, time, os
#生成资源文件目录访问路径
def resource_path(relative_path):
if getattr(sys, 'frozen', False): #是否Bundle Resource
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
print("开始刷写前,请确保设备已接入,且无多余的串口设备!")
os.system("pause")
try:
com = serial.tools.list_ports.comports()[0][0]
except IndexError:
print("未发现串口!")
exit(1)
os.system("pause")
else:
print("发现串口:" + com + ",重启到 Bootloader 模式...")
serial.Serial(com, 1200)
time.sleep(1)
print("重启完成!")
try:
com = serial.tools.list_ports.comports()[0][0]
except IndexError:
print("重启后未检查到串口,请检查设备是否错误!")
exit(1)
os.system("pause")
else:
print("发现串口:" + com + ",开始刷入...")
#cmd = "avrdude -p m32u4 -c avr109 -P " + com + " -D -U flash:w:32u4Serial.hex"
cmd = resource_path(os.path.join('avrdude.exe')) + ' -C' + resource_path(os.path.join('avrdude.conf')) + ' -v -patmega32u4 -cavr109 -P' + com + ' -b57600 -D -Uflash:w:' + resource_path(os.path.join('32u4Serial.ino.promicro.hex')) + ':i'
#print("刷入指令:" + cmd)
if (os.system(cmd)):
print("刷入错误,请检查输出。")
exit(1)
os.system("pause")
else:
print("刷入成功!")
os.system("pause")
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['Flash.py'],
pathex=['X:\\Upload\\src'],
binaries=[],
datas=[
("avrdude.exe","."),
("avrdude.conf","."),
("libusb0.dll","."),
("32u4Serial.ino.promicro.hex",".")],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='Flash',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True , icon='icon.ico')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment