■ ■ ■ ■ ■ ■
firmware/pycatsniffer/devpycatsniffer.py
| skipped 129 lines |
130 | 130 | | |
131 | 131 | | while self.running: |
132 | 132 | | if self.serial_port.in_waiting > 0: |
133 | | - | bytesteam = self.serial_port.read(self.serial_port.in_waiting) |
134 | | - | print ("RECV>> %s" % binascii.hexlify(bytesteam)) |
135 | | - | time.sleep(0.05) |
| 133 | + | bytestream = self.serial_port.read(self.serial_port.in_waiting) |
| 134 | + | print ("RECV>> %s" % binascii.hexlify(bytestream)) |
| 135 | + | time.sleep(0.5) |
| 136 | + | start_index = 0 |
| 137 | + | |
| 138 | + | while True: |
| 139 | + | # Find the index of the next occurrence of 0x40 0x53 |
| 140 | + | start_index = bytestream.find(b'\x40\x53', start_index) |
| 141 | + | # If not found, break out of the loop |
| 142 | + | if start_index == -1: |
| 143 | + | break |
| 144 | + | # Find the index of the next occurrence of 0x40 0x45 after the start index |
| 145 | + | end_index = bytestream.find(b'\x40\x45', start_index) |
| 146 | + | # If not found, break out of the loop |
| 147 | + | if end_index == -1: |
| 148 | + | break |
| 149 | + | # Get the substring between start_index and end_index |
| 150 | + | substring = bytestream[start_index:end_index+2] |
| 151 | + | # Do something with the substring |
| 152 | + | #print(substring) |
| 153 | + | print ("SUBSRECV>> %s" % binascii.hexlify(substring)) |
| 154 | + | # Set the start index to end_index + 2 (to skip over the 0x40 0x45 bytes) |
| 155 | + | start_index = end_index + 2 |
136 | 156 | | |
137 | | - | #if ret[0] == 0: |
138 | | - | # packet = self.parse_packet(ret) |
139 | | - | # if packet: |
140 | | - | # self.callback(packet) |
| 157 | + | |
| 158 | + | #if bytestream[0:2] == bytes([0x40, 0x53]): |
| 159 | + | # packet = self.parse_packet(bytestream) |
| 160 | + | #if packet: |
| 161 | + | # self.callback(packet) |
141 | 162 | | |
142 | 163 | | |
143 | 164 | | def set_channel(self, channel): |
| skipped 22 lines |
166 | 187 | | def get_channel(self): |
167 | 188 | | return self.channel |
168 | 189 | | |
| 190 | + | def parse_packet(self, packet): |
| 191 | + | |
| 192 | + | packetlen = packet[1] |
| 193 | + | |
| 194 | + | if len(packet) - 3 != packetlen: |
| 195 | + | return None |
| 196 | + | |
| 197 | + | # unknown header produced by the radio chip |
| 198 | + | header = packet[3:7].tostring() |
| 199 | + | |
| 200 | + | # the data in the payload |
| 201 | + | payload = packet[8:-2].tostring() |
| 202 | + | |
| 203 | + | # length of the payload |
| 204 | + | payloadlen = packet[7] - 2 # without fcs |
| 205 | + | |
| 206 | + | if len(payload) != payloadlen: |
| 207 | + | return None |
| 208 | + | |
| 209 | + | # current time |
| 210 | + | timestamp = time.gmtime() |
| 211 | + | |
| 212 | + | # used to derive other values |
| 213 | + | fcs1, fcs2 = packet[-2:] |
| 214 | + | |
| 215 | + | # rssi is the signed value at fcs1 |
| 216 | + | rssi = (fcs1 + 2**7) % 2**8 - 2**7 - 73 |
| 217 | + | |
| 218 | + | # crc ok is the 7th bit in fcs2 |
| 219 | + | crc_ok = fcs2 & (1 << 7) > 0 |
| 220 | + | |
| 221 | + | # correlation value is the unsigned 0th-6th bit in fcs2 |
| 222 | + | corr = fcs2 & 0x7f |
| 223 | + | |
| 224 | + | return Packet(timestamp, self.channel, header, payload, rssi, crc_ok, corr) |
| 225 | + | |
| 226 | + | |
169 | 227 | | def __repr__(self): |
170 | 228 | | |
171 | 229 | | if self.dev: |
| skipped 48 lines |
220 | 278 | | sniffer.initiatorc() |
221 | 279 | | sniffer.startc() |
222 | 280 | | print ("start") |
223 | | - | time.sleep(10) |
| 281 | + | time.sleep(5) |
224 | 282 | | sniffer.stop() |
225 | 283 | | sniffer.close() |
226 | 284 | | |