| #!/usr/bin/env python |
| # |
| # Copyright 2023 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| import argparse |
| import sys |
| import serial |
| from pexpect_serial import SerialSpawn |
| |
| |
| def main(): |
| parser = argparse.ArgumentParser(description="Run commands on nexus MCU") |
| parser.add_argument( |
| "--nexus_id", |
| action="store", |
| required=True, |
| help="Two digit numeric ID of the nexus board to control") |
| parser.add_argument("command", action='store') |
| args = parser.parse_args() |
| |
| fd = serial.Serial(f"/dev/Nexus-FTDI-{args.nexus_id}-MCU-UART", "115200") |
| port = SerialSpawn(fd) |
| # Redirect all port chatter to stdout buffer since it's in binary mode |
| port.logfile = sys.stdout.buffer |
| |
| port.send(f"{args.command}\r\n") |
| reply = port.expect([">", "Unrecognized command: .*\r\n>"]) |
| if reply: |
| print(f"MCU did not recognize the command \"{args.command}\"") |
| sys.exit(1) |
| |
| |
| if __name__ == "__main__": |
| main() |