listenUDP.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import socket
  4. import time
  5. import pprint
  6. import sys
  7. sys.path.insert(0,'..')
  8. from functions import *
  9. pp = pprint.PrettyPrinter(indent=4)
  10. port = 1234
  11. f = open('new struct.log','bw')
  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_i2c_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. /*-----Computed--------*/
  27. double pressure_cis; // no packing needed 1 x 8
  28. double temperature_cis;
  29. double pressure_i2c;
  30. double temperature_i2c;
  31. double temperature_spi;
  32. double adc_temperature;
  33. double adc_temperature_internal;
  34. double adc_voltage;
  35. double adc_current;
  36. double temperature_mpu;
  37. struct vector accel; // no packing needed 3 x 8
  38. struct vector gyro;
  39. struct vector rot;
  40. /*-----------gps------------*/
  41. int32_t longitude;
  42. int32_t longitude_mod2;
  43. int32_t latitude;
  44. int32_t latitude_mod2;
  45. int32_t altitude;
  46. int32_t altitude_mod2;
  47. uint32_t time;
  48. uint32_t time_mod2;
  49. uint32_t hdop;
  50. uint32_t hdop_mod2;
  51. /*--------------------------*/
  52. /*---------magnet-----------*/
  53. struct vector_Uint mag_raw; // pack 3 x 4
  54. int32_t temperature_mag_raw; // + 1 x 4
  55. struct vector mag;
  56. double temperature_mag;
  57. /*--------------------------*/
  58. uint32_t count; // pack 1 x 4
  59. uint32_t crc; // + 1 x 4
  60. """
  61. print("master:")
  62. master = PktParser(dataset)
  63. print("slave:")
  64. slave = PktParser(dataset, master.end)
  65. #x = master.parse(bytes("\00\00\00\01"*slave.end, 'ascii'))
  66. #pp.pprint(x)
  67. udpServer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  68. udpServer.bind(("", port))
  69. print("UDP Server is listening on port " + str(port))
  70. while True:
  71. try:
  72. data, addr = udpServer.recvfrom(2048)
  73. except WindowsError as e:
  74. print(e)
  75. time.sleep(1)
  76. continue
  77. if data == None:
  78. continue
  79. f.write(data + bytes("\r\n", 'ascii'))
  80. pp.pprint(master.parse(data))
  81. pp.pprint(slave.parse(data))