def test_encode_ascii_6_bit(): input_val = '001000000101001100001100001111100000010111001111010010001100000100100001' b = bitarray.bitarray(input_val) ascii6, padding = encode_ascii_6(b) assert ascii6 == "85<<?PG?B<4Q" assert padding == 0 bit_arr = decode_into_bit_array(ascii6.encode()) assert bit_arr.to01() == input_val assert decode_bin_as_ascii6(bit_arr) == "HELLO WORLD!"
def __init__(self, raw: bytes) -> None: if not isinstance(raw, bytes): raise ValueError(f"'NMEAMessage' only accepts bytes, but got '{type(raw)}'") validate_message(raw) # Initial values self.checksum: int = -1 # Store raw data self.raw: bytes = raw # An AIS NMEA message consists of seven, comma separated parts values = raw.split(b",") # Unpack NMEA message parts ( head, message_fragments, fragment_number, message_id, channel, payload, checksum ) = values # The talker is identified by the next 2 characters talker: str = head[1:3].decode('ascii') self.talker: TalkerID = TalkerID(talker) # The type of message is then identified by the next 3 characters self.type: str = head[3:].decode('ascii') # Total number of fragments self.message_fragments: int = int(message_fragments) # Current fragment index self.fragment_number: int = int(fragment_number) # Optional message index for multiline messages self.message_id: Optional[int] = int(message_id) if message_id else None # Channel (A or B) self.channel: str = channel.decode('ascii') # Decoded message payload as byte string self.payload: bytes = payload # Fill bits (0 to 5) self.fill_bits: int = int(chr(checksum[0])) # Message Checksum (hex value) self.checksum = int(checksum[2:], 16) # Finally decode bytes into bits self.bit_array: bitarray = decode_into_bit_array(self.payload) self.ais_id: int = get_int(self.bit_array, 0, 6)
def __init__(self, raw: bytes): # Set all values to None initially [setattr(self, name, None) for name in self.__slots__] # Store raw data self.raw = raw # An AIS NMEA message consists of seven, comma separated parts values = raw.split(b",") # Only encapsulated messages are currently supported if values[0][0] != 0x21: return if len(values) != 7: raise ValueError("Invalid NMEA message provided. " "A NMEA message needs to have exactly 7 comma separeted entries.") # Unpack NMEA message parts ( head, count, index, seq_id, channel, data, checksum ) = values # The talker is identified by the next 2 characters self.talker = head[1:3].decode('ascii') # The type of message is then identified by the next 3 characters self.msg_type = head[3:].decode('ascii') # Store other important parts self.count = int(count) self.index = int(index) self.seq_id = seq_id self.channel = channel self.data = data self.checksum = int(checksum[2:], 16) # Verify if the checksum is correct if not self.is_valid: raise ValueError("Invalid Checksum. Message is invalid!") # Finally decode bytes into bits self.bit_array = decode_into_bit_array(self.data) self.ais_id = get_int(self.bit_array, 0, 6)
def __init__(self, raw: bytes) -> None: # Initial values self.checksum: int = -1 # Store raw data self.raw: bytes = raw # An AIS NMEA message consists of seven, comma separated parts values = raw.split(b",") # Only encapsulated messages are currently supported if values[0][0] != 0x21: return if len(values) != 7: raise InvalidNMEAMessageException( "A NMEA message needs to have exactly 7 comma separated entries." ) # Unpack NMEA message parts (head, count, index, seq_id, channel, data, checksum) = values # The talker is identified by the next 2 characters talker: str = head[1:3].decode('ascii') self.talker: TalkerID = TalkerID(talker) # The type of message is then identified by the next 3 characters self.msg_type: str = head[3:].decode('ascii') # Store other important parts self.count: int = int(count) self.index: int = int(index) self.seq_id: bytes = seq_id self.channel: bytes = channel self.data: bytes = data self.checksum = int(checksum[2:], 16) # Verify if the checksum is correct if not self.is_valid: raise InvalidChecksumException( f"Invalid Checksum. Expected {self.checksum}, got {compute_checksum(self.data)}." ) # Finally decode bytes into bits self.bit_array: bitarray = decode_into_bit_array(self.data) self.ais_id: int = get_int(self.bit_array, 0, 6)
def __init__(self, raw: bytes) -> None: if not isinstance(raw, bytes): raise ValueError( f"'NMEAMessage' only accepts bytes, but got '{type(raw)}'") validate_message(raw) # Initial values self.checksum: int = -1 # Store raw data self.raw: bytes = raw # An AIS NMEA message consists of seven, comma separated parts values = raw.split(b",") # Unpack NMEA message parts (head, count, index, seq_id, channel, data, checksum) = values # The talker is identified by the next 2 characters talker: str = head[1:3].decode('ascii') self.talker: TalkerID = TalkerID(talker) # The type of message is then identified by the next 3 characters self.msg_type: str = head[3:].decode('ascii') # Store other important parts self.count: int = int(count) self.index: int = int(index) self.seq_id: bytes = seq_id self.channel: bytes = channel self.data: bytes = data self.checksum = int(checksum[2:], 16) # Finally decode bytes into bits self.bit_array: bitarray = decode_into_bit_array(self.data) self.ais_id: int = get_int(self.bit_array, 0, 6)