-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom.py
More file actions
84 lines (72 loc) · 1.92 KB
/
Copy pathcom.py
File metadata and controls
84 lines (72 loc) · 1.92 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
#coding:utf-8
import serial
import serial.tools.list_ports
#this is modify by any one
class opencom():
def __init__(self):
self.com=serial.Serial()
def initcom(self,comname,bsp=115200,bs=8,s=1,p=serial.PARITY_NONE):
try:
self.com.port = comname
self.com.baudrate = bsp
self.com.bytesize = bs
self.com.stopbits = s
self.com.parity = p
except Exception as e:
print(e)
def isopen(self):
return self.com.isOpen()
def opencom(self):
try:
self.com.open()
except Exception as e:
print(e)
return self.com.isOpen()
def CloseCom(self):
if self.com.isOpen():
self.com.close()
print("串口关闭")
def Get_ports(self):
clist=[]
port_list = list(serial.tools.list_ports.comports())
if len(port_list)> 0:
clist=[]
for e in port_list:
port_list_0 =list(e)
port_serial = port_list_0[0]
clist.append(port_serial)
return clist
def Get_p(self,p):
pstate=serial.PARITY_NONE
if p=="ODD":
pstate=serial.PARITY_ODD
elif p=="EVEN":
pstate=serial.PARITY_EVEN
return pstate
def comwritebytes(self,b):
wlen=self.com.write(b)
return wlen
def comwritestring(self,b):
wlen=self.com.write(b.encode("utf-8"))
return wlen
def HexToString(self,b):
rdata=""
for e in b:
rdata+=hex(e)+" "
rdata=rdata[:-1]
return rdata
def comreadbytes(self):
slen=self.com.in_waiting
sdata=b''
if slen>0:
sdata = self.com.read(slen)
return sdata
"""
c1=opencom()
clist=c1.Get_ports()
if len(clist)>0:
comname=clist[0]
c1.initcom(comname)
if c1.opencom():
c1.CloseCom()
"""