listenUDP.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import socket
  4. import time
  5. import pprint
  6. import sys
  7. import datetime
  8. from PyQt5.QtWidgets import *
  9. from PyQt5.QtCore import (QThread, pyqtSignal, pyqtSlot)
  10. from functions import *
  11. pp = pprint.PrettyPrinter(indent=4)
  12. dataset = """
  13. /*-------RAW-----------*/
  14. uint32_t pressure_cis_raw; // pack 1 x 4
  15. uint32_t temperature_cis_raw; // + 1 x 4
  16. uint32_t pressure_spi_raw;
  17. uint32_t temperature_i2c_raw;
  18. uint32_t temperature_spi_raw;
  19. uint32_t adc_temperature_raw;
  20. int32_t temperature_mpu_raw;
  21. uint32_t adc_temperature_internal_raw;
  22. uint32_t adc_voltage_raw;
  23. uint32_t adc_current_raw;
  24. struct vector_Uint accel_raw; // pack 3 x 4
  25. struct vector_Uint gyro_raw; // + 3 x 4
  26. struct vector_Uint mag_raw; // pack 3 x 4
  27. // + 1 x 4
  28. int32_t temperature_mag_raw;
  29. /*-----Computed--------*/
  30. double pressure_cis; // no packing needed 1 x 8
  31. double temperature_cis;
  32. double pressure_spi;
  33. double temperature_i2c;
  34. double temperature_spi;
  35. double adc_temperature;
  36. double adc_temperature_internal;
  37. double adc_voltage;
  38. double adc_current;
  39. double temperature_mpu;
  40. struct vector accel; // no packing needed 3 x 8
  41. struct vector gyro;
  42. struct vector rot;
  43. struct vector mag;
  44. double temperature_mag;
  45. struct vector global_accel;
  46. struct vector pos;
  47. /*-----------gps------------*/
  48. float longitude;
  49. float latitude;
  50. int32_t altitude;
  51. uint32_t time;
  52. uint32_t hdop;
  53. int32_t mag_head; //heading
  54. uint32_t count;
  55. uint32_t crc;
  56. """
  57. class listenUDP(QThread):
  58. newMsg = pyqtSignal(dict)
  59. master = PktParser(dataset)
  60. slave = PktParser(dataset, master.end)
  61. fusion = PktParser(dataset, slave.end)
  62. dumpFile = open("logs/dump_{}.bin".format(datetime.datetime.now().strftime('%m%d-%H%M%S')), "bw")
  63. def __init__(self, port = 1234):
  64. QThread.__init__(self)
  65. self._udpServer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  66. self._udpServer.bind(("", port))
  67. self.port = port
  68. def run(self):
  69. while self._udpServer:
  70. try:
  71. data, addr = self._udpServer.recvfrom(2048)
  72. except WindowsError as e:
  73. print(e)
  74. time.sleep(1)
  75. continue
  76. if data == None:
  77. continue
  78. self.dumpFile.write(data + bytes("\r\n", 'ascii'))
  79. data_master = self.master.parse(data)
  80. data_slave = self.slave.parse(data)
  81. data_fusion = self.fusion.parse(data)
  82. data = data_master
  83. if not data_master:
  84. continue
  85. for prop in self.master.keys():
  86. if data_slave:
  87. data["slave_" + prop] = data_slave[prop]
  88. if data_fusion:
  89. data["fusion_" + prop] = data_fusion[prop]
  90. data["datetime"] = str(datetime.datetime.now())
  91. if __name__ == "__main__":
  92. pp.pprint(data)
  93. self.newMsg.emit(data)
  94. time.sleep(0.01)
  95. def stop(self):
  96. self._udpServer.close()
  97. self._udpServer = None
  98. self.quit()
  99. if __name__ == "__main__":
  100. pp.pprint(obj)
  101. return
  102. if __name__ == "__main__":
  103. udp = listenUDP(port = 1234)
  104. print("UDP Server is listening on port " + str(udp.port))
  105. udp.run()