mavgps.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 """
3 Allows connection of the uBlox u-Center software to
4 a uBlox GPS device connected to a PX4 or Pixhawk device,
5 using Mavlink's SERIAL_CONTROL support to route serial
6 traffic to/from the GPS, and exposing the data to u-Center
7 via a local TCP connection.
8 
9 @author: Matthew Lloyd (github@matthewlloyd.net)
10 """
11 from __future__ import print_function
12 
13 from pymavlink import mavutil
14 from argparse import ArgumentParser
15 import socket
16 
17 
18 def main():
19  parser = ArgumentParser(description=__doc__)
20  parser.add_argument("--mavport", required=True,
21  help="Mavlink port name")
22  parser.add_argument("--mavbaud", type=int,
23  help="Mavlink port baud rate", default=115200)
24  parser.add_argument("--devnum", default=2, type=int,
25  help="PX4 UART device number (defaults to GPS port)")
26  parser.add_argument("--devbaud", default=115200, type=int,
27  help="PX4 UART baud rate (defaults to u-Blox GPS baud)")
28  parser.add_argument("--tcpport", default=2001, type=int,
29  help="local TCP port (defaults to %(default)s)")
30  parser.add_argument("--tcpaddr", default='127.0.0.1', type=str,
31  help="local TCP address (defaults to %(default)s)")
32  parser.add_argument("--debug", default=0, type=int,
33  help="debug level")
34  parser.add_argument("--buffsize", default=128, type=int,
35  help="buffer size")
36  parser.add_argument("--login", default=None, help="input log file")
37  parser.add_argument("--logout", default=None, help="output log file")
38  args = parser.parse_args()
39 
40  print("Connecting to MAVLINK...")
41  mav_serialport = mavutil.MavlinkSerialPort(
42  args.mavport, args.mavbaud,
43  devnum=args.devnum, devbaud=args.devbaud, debug=args.debug)
44 
45  listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46  listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
47  listen_sock.bind((args.tcpaddr, args.tcpport))
48  listen_sock.listen(1)
49 
50  print("Waiting for a TCP connection.")
51  print("Use tcp://%s:%d in u-Center." % (args.tcpaddr, args.tcpport))
52  conn_sock, addr = listen_sock.accept()
53  conn_sock.setblocking(0) # non-blocking mode
54  print("TCP connection accepted. Use Ctrl+C to exit.")
55 
56  login = None
57  logout = None
58  if args.login:
59  login = open(args.login, "w")
60  if args.logout:
61  logout = open(args.logout, "w")
62 
63  while True:
64  try:
65  data = conn_sock.recv(args.buffsize)
66  if data:
67  if login:
68  login.write(data)
69  if args.debug >= 1:
70  print('>', len(data))
71  mav_serialport.write(data)
72  except socket.error:
73  pass
74 
75  data = mav_serialport.read(args.buffsize)
76  if data:
77  if logout:
78  logout.write(data)
79  if args.debug >= 1:
80  print('<', len(data))
81  conn_sock.send(data)
82 
83 
84 if __name__ == '__main__':
85  main()


mavlink
Author(s): Lorenz Meier
autogenerated on Sun Jul 7 2019 03:22:07