-
Notifications
You must be signed in to change notification settings - Fork 1
/
pycom.py
executable file
·60 lines (51 loc) · 1.47 KB
/
pycom.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
#!/usr/bin/env python
import random, time, math, os, serial, sys
def read_str():
t = 0
try:
result = ser.readline()
if result.strip() != "":
print(result)
while 0 != cmp(result , "OK\r\n"):
# print t
t=t+1
if 20 == t:
sys.stderr.write("read timeouti1 \r\n");
sys.exit(1);
ser.close()
break;
result = ser.readline()
if result.strip() != "":
print(result)
except serial.serialutil.SerialException as ee:
sys.stderr.write("read fail \r\n");
sys.exit(1);
ser.close()
except AttributeError as aee:
sys.stderr.write("read timeout2 \r\n");
sys.exit(1);
ser.close()
def write_str(str):
print(str)
x = ser.write(str)
def main(port,data,rate):
try:
global ser;
ser = serial.Serial(port,rate,timeout = 2)
except serial.SerialException as e:
sys.stderr.write(port+" not found or Access "+ port +" Permission denied\r\n");
sys.exit(1);
write_str(data+"\r\n")
read_str()
ser.close()
if __name__ == "__main__":
global send_port;
global send_data;
global send_rate;
if 3 != len(sys.argv):
print("param error!\r\nex:./pycom.py at /dev/ttyUSB0 \r\n");
sys.exit(1);
send_data = sys.argv[1];
send_port = sys.argv[2];
send_rate = 115200;
main(send_port,send_data,send_rate);