Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the format for comparison between RTCM packet preamble and current byte(#14) #15

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/ntrip_client/rtcm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@ def parse(self, buffer):
rtcm_packets = []
while index < len(combined_buffer):
# Find the start of the RTCM 3.2 packet
if combined_buffer[index] == _RTCM_3_2_PREAMBLE:
ascii_values = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me the buffer still comes in as a byte array by default, and this causes the node to crash for me. so we should have some logic in here to handle both cases. Also I think that you could use the encode function instead of looping. I had success with the following implementation, but as I said, my buffer is already bytes, so please let me know if this works for you

if isinstance(combined_buffer, str):
  ascii_values = combined_buffer.encode('utf-8')
elif isinstance(combined_buffer, bytes):
  ascii_values = combined_buffer
else:
  raise Exception('Buffer type is "{}" not a string or bytes, so we cannot parse it.'.format(type(combined_buffer)))

Also I think a better name for ascii_values would be combined_buffer_bytes

for ch in combined_buffer:
ascii_values.append(ord(ch))
if ascii_values[index] == _RTCM_3_2_PREAMBLE:
# Make sure we have enough data to find the length
if len(combined_buffer) <= index + 2:
if len(ascii_values) <= index + 2:
self._logdebug('Found beginning of RTCM packet at {}, but there is not enough data in the buffer to find the message length'.format(index))
self._caching_data = True
buffer = buffer[index:]
break

# Make sure we have enough data in the packet to validate it
message_length = (combined_buffer[index + 1] << 8 | combined_buffer[index + 2]) & 0x03FF
if index + message_length + 6 <= len(combined_buffer):
message_length = (ascii_values[index + 1] << 8 | ascii_values[index + 2]) & 0x03FF
if index + message_length + 6 <= len(ascii_values):
# Grab the packet from the buffer, and verify that it is valid by comparing checksums
packet = combined_buffer[index:index + message_length + 6]
packet = ascii_values[index:index + message_length + 6]
expected_checksum = packet[-3] << 16 | packet[-2] << 8 | packet[-1]
actual_checksum = self._checksum(packet[:-3])
if expected_checksum == actual_checksum:
Expand Down