Created
January 7, 2025 15:26
-
-
Save pelwell/e70c9d720acf8b17f4f724d3026557e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/usr/python | |
import os; windows = os.name == "nt" | |
import sys | |
import serial | |
import threading | |
import time | |
comPort = None | |
def TransmitThread(): | |
while comPort: | |
for n in range(ord("A"),ord("Z")+1): | |
if comPort: | |
comPort.write(chr(n).encode()) | |
time.sleep(1) | |
def ReceiveThread(): | |
while comPort: | |
if comPort.inWaiting() > 0: | |
n = comPort.read(1) | |
print(chr(ord(n))) | |
else: | |
time.sleep(0.1) | |
def LoopbackTest(comPortName, comBaudRate): | |
global comPort | |
print("Loopback Test on " + comPortName+" at " + str(comBaudRate) + " baud") | |
time.sleep(1) | |
comPort = serial.Serial \ | |
( | |
port = comPortName, | |
baudrate = comBaudRate, | |
parity = serial.PARITY_NONE, | |
stopbits = serial.STOPBITS_ONE, | |
bytesize = serial.EIGHTBITS | |
) | |
threading.Thread(target=TransmitThread).start() | |
threading.Thread(target=ReceiveThread).start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
print("") | |
finally: | |
comPort = None | |
if __name__ == "__main__": | |
if len(sys.argv) <= 1: | |
if windows : LoopbackTest("COM1", 9600) | |
else : LoopbackTest("/dev/serial0", 9600) | |
elif len(sys.argv) <= 2 : LoopbackTest(sys.argv[1], 9600) | |
else : LoopbackTest(sys.argv[1], int(sys.argv[2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment