Last active
July 9, 2018 15:35
-
-
Save DerEnderKeks/855ce104eed2fb2a91d8b28b1671dbe0 to your computer and use it in GitHub Desktop.
Script to backup the configuration of betaflight/INAV flight controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import io | |
import time | |
from time import gmtime, strftime | |
import serial | |
import serial.tools.list_ports | |
if __name__ == '__main__': | |
comports = serial.tools.list_ports.comports() | |
if len(comports) == 1: | |
port = 0 | |
print('Auto-selected port ' + comports[port].device) | |
else: | |
print('Available COM ports:\n') | |
for i, val in enumerate(comports): | |
print(str(i) + ': ' + str(val)) | |
print() | |
port = int(input('COM Port: ')) | |
ser = serial.Serial() | |
ser.baudrate = 115200 | |
ser.port = comports[port].device | |
ser.timeout = 1 | |
ser.open() | |
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser)) | |
sio.write(str('#')) | |
sio.flush() | |
sio.readlines() | |
sio.write(str('diff all\n')) | |
sio.flush() | |
data = sio.readlines() | |
#print(data) | |
if not '# version\n' in data: | |
print('Failed to read backup.') | |
exit(1) | |
data[0] = '# ' + data[0] | |
firmware = "" | |
name = "" | |
for i, line in enumerate(data): | |
if str(line).startswith('# version'): | |
firmware = str(data[i + 1]).split('/')[0][2:].strip() | |
print('Firmware: ' + firmware) | |
if str(line).startswith('name'): | |
name = str(data[i])[5:].replace('\n', '') | |
print('Name: ' + name) | |
f = open(firmware + '_backup_' + name + '_' + strftime("%Y%m%d_%H%M%S", gmtime()) + '.txt', 'w+') | |
for line in data: | |
f.write(line) | |
f.close() | |
sio.write(str('exit\n')) | |
sio.flush() | |
ser.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment