import subprocess
import os
import sys
def process_line(msg_type, send_addr, recv_addr, node_rx, last_tx, last_rx):
"""
User-defined function to process the parsed parameters.
This is a placeholder; modify as needed.
Parameters:
- msg_type (int): The message type (0-3)
- send_addr (int): The send address as a little-endian 16-bit integer
- recv_addr (int): The receive address as a little-endian 16-bit integer
- node_rx (int): The node_rx as a little-endian 40-bit integer
- last_tx (int): The last_tx as a little-endian 40-bit integer
- last_rx (int): The last_rx as a little-endian 40-bit integer
"""
print(f"Processed line: msg_type={msg_type}, send_addr={send_addr}, recv_addr={recv_addr}, node_rx={node_rx}, last_tx={last_tx}, last_rx={last_rx}")
def main():
script_dir = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(script_dir, 'syslogger')
try:
proc = subprocess.Popen([executable_path, '-D', '/dev/ttyACM0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
except FileNotFoundError:
print(f"Error: Executable 'syslogger' not found in {script_dir}")
sys.exit(1)
except Exception as e:
print(f"Error starting subprocess: {e}")
sys.exit(1)
for line in proc.stdout:
line = line.strip() # Remove trailing newline
parts = line.split() # Split by spaces
if len(parts) != 21: # Expect exactly 21 hex strings
print(f"Invalid line (wrong number of parts): {line}")
continue
try:
bytes_list = [int(x, 16) for x in parts] # Convert hex strings to integers
except ValueError:
print(f"Invalid hex values in line: {line}")
continue
if bytes_list[0] != 1: # Verify the first byte
print(f"First byte is not 01: {bytes_list[0]}")
continue # Skip this line
msg_type = bytes_list[1] # 1 byte: msg_type (0-3)
# Extract and convert the rest
send_addr = int.from_bytes(bytes([bytes_list[2], bytes_list[3]]), 'little') # 2 bytes: send_addr
recv_addr = int.from_bytes(bytes([bytes_list[4], bytes_list[5]]), 'little') # 2 bytes: recv_addr
node_rx = int.from_bytes(bytes(bytes_list[6:11]), 'little') # 5 bytes: node_rx
last_tx = int.from_bytes(bytes(bytes_list[11:16]), 'little') # 5 bytes: last_tx
last_rx = int.from_bytes(bytes(bytes_list[16:21]), 'little') # 5 bytes: last_rx
# Call the user-defined function
process_line(msg_type, send_addr, recv_addr, node_rx, last_tx, last_rx)
# Wait for the process to finish
proc.wait()
if proc.returncode != 0:
print(f"syslogger exited with code {proc.returncode}")
# Optionally print stderr
try:
error_output = proc.stderr.read()
if error_output:
print(f"stderr output: {error_output}")
except Exception:
pass # stderr might already be closed
if __name__ == "__main__":
main()
Explanation:
- Running the Executable: The script uses
subprocess.Popen to run ./syslogger -D /dev/ttyACM0. It captures stdout and stderr.
- Capturing and Parsing Lines: The script iterates over the stdout lines. For each line:
- It splits the line into parts (hex strings).
- Verifies there are exactly 21 parts.
- Converts the hex strings to a list of integers.
- Checks if the first byte is 1.
- Extracts and converts the specified fields using
int.from_bytes with little-endian byte order.
- Calling the User-Defined Function: After parsing, it calls
process_line with the extracted parameters: msg_type, send_addr, recv_addr, node_rx, last_tx, and last_rx.
- Error Handling:
- Skips invalid lines (e.g., wrong number of parts, invalid hex, or first byte not 1).
- Handles exceptions when starting the subprocess.
- Waits for the process to end and checks the return code.
You can modify the process_line function to suit your needs, such as logging to a file or performing further processing. Ensure the script is in the same directory as the syslogger executable.