def finishframe(self, symbols):
# look for flag at end.
flagcorr = numpy.correlate(symbols, [-1, 1, 1, 1, 1, 1, 1, 1, -1])
cimax = numpy.argmax(flagcorr) # index of first flag
cimin = numpy.argmin(flagcorr) # index of first inverted flag
if flagcorr[cimax] == 9 and flagcorr[cimin] == -9:
# they are both proper flags
ci = min(cimax, cimin)
elif flagcorr[cimax] == 9:
ci = cimax
else:
ci = cimin
symbols = symbols[0:ci+1]
# un-nrzi symbols to get bits.
bits = numpy.where(numpy.equal(symbols[:-1], symbols[1:]), 1, 0)
# un-bit-stuff. every sequence of five ones must be followed
# by a zero, which we should delete.
nones = 0
nbits = [ ]
for i in range(0, len(bits)):
if nones == 5:
nones = 0
# assuming bits[i] == 0
# don't append the zero...
elif bits[i] == 1:
nones += 1
nbits.append(bits[i])
else:
nbits.append(bits[i])
nones = 0
bits = nbits
if len(bits) < 8:
return [ 0, None, 0 ]
# convert bits to bytes.
# bits[] is least-significant-first, but
# numpy.packbits() wants MSF.
bits = numpy.array(bits)
bits = bits[0:(len(bits)/8)*8]
assert (len(bits)%8) == 0
bits = bits[::-1]
bytes = numpy.packbits(bits)
bytes = bytes[::-1]
msg = None
ok = self.checkpacket(bytes)
if ok > 0 or len(bytes) > 16:
msg = self.printpacket(bytes)
return [ ok, msg, len(symbols) ]
# 0: syntactically unlikely to be a packet
# 1: syntactically plausible but crc failed
# 2: crc is correct
评论列表
文章目录