Created
July 24, 2018 15:18
-
-
Save JimCircadian/23e581b75dd1ef5f59db4a077c45b746 to your computer and use it in GitHub Desktop.
A little serial testing utility (found it helpful for identifying character stripping)
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
# Run this first, then recv, then send | |
socat pty,link=/tmp/ttySEND pty,link=/tmp/ttyRECV |
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
#!/usr/bin/env python3 | |
import argparse | |
import os | |
import serial | |
import time as t | |
if __name__ == "__main__": | |
a = argparse.ArgumentParser() | |
a.add_argument('action', choices=("send", "recv")) | |
a.add_argument('--baud', type=int, default=9200) | |
a.add_argument('--ack', default="\r") | |
a.add_argument('--encoding', default="latin-1") | |
a.add_argument('--range-start', type=int, default=0) | |
a.add_argument('--range-end', type=int, default=256) | |
args = a.parse_args() | |
s = serial.Serial( | |
port="/tmp/tty{}".format(args.action.upper()), | |
baudrate=args.baud, | |
bytesize=serial.EIGHTBITS, | |
parity=serial.PARITY_NONE, | |
stopbits=serial.STOPBITS_ONE, | |
rtscts=True, | |
dsrdtr=True, | |
) | |
os.unlink("{}.out".format(args.action)) | |
if args.action == "send": | |
for value in range(args.range_start, args.range_end): | |
print("Sending {}".format(value)) | |
s.write(chr(value).encode(args.encoding)) | |
ch = s.read(len(args.ack)) | |
with open("send.out", "a") as sd: | |
sd.write("{:3d}\t{}\n".format(value, chr(value).encode(args.encoding))) | |
if ch == args.ack.encode(): | |
print("Ack'd") | |
elif args.action == "recv": | |
i = 0 | |
while True: | |
data = s.read(1) | |
print("Received {}".format(i)) | |
with open("recv.out", "a") as rd: | |
rd.write("{:3d}\t{}\n".format(i, data)) | |
s.write(args.ack.encode(args.encoding)) | |
i += 1 | |
print("Finished") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment