daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pprint import pprint
  2. from threading import Thread
  3. import queue
  4. import struct
  5. import crc8
  6. import json
  7. import math
  8. from matplotlib import pyplot
  9.  
  10. START = 0x01
  11.  
  12. vbats = []
  13. x = range(1000)
  14.  
  15.  
  16. class ChecksumException(Exception):
  17.     def __init__(self, message):
  18.         super().__init__(message)
  19.         self.message = message
  20.  
  21.  
  22. class Parser():
  23.     def __init__(self, in_file, out_file):
  24.         self.in_file = in_file
  25.         self.out_file = out_file
  26.         self.data_queue = queue.Queue()
  27.  
  28.     def run(self):
  29.         temp_size = 0
  30.         with open(self.in_file, "rb") as f:
  31.             data = f.read(1)
  32.             self.data_queue.put(data[0])
  33.             while len(vbats) < 1000:
  34.                 data = f.read(1)
  35.                 self.data_queue.put(data[0])
  36.                 if self.data_queue.qsize() >= 2:
  37.                     if self.data_queue.queue[0] == START:
  38.                         if self.data_queue.queue[1] in type_dict:
  39.                             constructor, length = type_dict[self.data_queue.queue[1]]
  40.                             while self.data_queue.qsize() < length:
  41.                                 data = f.read(1)
  42.                                 self.data_queue.put(data[0])
  43.                             try:
  44.                                 packet = constructor(
  45.                                     list(self.data_queue.queue)[:length])
  46.                                 for i in range(length-1):
  47.                                     self.data_queue.get()
  48.                             except ChecksumException:
  49.                                 pass
  50.                                 # print("Checksum failed")
  51.                             except ZeroDivisionError:
  52.                                 pass
  53.                                 # print("Division by zero")
  54.                             except ValueError:
  55.                                 pass
  56.                                 # print("Value error")
  57.                     self.data_queue.get()
  58.  
  59.         pyplot.plot(x, vbats)
  60.         pyplot.show()
  61.  
  62.  
  63. class Pack:
  64.  
  65.     def __init__(self):
  66.         self.type = None
  67.         self.timestamp = None
  68.         self.checksum = None
  69.  
  70.     def show(self):
  71.         pprint(vars(self))
  72.  
  73.     def validate(self, data):
  74.         if crc8.crc8((bytearray(data)[:len(bytearray(data))-2])) != self.checksum:
  75.             raise ChecksumException
  76.  
  77.  
  78. class TemperaturePack(Pack):
  79.     def __init__(self, data):
  80.         Pack.__init__(self)
  81.         self.type = "TEMP"
  82.         self.temp1 = None
  83.         self.temp2 = None
  84.         self.temp3 = None
  85.         self.temp4 = None
  86.         self.pressure = None
  87.         self.vbat = None
  88.         self.humid = None
  89.         self.checksum = None
  90.  
  91.         self.calibrationNTC = {'R_0': 33000, 'T_0': 298.15,
  92.                                'R_1': 33000, 'B': 5000, 'resolution': 12}
  93.         self.calibrationVBAT = {'multiplier': 0.5, 'reference': 2.50}
  94.  
  95.         self.parse(data)
  96.  
  97.     def parse(self, data):
  98.         raw_temp1, self.timestamp, raw_temp2,  self.temp3, self.temp4, raw_pressure,  raw_vbat, self.humid, self.checksum = struct.unpack(
  99.             'xxHIHhhhHBB', bytearray(data))
  100.         self.pressure = self.convert_pressure(raw_pressure)
  101.         self.temp1 = self.convertNTC(raw_temp1, **self.calibrationNTC)
  102.         self.temp2 = self.convertNTC(raw_temp2, **self.calibrationNTC)
  103.         self.vbat = self.convert_vbat(raw_vbat, **self.calibrationVBAT)
  104.         vbats.append(self.vbat)
  105.  
  106.     def convert_pressure(self, pressureRaw):
  107.         return (pressureRaw / 100.0) + 750.0
  108.  
  109.     def convertNTC(self, temperature_raw, R_1, resolution, T_0, B, R_0):
  110.         R = R_1 * ((2**resolution) / temperature_raw - 1)
  111.         T = 1/(1/T_0 + 1/B * math.log(R/R_0))
  112.         return T - 273.18
  113.  
  114.     def convert_vbat(self, vbat_raw, multiplier, reference):
  115.         return vbat_raw / 4096 * 2.5 * 2
  116.  
  117.  
  118. class GPSPack(Pack):
  119.     def __init__(self, data):
  120.         Pack.__init__(self)
  121.         self.type = "GPS"
  122.         self.hdop = None
  123.         self.hour = None
  124.         self.minute = None
  125.         self.second = None
  126.         self.lat = None
  127.         self.lon = None
  128.         self.height = None
  129.         self.parse(data)
  130.  
  131.     def parse(self, data):
  132.         rawHDOP, self.timestamp, rawGPStime, self.lat, self.lon, self.height, self.checksum = struct.unpack(
  133.             "xxBxIIfffB", bytearray(data))
  134.         self.hdop = self.convertHDOP(rawHDOP)
  135.         self.hour, self.minute, self.second = self.convertGPStime(rawGPStime)
  136.  
  137.     def convertHDOP(self, rawHDOP):
  138.         return rawHDOP / 10.0
  139.  
  140.     def convertGPStime(self, rawGPStime):
  141.         try:
  142.             text = str(rawGPStime)
  143.             hour = int(text[:2])
  144.             minute = int(text[2:4])
  145.             second = int(text[4:6])
  146.             return [hour, minute, second]
  147.         except ValueError:
  148.             return [0, 0, 0]
  149.  
  150.  
  151. class AirPack(Pack):
  152.     def __init__(self, data):
  153.         Pack.__init__(self)
  154.         self.type = "AIR"
  155.         self.millis = None
  156.         self.deviation = None
  157.         self.range = None
  158.         self.parse(data)
  159.  
  160.     def parse(self, data):
  161.         self.deviation, self.timestamp, self.millis, self.range, self.checksum = struct.unpack(
  162.             "xxHIIHB", bytearray(data))
  163.  
  164.  
  165. class AccPack(Pack):
  166.     def __init__(self, data):
  167.         Pack.__init__(self)
  168.         self.type = "ACC"
  169.         self.accx = None
  170.         self.accy = None
  171.         self.accz = None
  172.         self.gyrx = None
  173.         self.gyry = None
  174.         self.gyrz = None
  175.         self.parse(data)
  176.  
  177.     def parse(self, data):
  178.         self.accx, self.timestamp, self.accy, self.accz, self.gyrx, self.gyry, self.gyrz, self.checksum = struct.unpack(
  179.             "xxhIhhhhhB", bytearray(data))
  180.  
  181.  
  182. class RSSIPack(Pack):
  183.     def __init__(self, data):
  184.         Pack.__init__(self)
  185.         self.type = "RSSI"
  186.         self.rssi = None
  187.         self.parse(data)
  188.  
  189.     def parse(self, data):
  190.         self.rssi, self.timestamp, self.checksum = struct.unpack(
  191.             "xxhIB", bytearray(data))
  192.  
  193.  
  194. type_dict = {
  195.     2: (TemperaturePack, 20),
  196.     3: (GPSPack, 25),
  197.     4: (AirPack, 15),
  198.     5: (AccPack, 19),
  199.     6: (RSSIPack, 9),
  200. }
  201.  
  202. if __name__ == "__main__":
  203.     p = Parser("data3", None)
  204.     p.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top