本文整理汇总了Python中bitstring.BitStream.clear方法的典型用法代码示例。如果您正苦于以下问题:Python BitStream.clear方法的具体用法?Python BitStream.clear怎么用?Python BitStream.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bitstring.BitStream
的用法示例。
在下文中一共展示了BitStream.clear方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PacketDecoder
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import clear [as 别名]
class PacketDecoder(object):
HEADER = "0x59"
FOOTER = "0x5254464d"
LENGTH = 56 * 8
PATTERN = "2BH9I8B2I"
packet_store = []
def __init__(self, listener_func):
self.listener_func = listener_func
self.stream = BitStream()
def push(self, byte_array):
self.stream.append(byte_array)
def handle(self):
while True:
if len(self.stream) < self.LENGTH:
break
# Look for a header
header_pos = self.stream.find(self.HEADER)
if header_pos:
header_pos = header_pos[0]
else:
self.stream.clear()
break
end_pos = header_pos + self.LENGTH
if len(self.stream) < end_pos:
# Not Enough Data
break
# Extract potential packet
potential_packet = self.stream[header_pos:end_pos].bytes
valid_packet = self.analyze_packet(potential_packet)
if valid_packet:
self.stream = self.stream[end_pos:]
else:
self.stream = self.stream[header_pos + 1 :]
self.save_packets()
self.packet_store = []
def analyze_packet(self, byte_string):
if len(byte_string) != 56:
return
data = struct.unpack(self.PATTERN, byte_string)
header = data[0]
footer = data[21]
if hex(header) == self.HEADER and hex(footer) == self.FOOTER:
if self.validate_checksums(data):
packet = self.create_packet(data)
self.packet_store.append(packet)
return True
return False
def validate_checksums(self, packet_data):
# voltage_checksum = packet_data[12]
# current_checksum = packet_data[13]
# period_checksum = packet_data[14]
# active_checksum = packet_data[15]
# reactive_checksum = packet_data[16]
# apparent_power = packet_data[17]
# phase_angle_checksum = packet_data[18]
# power_factor_checksum = packet_data[19]
# checksum = packet_data[20]
return True
def create_packet(self, packet_data):
flags = packet_data[2]
epoch = packet_data[3]
voltage = packet_data[4] * (2.37748 * 10 ** -4) + -0.14427
current = packet_data[5] * (113240.82786) + 953.97194
period = packet_data[6]
active_power = packet_data[7]
reactive_power = packet_data[8]
apparent_power = packet_data[9]
phase_angle = packet_data[10]
power_factor = packet_data[11]
return Packet(
flags=flags,
epoch=epoch,
voltage=voltage,
current=current,
period=period,
active_power=active_power,
reactive_power=reactive_power,
apparent_power=apparent_power,
phase_angle=phase_angle,
power_factor=power_factor,
)
def save_packets(self):
# submit packets to lib
if self.packet_store:
self.listener_func(self.packet_store)
示例2: elif
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import clear [as 别名]
if( usbEndpoint == INCOMING_ENDPOINT and usbHeader[0] != 'ABC' ):
print 'Unexpected USB Header. Expected "0x414243", got "0x%s".' % ( usbHeader[0].encode( 'hex' ) )
raise Exception
messageBuffer.append( usbBuffer.read( usbHeader[1] * 8 ) )
# Clear the messageBuffer if we have a full message
# TODO - we need to be able to figure out if all 60 bytes are conusumed, but it's the end of the message
if( usbHeader[1] < USB_PACKET_SIZE ):
print >> sys.stderr, 'Message %s' % ( 'OUT' if usbEndpoint == OUTGOING_ENDPOINT else 'IN' )
print >> sys.stderr, 'Hex: %s' % ( messageBuffer.hex )
# TODO - make a bayerMessage to also handle standard command sequences and ASTM messages
if( messageBuffer.bytes[0:2] != 'Q\x03' ):
print >> sys.stderr, 'String: %s\n' % ( messageBuffer.bytes )
else:
msg = BayerBinaryMessage.MessageFactory( messageBuffer,
BayerBinaryMessage.OUT if usbEndpoint == OUTGOING_ENDPOINT else BayerBinaryMessage.IN, pumpSession )
if( msg is not None ):
if( isinstance( msg, MtGetAttachedPumpMessage ) ):
pumpSession.pumpSerial = msg.pumpSerial
pumpSession.stickSerial = msg.stickSerial
elif( isinstance( msg, MtFindPump ) ):
pumpSession.radioChannel = msg.radioChannel
print msg
print
messageBuffer.clear()
i+=1
print "Processed %d messages" % ( i )
示例3: dataCollect
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import clear [as 别名]
#.........这里部分代码省略.........
#if p_process_start.search(line) and complete_block is False:
# # skip part event log block
# complete_block = True
# continue
##if p_process_found is False and p_process_start.search(line):
## p_process_found = True
## continue
##if p_process_found and skip_lines > 0:
## skip_lines -= 1
## continue
##if p_process_found and p_close.search(line):
## p_process_found = False
## break
##if args.maxNumEventEntries :
## if maxNumLines > 0:
## maxNumLines -= 1
## else:
## break
currentLine += 1 #ag counter from 1
if currentLine >= startLine and currentLine < endLine: #[startline,endline)
if (currentLine - startLine)%1000 == 0:
print("{} entries collected".format((currentLine - startLine)*2))
##if p_process_found and skip_lines == 0:
#m = p_data.search(line)
##print(line)
#data = m.group(1).split(" ")
#if len(data) == 4:
# eventLogEntryBitStream.append('0x'+data[0])
# eventLogEntryBitStream.append('0x'+data[1])
# eventLogEntryBitStream.append('0x'+data[2])
# eventLogEntryBitStream.append('0x'+data[3])
# self.dataEntryAppend(eventLogEntryBitStream)
# eventLogEntryBitStream.clear()
# entryList.append(entryIndex)
# entryIndex += 1
#else:
# # two event entries of 64 bits for each
# eventLogEntryBitStream.append('0x'+data[0])
# eventLogEntryBitStream.append('0x'+data[1])
# eventLogEntryBitStream.append('0x'+data[2])
# eventLogEntryBitStream.append('0x'+data[3])
# self.dataEntryAppend(eventLogEntryBitStream)
# eventLogEntryBitStream.clear()
# entryList.append(entryIndex)
# entryIndex += 1
# eventLogEntryBitStream.append('0x'+data[4])
# eventLogEntryBitStream.append('0x'+data[5])
# eventLogEntryBitStream.append('0x'+data[6])
# eventLogEntryBitStream.append('0x'+data[7])
# self.dataEntryAppend(eventLogEntryBitStream)
# eventLogEntryBitStream.clear()
# entryList.append(entryIndex)
# entryIndex += 1
data = line.strip().split(" ")
# offset '' entries
if len(data) == 6:
eventLogEntryBitStream.append('0x'+data[2])
eventLogEntryBitStream.append('0x'+data[3])
eventLogEntryBitStream.append('0x'+data[4])
eventLogEntryBitStream.append('0x'+data[5])
self.dataEntryAppend(eventLogEntryBitStream)
eventLogEntryBitStream.clear()
entryList.append(entryIndex)
entryIndex += 1
else:
# two event entries of 64 bits for each
eventLogEntryBitStream.append('0x'+data[2])
eventLogEntryBitStream.append('0x'+data[3])
eventLogEntryBitStream.append('0x'+data[4])
eventLogEntryBitStream.append('0x'+data[5])
self.dataEntryAppend(eventLogEntryBitStream)
eventLogEntryBitStream.clear()
entryList.append(entryIndex)
entryIndex += 1
eventLogEntryBitStream.append('0x'+data[6])
eventLogEntryBitStream.append('0x'+data[7])
eventLogEntryBitStream.append('0x'+data[8])
eventLogEntryBitStream.append('0x'+data[9])
self.dataEntryAppend(eventLogEntryBitStream)
eventLogEntryBitStream.clear()
entryList.append(entryIndex)
entryIndex += 1
print("--finish 64 bit eventlog entry collection")
pass