|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import serial |
| 5 | +import time |
| 6 | + |
| 7 | +# Function to calculate NMEA checksum |
| 8 | +def calculate_nmea_checksum(nmea_sentence): |
| 9 | + checksum = 0 |
| 10 | + # Iterate through each character after the starting '$' and before '*' |
| 11 | + for char in nmea_sentence[1:]: |
| 12 | + checksum ^= ord(char) |
| 13 | + return f"{nmea_sentence}*{checksum:02X}" |
| 14 | + |
| 15 | +# Function to append checksum if not provided |
| 16 | +def append_checksum_if_missing(nmea_sentence): |
| 17 | + if '*' not in nmea_sentence: |
| 18 | + # Calculate and append checksum if '*' is missing |
| 19 | + return calculate_nmea_checksum(nmea_sentence) |
| 20 | + return nmea_sentence |
| 21 | + |
| 22 | +# Function to handle serial communication |
| 23 | +def send_nmea_command(port, speed, timeout, nmea_command, verbose): |
| 24 | + # Open serial port |
| 25 | + try: |
| 26 | + with serial.Serial(port, baudrate=speed, timeout=timeout) as ser: |
| 27 | + # Append checksum if missing |
| 28 | + nmea_command_with_checksum = append_checksum_if_missing(nmea_command) |
| 29 | + |
| 30 | + # Write NMEA command to the serial port |
| 31 | + ser.write((nmea_command_with_checksum + '\r\n').encode('ascii')) |
| 32 | + if verbose: |
| 33 | + print(f"Sent command: {nmea_command_with_checksum}") |
| 34 | + |
| 35 | + # Wait for the response |
| 36 | + start_time = time.time() |
| 37 | + while time.time() - start_time < timeout: |
| 38 | + try: |
| 39 | + response = ser.readline().decode('ascii', errors='ignore').strip() |
| 40 | + # Process only valid ASCII responses |
| 41 | + if response and response.startswith('$'): |
| 42 | + if verbose: |
| 43 | + print(f"Received response: {response}") |
| 44 | + return response |
| 45 | + except UnicodeDecodeError as e: |
| 46 | + # Skip non-ASCII responses (likely RTCM3 messages) |
| 47 | + if verbose: |
| 48 | + print(f"Non-ASCII data skipped: {e}") |
| 49 | + if verbose: |
| 50 | + print("Timeout: No matching response received.") |
| 51 | + except serial.SerialException as e: |
| 52 | + print(f"Error opening serial port: {e}") |
| 53 | + |
| 54 | +# Function to read NMEA commands from a file and ignore lines starting with '#' and blank lines |
| 55 | +def read_commands_from_file(file_path): |
| 56 | + try: |
| 57 | + with open(file_path, 'r') as file: |
| 58 | + commands = [] |
| 59 | + for line in file: |
| 60 | + line = line.strip() |
| 61 | + # Ignore blank lines and lines starting with '#' |
| 62 | + if line and not line.startswith('#') or line.startswith('#SLEEP#'): |
| 63 | + commands.append(line) |
| 64 | + return commands |
| 65 | + except FileNotFoundError: |
| 66 | + print(f"Error: File '{file_path}' not found.") |
| 67 | + return [] |
| 68 | + |
| 69 | +# Function to handle the sleep command in the file |
| 70 | +def handle_sleep_command(command, verbose): |
| 71 | + try: |
| 72 | + sleep_time_ms = int(command.split('#SLEEP# ')[1]) |
| 73 | + if verbose: |
| 74 | + print(f"Sleeping for {sleep_time_ms} ms") |
| 75 | + time.sleep(sleep_time_ms / 1000) # Convert to seconds |
| 76 | + except (IndexError, ValueError): |
| 77 | + print(f"Invalid sleep command format: {command}") |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + # Parse command line arguments |
| 81 | + parser = argparse.ArgumentParser(description="Send NMEA commands to Quectel LC29H module") |
| 82 | + parser.add_argument('port', type=str, help='Serial port to use (e.g., /dev/ttyUSB0 or COM3)') |
| 83 | + parser.add_argument('speed', type=int, help='Baud rate (e.g., 9600)') |
| 84 | + parser.add_argument('timeout', type=int, help='Timeout in seconds') |
| 85 | + parser.add_argument('command', nargs='?', type=str, help='NMEA command to send (optional, overrides file)') |
| 86 | + parser.add_argument('--file', type=str, help='File with NMEA commands to send') |
| 87 | + parser.add_argument('--verbose', action='store_true', help='Enable verbose output for tracing') |
| 88 | + |
| 89 | + args = parser.parse_args() |
| 90 | + |
| 91 | + # Determine which commands to send (from file or argument) |
| 92 | + if args.command: |
| 93 | + # Send the provided command as an argument |
| 94 | + nmea_commands = [args.command] |
| 95 | + elif args.file: |
| 96 | + # Read commands from the file, ignoring comments and blank lines |
| 97 | + nmea_commands = read_commands_from_file(args.file) |
| 98 | + else: |
| 99 | + print("Error: You must provide either a command or a file containing commands.") |
| 100 | + exit(1) |
| 101 | + |
| 102 | + # Send each NMEA command from the list |
| 103 | + for command in nmea_commands: |
| 104 | + if command.startswith('#SLEEP#'): |
| 105 | + handle_sleep_command(command, args.verbose) |
| 106 | + else: |
| 107 | + send_nmea_command(args.port, args.speed, args.timeout, command, args.verbose) |
0 commit comments