bwtest.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python
  2. '''
  3. check bandwidth of link
  4. '''
  5. from __future__ import print_function
  6. import time
  7. from pymavlink import mavutil
  8. #using argparse to receive options from the command line
  9. from argparse import ArgumentParser
  10. parser = ArgumentParser(description=__doc__)
  11. parser.add_argument("--baudrate", type=int,
  12. help="master port baud rate", default=115200)
  13. parser.add_argument("--device", required=True, help="serial device")
  14. args = parser.parse_args()
  15. ### MAV related code starts here ###
  16. # create a mavlink serial instance
  17. master = mavutil.mavlink_connection(args.device, baud=args.baudrate)
  18. t1 = time.time()
  19. counts = {}
  20. bytes_sent = 0
  21. bytes_recv = 0
  22. while True:
  23. #send some messages to the target system with dummy data
  24. master.mav.heartbeat_send(1, 1)
  25. master.mav.sys_status_send(1, 2, 3, 4, 5, 6, 7)
  26. master.mav.gps_raw_send(1, 2, 3, 4, 5, 6, 7, 8, 9)
  27. master.mav.attitude_send(1, 2, 3, 4, 5, 6, 7)
  28. master.mav.vfr_hud_send(1, 2, 3, 4, 5, 6)
  29. #Check for incoming data on the serial port and count
  30. #how many mesages of each type have been received
  31. while master.port.inWaiting() > 0:
  32. #recv_msg will try parsing the serial port buffer
  33. #and return a new message if available
  34. m = master.recv_msg()
  35. if m is None: break #No new message
  36. if m.get_type() not in counts:
  37. #if no messages of this type received, add this type to the counts dict
  38. counts[m.get_type()] = 0
  39. counts[m.get_type()] += 1
  40. #Print statistics every second
  41. t2 = time.time()
  42. if t2 - t1 > 1.0:
  43. print("%u sent, %u received, %u errors bwin=%.1f kB/s bwout=%.1f kB/s" % (
  44. master.mav.total_packets_sent,
  45. master.mav.total_packets_received,
  46. master.mav.total_receive_errors,
  47. 0.001*(master.mav.total_bytes_received-bytes_recv)/(t2-t1),
  48. 0.001*(master.mav.total_bytes_sent-bytes_sent)/(t2-t1)))
  49. bytes_sent = master.mav.total_bytes_sent
  50. bytes_recv = master.mav.total_bytes_received
  51. t1 = t2