-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendHex.py
More file actions
60 lines (52 loc) · 1.75 KB
/
sendHex.py
File metadata and controls
60 lines (52 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import serial
import sys
import string
def read_in():
"""reads 6 chars from stdin"""
hexColor = sys.stdin.read(6)
return hexColor
def clear_in():
"""this is an imporvised way to clear stdin
implied \n termination of input"""
c = sys.stdin.read(1)
while(c!='\n'):
c = sys.stdin.read(1)
def char_range(c1, c2):
"""Generates the character list from `c1` to `c2`, inclusive."""
for c in range(ord(c1), ord(c2)+1):
yield chr(c)
def main():
portName = '/dev/ttyS2'
#portName = 'COM3'
"""This /dev/tty naming convention is used on linux,
though on Windows serial ports are named COM*,
though I am running this from an Ubuntu VM on a Windows machine, so I use /dev/tty
and have routed it through my VMware to my physical computer"""
sys.stdout.write("Openning serial with name "+portName+"\n")
sys.stdout.write("Change this in this files' (sendHex.py) src")
sys.stdout.flush()
ser = serial.Serial(portName, 9600)
sys.stdout.write("waiting for input\n")
sys.stdout.flush()
try:
while 1:
line = read_in()
clear_in()
for c in line:
if c not in string.hexdigits:
print("bad hex read")
sys.stdout.flush()
break
print("writing hex: "+"\033[1m"+line+'\033[0m') #bold characters
sys.stdout.flush()
ser.write(bytes(line, 'utf-8'))
line = ""
sys.stdout.flush()
except KeyboardInterrupt:
sys.stdout.write("quitting..\n")
sys.stdout.flush()
ser.close();
exit()
if __name__ == '__main__':
main()
ser.close()