-
Notifications
You must be signed in to change notification settings - Fork 0
/
COM.py
78 lines (60 loc) · 1.67 KB
/
COM.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
import serial
import subprocess
class MySerial:
def __init__(self):
p = subprocess.check_output("python -m serial.tools.list_ports")
ports = p.replace("\n", " ").split()
for i in range(len(ports)):
print("port " + str(i) + " : " + ports[i])
port_n = input('Input port number: ')
print("Your select : " + str(ports[int(port_n)]))
self.port = ports[int(port_n)]
self.ser = serial.Serial(
port=self.port,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=None)
def open(self):
if self.ser.isOpen():
pass
elif not self.ser.isOpen():
self.ser.open()
def close(self):
self.ser.close()
def read(self):
self.open()
while True:
result = ''
while self.ser.inWaiting() == 0:
pass
result = self.ser.readline()
if result != None:
return result
def write(self, data):
try:
self.ser.open()
except:
pass
try:
self.ser.write(data)
except:
return(False)
self.ser.close()
return(True)
'''
while ser.inWaiting() == 0:
pass
#необходимая работа с данными...'''
class MyDevises:
def __init__(self, name, type):
self.name = name
self.serial = MySerial()
self.type = type
def testing(self):
self.serial.write('test')
def turn_on(self):
pass
def turn_off(self):
pass