class uploader(object): '''Uploads a firmware file to the SiK bootloader''' NOP = chr(0x00) OK = chr(0x10) FAILED = chr(0x11) INSYNC = chr(0x12) EOC = chr(0x20) GET_SYNC = chr(0x21) GET_DEVICE = chr(0x22) CHIP_ERASE = chr(0x23) LOAD_ADDRESS= chr(0x24) PROG_FLASH = chr(0x25) READ_FLASH = chr(0x26) PROG_MULTI = chr(0x27) READ_MULTI = chr(0x28) PARAM_ERASE = chr(0x29) REBOOT = chr(0x30) PROG_MULTI_MAX = 32 # 64 causes serial hangs with some USB-serial adapters READ_MULTI_MAX = 255 BANK_PROGRAMING = -1 def __init__(self, portname, atbaudrate=57600, use_mavlink=False, mavport=0, debug=0): print("Connecting to %s" % portname) if use_mavlink: from pymavlink import mavutil self.port = mavutil.MavlinkSerialPort(portname, 115200, devnum=mavport, timeout=3, debug=debug) else: self.port = serial.Serial(portname, 115200, timeout=3) self.atbaudrate = atbaudrate
def main(): parser = ArgumentParser(description=__doc__) parser.add_argument("--mavport", required=True, help="Mavlink port name") parser.add_argument("--mavbaud", type=int, help="Mavlink port baud rate", default=115200) parser.add_argument("--devnum", default=2, type=int, help="PX4 UART device number (defaults to GPS port)") parser.add_argument( "--devbaud", default=38400, type=int, help="PX4 UART baud rate (defaults to u-Blox GPS baud)") parser.add_argument("--tcpport", default=1001, type=int, help="local TCP port (defaults to %(default)s)") parser.add_argument("--debug", default=0, type=int, help="debug level") parser.add_argument("--buffsize", default=128, type=int, help="buffer size") args = parser.parse_args() print("Connecting to MAVLINK...") mav_serialport = mavutil.MavlinkSerialPort(args.mavport, args.mavbaud, devnum=args.devnum, devbaud=args.devbaud, debug=args.debug) listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_sock.bind(('127.0.0.1', args.tcpport)) listen_sock.listen(1) print("Waiting for a TCP connection.") print("Use tcp://localhost:%d in u-Center." % args.tcpport) conn_sock, addr = listen_sock.accept() conn_sock.setblocking(0) # non-blocking mode print("TCP connection accepted. Use Ctrl+C to exit.") while True: try: data = conn_sock.recv(args.buffsize) if data: if args.debug >= 1: print '>', len(data) mav_serialport.write(data) except socket.error: pass data = mav_serialport.read(args.buffsize) if data: if args.debug >= 1: print '<', len(data) conn_sock.send(data)
def init_port(portname, baudrate=57600, use_mavlink=False, mavport=0): print("Connecting to %s" % portname) port = None if use_mavlink: from pymavlink import mavutil port = mavutil.MavlinkSerialPort(portname, baudrate, devnum=mavport) else: port = serial.Serial(portname, baudrate, timeout=3) return port
def __init__(self, portname, atbaudrate=57600, use_mavlink=False, mavport=0, debug=0): print("Connecting to %s" % portname) if use_mavlink: from pymavlink import mavutil self.port = mavutil.MavlinkSerialPort(portname, 115200, devnum=mavport, timeout=3, debug=debug) else: self.port = serial.Serial(portname, 115200, timeout=3) self.atbaudrate = atbaudrate self._debug = debug self.percent = -1 if use_mavlink: # we can send a bit more on mavlink uploader.PROG_MULTI_MAX = 64