listenUDP.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. dumpFile = open("logs/dump_{}.bin".format(datetime.datetime.now().strftime('%m%d-%H%M%S')), "bw")
  62. def __init__(self, port = 1234):
  63. QThread.__init__(self)
  64. self._udpServer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  65. self._udpServer.bind(("", port))
  66. self.port = port
  67. def run(self):
  68. while self._udpServer:
  69. try:
  70. data, addr = self._udpServer.recvfrom(2048)
  71. except WindowsError as e:
  72. print(e)
  73. time.sleep(1)
  74. continue
  75. if data == None:
  76. continue
  77. self.dumpFile.write(data + bytes("\r\n", 'ascii'))
  78. data = self.master.parse(data)
  79. if not data:
  80. continue
  81. if __name__ == "__main__":
  82. pp.pprint(data)
  83. self.newMsg.emit(data)
  84. time.sleep(0.01)
  85. def stop(self):
  86. self._udpServer.close()
  87. self._udpServer = None
  88. self.quit()
  89. if __name__ == "__main__":
  90. pp.pprint(obj)
  91. return obj
  92. if __name__ == "__main__":
  93. udp = listenUDP(port = 1234)
  94. print("UDP Server is listening on port " + str(udp.port))
  95. udp.run()