-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialFlooder.py
172 lines (139 loc) · 5.03 KB
/
serialFlooder.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
## serialFlooder.py
## This program is intended to write as many characters as possbile to serial port
## like it's flooding it.
import os
import sys
import serial
import argparse
import os.path
import json
# custom imports
import serialEnu
def CheckExt(choices):
class Act(argparse.Action):
def __call__(self,parser,namespace,fname,option_string=None):
ext = os.path.splitext(fname)[1][1:]
if ext not in choices:
option_string = '({})'.format(option_string) if option_string else ''
parser.error("configfile doesn't end with one of {}{}".format(choices,option_string))
else:
setattr(namespace,self.dest,fname)
return Act
def loadconfig(argom):
print('\n\n...Sono dentro a loadconfig...\n')
cnf = getattr(argom, 'config', None)
file = getattr(argom, 'configfile', None)
if cnf:
# assume that on index[2] of argv there will be the settings.py file
#try:
print('\n\nFile: ' + file)
with open(file, 'r') as f:
sett = json.load(f)
print('\nsett is: ' + str(type(sett)))
for elem in sett:
print(elem)
# f = open(file, 'r')
# a = list(f.readlines())
# f.close()
# print('\na is: ' + str(type(a)))
# for elem in a:
# print(elem)
#exec("import %s" % ext)
#except ImportError:
# print('ERROR: Wrong config file!\nFile should be in python format!\n\n')
else:
pass
return sett
def configure(argom):
# this function enumerates serial devices to let the user choose the correct one
cnf = getattr(argom, 'config', None)
file = getattr(argom, 'configfile', None)
if args.setup:
serialEnu.SerialMyEnumerator()
else:
return
com = input('\nNow, please, insert che correct device name for the serial port to use: ')
print('\nSerial speed:\n1) 9600\n2) 14400\n3) 19200\n4) 28800\n5) 38400\n6) 57600\n7) 115200')
vel = input('\nSelect a speed (1,2,3,4,5,6 or 7): ')
databit = input('\nEnter data bits (5, 6, 7 or 8): ')
parit = input('\nEnter parity (E)ven, (O)dd, (N)one, (M)ark, (S)pace: ')
stopb = input('\nStopbits\n1) 1 stopbit\n2) 1,5 stopbits\n3) 2 stopbits\n Enter how many stopbits you want: ')
flowctrl = input('\nDo you want software flow control Xon/Xoff (Y/N)?: ')
flowctrl.upper()
sett = {'comport':com}
sett['speed'] = vel
sett['databit'] = databit
sett['parity'] = parit
sett['stopbit'] = stopb
sett['flowctrl'] = flowctrl
with open(file, 'w') as cj:
json.dump(sett, cj)
# cf = open('settings.py', 'w', -1, 'utf-8')
# cf.write('com = \'%s' % com + '\'\n')
# cf.write('vel = \'%s' % vel + '\'\n')
# cf.write('databit = \'%s' % databit + '\'\n')
# cf.write('parit = \'%s' % parit + '\'\n')
# cf.write('stopb = \'%s' % stopb + '\'\n')
# cf.write('flowctrl = \'%s' % flowctrl + '\'\n')
#
# cf.flush()
# cf.close()
print('\n\nConfiguration file created!\nNow call program with "-c ' + file + '" as parameter.\n\n')
return
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-c', '--config', help='Load config from <configfile>', action='store_true')
parser.add_argument('configfile', help='Configuration file', type=str, action=CheckExt({'json'}))
group.add_argument('-s', '--setup', help='Generate config file', action='store_true')
args = parser.parse_args()
print(type(args))
if args.config:
conf = loadconfig(args)
print (conf)
print(str(type(conf)))
print('################\n')
for element in conf:
print(element, conf[element])
exec("%s = \'%s\'" % (element, conf[element]))
print('!!!!!!!!!!!!!!!!!\n')
print("comport = \'%s\'" % comport)
print("speed = \'%s\'" % speed)
print("databit = \'%s\'" % databit)
print("parity = \'%s\'" % parity)
print("stopbit = \'%s\'" % stopbit)
print("flowctrl = \'%s\'" % flowctrl)
if args.setup:
configure(args)
exit(0)
"""
print('com = \'%s' % com + '\'\n')
print('vel = \'%s' % vel + '\'\n')
print('databit = \'%s' % databit + '\'\n')
print('parit = \'%s' % parit + '\'\n')
print('stopb = \'%s' % stopb + '\'\n')
print('flowctrl = \'%s' % flowctrl + '\'\n')
"""
# this is the main
ser = serial.Serial()
ser.port = comport
ser.baudrate = 115200 #int(speed)
ser.bytesize = serial.EIGHTBITS #(databit)
ser.parity = serial.PARITY_NONE #parity
ser.stopbits = 1 #int(stopbit)
ser.xonxoff = True
print('\nIs serial port opened? ',ser.isOpen())
ser.open()
print('\nIs serial port opened? ',ser.isOpen())
"""
ser = serial.Serial('/dev/serial0', 115200, serial.EIGHTBITS, serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,xonxoff=True)
"""
while True:
try:
for i in range(33,126):
print(chr(i))
ser.write(chr(i).encode('utf-8'))
ser.write(b'\r\n')
except KeyboardInterrupt:
print('Pressed an interruption key (Ctrl+C)... Stoppping serial communication!')
break
ser.close()