-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_write_com_linux.py
More file actions
103 lines (98 loc) · 4.33 KB
/
Copy pathread_write_com_linux.py
File metadata and controls
103 lines (98 loc) · 4.33 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
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
import time
import serial
import random
from HexToBin import Hex2Bin
from optparse import OptionParser
usage = '''
usage: %prog [options]
Files needed: com_select(Serial address of the device) (optional, default = /dev/ttyUSB0)
bps(Baud rate of communication) (optional, default = 115200)
string_len(Number of generated sequences) (optional, default = 1000)
flag_bin(Binary file) (optional, default = 1)
file(Record Location) (optional, default = ./test.txt)
ID (ID of user) (optional, default = 19970408)
Type (The methods of STM32) (optional, default = 00)
From_Addr (From Address) (optional, default = 01)
To_Addr (To Address) (optional, default = 02)
Len (The len of package data) (optional, default = 0010)
Data (package data) (optional, default = 0123456789)
err (Error) (optional, default = 1)
'''
parser = OptionParser(usage =usage)
parser.add_option('-c', '--com', dest='com', default='/dev/ttyUSB0',
help='com_select(device port) (optional, default = /dev/ttyUSB0)')
parser.add_option('-b', '--bps', dest='bps', type=int,default=115200,
help='bps(device bps) (optional, default = 115200)')
parser.add_option('-s', '--string', dest='string', type=int, default=1000,
help='string_len(data length) (optional, default = 1000)')
parser.add_option('-f', '--flag', dest='flag', type=int, default=1,
help='flag_bin(bin) (optional, default = 1)')
parser.add_option('-A', '--AddressFile', dest='AddressFile', default='./test.txt',
help='file(file) (optional, default = ./test.txt)')
parser.add_option('-I', '--ID', dest='ID', default='19970408',
help='ID (ID of user) (optional, default = 19970408)')
parser.add_option('-T', '--Type', dest='Type', default='00',
help='Type (The methods of STM32) (optional, default = 00)')
parser.add_option('-F', '--From', dest='From', default='01',
help='From_Addr (From Address) (optional, default = 01)')
parser.add_option('-t', '--to', dest='To', default='02',
help='To_Addr (To Address) (optional, default = 02)')
parser.add_option('-L', '--Len', dest='Len', default='0010',
help='Len (The len of package data) (optional, default = 0010)')
parser.add_option('-D', '--Data', dest='Data', default='0123456789',
help='Data (package data) (optional, default = 0123456789)')
parser.add_option('-E', '--Err', dest='Error', default='1',
help='err (Error) (optional, default = 1)')
options, args = parser.parse_args()
com_select=options.com
bps=options.bps
string_len=options.string
flag_bin=options.flag
file=options.AddressFile
ID=options.ID
Type=options.Type
From_Addr=options.From
To_Addr=options.To
Len=options.Len
Data=options.Data
Err=options.Error
#功能选择控制部分
#输出规定长度散列序列
len_s = 0
temp_str=[]
str_word = 'Z'+ID+Type+From_Addr+To_Addr+Len+Data+Err+'Y'
ser = serial.Serial(com_select,bps)
if(Type=='00'):
with open(file, 'ab+') as f:
while len_s < string_len:
ser.write(str_word.encode())
line = ser.readline()
f.write(line.strip())
if line.strip().decode()=='':
temp_str.append(1)
if len(temp_str)==8:
print(" request timeout!")
break
continue
temp_str=[]
len_s = len_s + 1
print(line.strip())
print(len_s)
ser.close()
if flag_bin == 1:
Hex2Bin(file)
#停止产生随机序列
elif(Type=='01'):
print("Device stopped generating random sequence!")
ser.write(str_word.encode())
ser.close()
#开启产生随机序列
elif(Type=='02'):
print("Device turned on to generate random sequence!")
ser.write(str_word.encode())
ser.close()
#清空循环队列数据
elif (Type == '03'):
print("Device cleared circular queue buffer!")
ser.write(str_word.encode())
ser.close()