Go to the documentation of this file.00001
00002 """
00003 Allows connection of the uBlox u-Center software to
00004 a uBlox GPS device connected to a PX4 or Pixhawk device,
00005 using Mavlink's SERIAL_CONTROL support to route serial
00006 traffic to/from the GPS, and exposing the data to u-Center
00007 via a local TCP connection.
00008
00009 @author: Matthew Lloyd (github@matthewlloyd.net)
00010 """
00011
00012 from pymavlink import mavutil
00013 from argparse import ArgumentParser
00014 import socket
00015
00016
00017 def main():
00018 parser = ArgumentParser(description=__doc__)
00019 parser.add_argument("--mavport", required=True,
00020 help="Mavlink port name")
00021 parser.add_argument("--mavbaud", type=int,
00022 help="Mavlink port baud rate", default=115200)
00023 parser.add_argument("--devnum", default=2, type=int,
00024 help="PX4 UART device number (defaults to GPS port)")
00025 parser.add_argument("--devbaud", default=38400, type=int,
00026 help="PX4 UART baud rate (defaults to u-Blox GPS baud)")
00027 parser.add_argument("--tcpport", default=1001, type=int,
00028 help="local TCP port (defaults to %(default)s)")
00029 parser.add_argument("--tcpaddr", default='127.0.0.1', type=str,
00030 help="local TCP address (defaults to %(default)s)")
00031 parser.add_argument("--debug", default=0, type=int,
00032 help="debug level")
00033 parser.add_argument("--buffsize", default=128, type=int,
00034 help="buffer size")
00035 args = parser.parse_args()
00036
00037 print("Connecting to MAVLINK...")
00038 mav_serialport = mavutil.MavlinkSerialPort(
00039 args.mavport, args.mavbaud,
00040 devnum=args.devnum, devbaud=args.devbaud, debug=args.debug)
00041
00042 listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00043 listen_sock.bind((args.tcpaddr, args.tcpport))
00044 listen_sock.listen(1)
00045
00046 print("Waiting for a TCP connection.")
00047 print("Use tcp://%s:%d in u-Center." % (args.tcpaddr, args.tcpport))
00048 conn_sock, addr = listen_sock.accept()
00049 conn_sock.setblocking(0)
00050 print("TCP connection accepted. Use Ctrl+C to exit.")
00051
00052 while True:
00053 try:
00054 data = conn_sock.recv(args.buffsize)
00055 if data:
00056 if args.debug >= 1:
00057 print '>', len(data)
00058 mav_serialport.write(data)
00059 except socket.error:
00060 pass
00061
00062 data = mav_serialport.read(args.buffsize)
00063 if data:
00064 if args.debug >= 1:
00065 print '<', len(data)
00066 conn_sock.send(data)
00067
00068
00069 if __name__ == '__main__':
00070 main()