Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
Revised version: now able to take input from a serial port and, on Li…
Browse files Browse the repository at this point in the history
…nux, send output to a virtual serial port.

Test: none
  • Loading branch information
RobMeades committed Mar 18, 2024
1 parent 63a2610 commit 2fdb23e
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions gnss/api/u_gnss_ucenter_ubx.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,31 @@ def main(source, output_file, responses_only, echo_on, baud_rate):
'''Main as a function'''
return_value = 1

try:
# Open the source
reading_from_device = False
# Open the source
reading_from_device = False
source_handle = None
if os.path.isfile(source):
try:
source_handle = open(source, "r", encoding="utf8")
except FileNotFoundError:
print(f"Cannot open \"{source}\" as a file, trying to open it as a serial device.")
try:
# Open what is assumed to be a serial device
source_handle = serial.Serial(baudrate=baud_rate, timeout=0.05)
source_handle.port = source
source_handle.open()
reading_from_device = True
except (ValueError, serial.SerialException) as ex:
print(f"{type(ex).__name__} while opening port \"{source}\" for source.")
source_handle = None
if source_handle:
# Open either the output file or a virtual
# device that the caller can connect uCenter to
output_file_handle = None
device_output = None
except (NameError, FileNotFoundError, PermissionError) as ex:
print(f"{type(ex).__name__} while trying to open \"{source}\".")
else:
print(f"\"{source}\" is not a file, trying to open it as a device.")
try:
# Open what is assumed to be a serial device
source_handle = serial.Serial(baudrate=baud_rate, timeout=0.05)
source_handle.port = source
source_handle.open()
reading_from_device = True
except (ValueError, serial.SerialException) as ex:
print(f"{type(ex).__name__} while opening device \"{source}\".")
source_handle = None
if source_handle:
# Open either the output file or a virtual
# device that the caller can connect uCenter to
output_file_handle = None
writing_to_device = False
try:
if output_file:
if not os.path.splitext(output_file)[1]:
output_file += "." + OUTPUT_FILE_EXTENSION
Expand All @@ -209,20 +213,21 @@ def main(source, output_file, responses_only, echo_on, baud_rate):
_, device_output = pty.openpty()
output_file = os.ttyname(device_output)
output_file_handle = serial.Serial(output_file)
writing_to_device = True
except (NameError, FileNotFoundError, PermissionError) as ex:
print(f"{type(ex).__name__} while trying to create virtual device" \
f" for output: : {str(ex)}.")
else:
if not output_file_handle:
# Output file name is source file with modified extension
output_file = os.path.splitext(output_file)[0] + "." + OUTPUT_FILE_EXTENSION
output_file = os.path.splitext(source)[0] + "." + OUTPUT_FILE_EXTENSION
output_file_handle = open(output_file, "wb")
if output_file_handle:
print_text = f"Reading from {source}"
if output_file:
print_text += f", writing to {output_file}"
print_text += f", looking for lines containing \"{GNSS_LOG_LINE_MARKER_RECEIVE}\""
if not responses_only:
print_text += f" and \"{GNSS_LOG_LINE_MARKER_COMMAND}\""
print_text += f" and \"{GNSS_LOG_LINE_MARKER_COMMAND}\"..."
print(print_text)
if reading_from_device:
print("Use CTRL-C to stop.")
Expand Down Expand Up @@ -259,15 +264,15 @@ def main(source, output_file, responses_only, echo_on, baud_rate):
pass
print(f"Found {message_count} UBX messages(s) in {line_number} line(s).")
source_handle.close()
if output_file and not device_output and message_count > 0:
if output_file and not writing_to_device and message_count > 0:
print(f"File {output_file} has been written: you may open it in uCenter.")
return_value = 0
if output_file_handle:
output_file_handle.close()
except (NameError, FileNotFoundError, PermissionError) as ex:
print(f"{type(ex).__name__} while trying to open {output_file} for writing.")
else:
print(f"Unable to open \"{source}\".")
except (NameError, FileNotFoundError, PermissionError) as ex:
print(f"{type(ex).__name__} while trying to open {output_file} for writing.")
else:
print(f"Unable to open \"{source}\".")

return return_value

Expand All @@ -288,12 +293,12 @@ def main(source, output_file, responses_only, echo_on, baud_rate):
" if no extension is given ." + \
OUTPUT_FILE_EXTENSION + " will be added)." \
" If this parameter is omitted and the" \
" source is a device then, on Linux, the" \
" output will be written to a virtual COM" \
" port to which you may connect uCenter" \
" source is a device then, on Linux only" \
" I'm afraid, the output will be written" \
" to a PTY to which you may connect uCenter"\
" directly. Otherwise the output will" \
" be written to the same name as the source" \
" but with extension " + \
" be written to the same name as the source"\
" but with extension ." + \
OUTPUT_FILE_EXTENSION + ".")
PARSER.add_argument("-r", action="store_true", help="include" \
" only the responses from the GNSS device" \
Expand Down

0 comments on commit 2fdb23e

Please sign in to comment.