-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
44 lines (38 loc) · 1.04 KB
/
utils.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
import os
import time
import serial
class Sender:
def __init__(self, is_show_serial):
self.ser = None
self.is_show_serial = is_show_serial
def openSerial(self, portNum):
try:
if os.name == 'nt':
print('connecting to ' + "COM" + str(portNum))
self.ser = serial.Serial("COM" + str(portNum), 9600)
return True
elif os.name == 'posix':
print('connecting to ' + "/dev/ttyUSB" + str(portNum))
self.ser = serial.Serial("/dev/ttyUSB" + str(portNum), 9600)
return True
else:
print('not supported OS')
return False
except IOError as e:
print('COM Port: can\'t be established')
print(e)
return False
def closeSerial(self):
self.ser.close()
def isOpened(self):
return not self.ser is None and self.ser.isOpen()
def writeRow(self, row):
try:
self.ser.write((row+'\r\n').encode('utf-8'))
except serial.serialutil.SerialException as e:
print(e)
except AttributeError:
print('Attempting to use a port that is not open')
# Show sending serial datas
if self.is_show_serial:
print(row)