#!/usr/bin/env python3 import os import sys import struct import serial from subprocess import Popen, PIPE, run ser = serial.Serial('/dev/ttyUSB0', 115200) def flash(): run("JLinkExe flash.jlink", shell=True, check=True) def read(channel, l): if channel == 'ser': data = ser.read(l) elif channel == 'std': data = sys.stdin.read(l) else: raise Exception("read() complains: no sai channelino") if len(data) != l: raise Exception("could not read %d bytes of data (got %d)" % (l, len(data))) return data def write(channel, data): if channel == 'ser': l = ser.write(data) elif channel == 'std': l = sys.stdout.write(data) else: raise Exception("write() complains: no sai channelino") if len(data) != l: raise Exception("could not write %d bytes of data (put %d)" % (len(data), l)) def obtain(channel): l = read(channel, 4) l = str.encode(l) (l, ) = struct.unpack("