-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleserial.py
executable file
·178 lines (156 loc) · 6.47 KB
/
simpleserial.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python2.7
###############################################################################
#
# simpleserial.py - Michael J. Smalley
#
# Interactive serial connection interface
#
# Requires:
# Python 2.7+
# pyserial (http://pyserial.sourceforge.net/) -- to install run:
# pip install pyserial
#
###############################################################################
import argparse
import serial
import subprocess
import serial.tools.list_ports
import time
from sys import platform
def stopbits_convert(value):
''' Converts value to constant understood by pyserial'''
if value=="1":
stopbits = serial.STOPBITS_ONE
elif value=="1.5":
stopbits = serial.STOPBITS_ONE_POINT_FIVE
elif value=="2":
stopbits = serial.STOPBITS_TWO
else:
stopbits = stopbits_convert(generate_menu("Available Stopbits", ['1', '1.5', '2'], "Choose a stopbits value"))
return stopbits
def parity_convert(parityvalue):
if parityvalue in ("None", "none", "NONE", "N", "n"):
parity = serial.PARITY_NONE
elif parityvalue in ("Even", "even", "EVEN", "E", "e"):
parity = serial.PARITY_EVEN
elif parityvalue in ("Odd", "odd", "ODD", "O", "o"):
parity = serial.PARITY_ODD
else:
parity = parity_convert(generate_menu("Available Parities", ['None', 'Even', 'Odd'], "Choose a parity"))
return parity
def generate_menu(title, options, promptmsg):
print title
print "=" * len(title)
for option in enumerate(options):
print "%d - %s" % option
try:
choice = int(raw_input(promptmsg + ": "))
except ValueError:
print "Not a valid choice."
try:
chosen = options[choice]
except IndexError:
print "Invalid choice."
return chosen
def handshake_selection():
print "Hardware handshake On or Off?"
print "======================="
handshake = ['Off', 'On']
for item in enumerate(handshake):
print "%d - %s" % item
try:
choice = int(raw_input("Choose whether Hardware Handshake should be on or off for serial communications: "))
except ValueError:
print "Not a number."
try:
chosen = handshake[choice]
except IndexError:
print "Invalid hardware handshake choice."
return chosen
def simpleserial():
# Set up command line argument parser
parser = argparse.ArgumentParser(description='A simple serial connection handler.')
parser.add_argument('-i', '--interface', dest='interface', type=str, help='path to a serial interface, (e.g. /dev/tty.interfacename)')
parser.add_argument('-r', '--baudrate', dest='baudrate', help='Desired baud rate, (e.g. 9600)')
parser.add_argument('-d', '--databits', dest='databits', type=str, help='Desired data bits (e.g. 5, 6, 7 or 8)')
parser.add_argument('-s', '--stopbits', dest='stopbits', type=str, help='Desired stop bits (e.g. 1, 1.5, or 2)')
parser.add_argument('-p', '--parity', dest='parity', type=str, help='Desired parity (e.g. None, Even, Odd, Mark, or Space)')
parser.add_argument('-c', '--flowcontrol', dest='flowcontrol', type=str, help='Flow control on or off')
parser.add_argument('-a', '--handshake', dest='handshake', type=str, help='Hardware handshake on or off')
messagesource = parser.add_mutually_exclusive_group()
messagesource.add_argument('-m', '--message', dest='message', type=str, help='Message to send over the serial connection')
messagesource.add_argument('-f', '--filename', dest='filename', type=str, help='Path to a comma-delimted file containing commands to loop over')
args = parser.parse_args()
# Get value or user selection for serial connection properties and initialize it
interface = args.interface if args.interface else generate_menu("Available Serial Devices", [interface[0] for interface in serial.tools.list_ports.comports()], "Choose an interface")
baudrate = args.baudrate if args.baudrate else generate_menu("Available Baud Rates", [110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000, 57600, 115200], "Choose a baud rate")
databits = args.databits if args.databits else generate_menu("Available Databits", [7, 8], "Choose a databits setting: ")
stopbits = stopbits_convert(args.stopbits)
parity = parity_convert(args.parity)
if args.flowcontrol in ("0", "none", "NONE", "N", "n", "No", "Off", "off"):
flowcontrol = "Off"
elif args.flowcontrol in ("1", "Yes", "yes", "On", "on"):
flowcontrol = "On"
else:
flowcontrol = generate_menu("Available Flow Controls", ["Off", "On"], "Flow control on or off")
# handshake = args.handshake if args.handshake else generate_menu("Hardware handshake Off or On?", ["Off", "On"], "Hardware handshake be on or off")
if args.filename:
with open(args.filename) as f:
messagelist = f.readlines()
elif args.message:
message = args.message
else:
print "Please enter a message to send over the serial connection (press [Enter] when finished):"
message = raw_input("> ")
# Assign values to ser object
ser = serial.Serial()
ser.port = interface
ser.baudrate = baudrate
ser.bytesize = int(databits)
ser.stopbits = stopbits
ser.parity = parity
ser.xonxoff = flowcontrol
# sets read timeout to 5 seconds
ser.timeout = 5
###
# STILL NEED TO IMPLEMENT HARDWARE HANDSHAKE
# if rts:
# ser.rtscts = handshake
# else if dsr:
# ser.dsrdtr = handshake
###
# Output information about the ser object
print "Serial Device: " + str(ser.name)
print "Baud Rate: " + str(ser.baudrate)
print "Data Bits: " + str(ser.bytesize)
print "Stop Bits: " + str(ser.stopbits)
print "Parity: " + str(ser.parity)
print "Flow Control: " + str(ser.xonxoff)
try: message
except NameError:
print "Message:\n"
for line in messagelist:
print line
else:
print "Message: " + str(message)
###
# STILL NEED TO IMPLEMENT HARDWARE HANDSHAKE
###
# Write to the serial interface
ser.open()
try: message
except NameError:
for line in messagelist:
time.sleep(.5)
line = line.split(',')
ser.write(str(line[0])+'\r')
ser.flush
print ser.read(int(line[1].rstrip('\r')))
else:
ser.write(str(message)+'\r')
ser.flush()
print ser.read(3)
# Close the connection
ser.close()
if __name__ == '__main__':
simpleserial()