mavgps.py 3.0 KB

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