1
ewconfig/scripts/kxsend
2023-11-30 18:37:20 -05:00

57 lines
1.7 KiB
Python
Executable File

#! /usr/bin/env python3
import argparse
import sys
import logging
import serial
logger = logging.getLogger(__name__)
def main() -> int:
# Handle program arguments
ap = argparse.ArgumentParser(
prog="kxsend", description="Send a string over CW on a KX2"
)
ap.add_argument("text", help="Text to send", nargs="+")
ap.add_argument("--speed", help="Transmit speed in WPM", type=int)
ap.add_argument("--device", "-d", help="Serial device", default="/dev/ttyUSB0")
ap.add_argument("--baud", "-b", help="Serial baud rate", default=38400, type=int)
ap.add_argument(
"-v", "--verbose", help="Enable verbose logging", action="store_true"
)
args = ap.parse_args()
args.text = " ".join(args.text)
# Configure logging
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO,
format="%(levelname)s: %(message)s",
)
# Connect to the radio
logger.debug(f"Connecting to radio: {args.device}")
serial_conn = serial.Serial(args.device, args.baud)
# If we have a specific speed, set it
if args.speed:
cmd = "KS{:0>3};".format(args.speed)
logger.debug(f"Sending command: {cmd}")
serial_conn.write(cmd.encode("ascii"))
# Break the text into max 24 character chunks
chunks = [args.text[i:i+24] for i in range(0, len(args.text), 24)]
logger.info(f"Sending {len(chunks)} chunks")
# Handle each chunk
for chunk in chunks:
# Send the text
cmd = f"KY {chunk};"
logger.debug(f"Sending command: {cmd}")
serial_conn.write(cmd.encode("ascii"))
return 0
if __name__ == "__main__":
sys.exit(main())