-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathespModule.py
More file actions
76 lines (65 loc) · 2.39 KB
/
espModule.py
File metadata and controls
76 lines (65 loc) · 2.39 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
import serial
import time
class SerialParsedData:
def __init__(self, command, data):
self.command = command
self.data = data
port = "COM3"
baudrate = 9600
timeout = 5
loopDuration = 5
commandDuration = 2
global isSerialOpen
isSerialOpen = False
global globalSerial
def initialize():
global globalSerial
global isSerialOpen
while (isSerialOpen == False):
try:
globalSerial = serial.Serial(port=port, baudrate=baudrate, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, xonxoff=False, rtscts=False, dsrdtr=False, timeout=timeout)
isSerialOpen = True
except:
print("***********************************")
print('Port Acilamadi. Tekrar deniyorum....')
print("***********************************")
time.sleep(commandDuration)
def openSerial():
global globalSerial
if(globalSerial.is_open == False):
globalSerial = serial.Serial(port=port, baudrate=baudrate, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, xonxoff=False, rtscts=False, dsrdtr=False, timeout=timeout)
def closeSerial():
global globalSerial
global isSerialOpen
isSerialOpen = False
globalSerial.reset_input_buffer()
globalSerial.reset_output_buffer()
globalSerial.close()
def parseIncommingData(incommingData):
strArray = incommingData.replace('\n', '').replace('\r', '').split(",")
command = strArray[0]
del strArray[0]
data = SerialParsedData(command, strArray)
return data
def sendCommand(command):
global globalSerial
command = command + ","
print("+ command: " + command + " will send")
openSerial()
globalSerial.write(command.encode('utf-8'))
incommingData = globalSerial.readline()
print("- DATA: " + str(incommingData).upper())
print("---------------------------------")
time.sleep(commandDuration)
def sendCommandWithHandler(command, handler):
global globalSerial
command = command + ","
print("+ command (Handler): " + command + " will send")
openSerial()
globalSerial.write(command.encode('utf-8'))
incommingData = globalSerial.readline()
print("- DATA (Handler): " + str(incommingData).upper())
parsedData = parseIncommingData(incommingData.decode())
handler(parsedData)
print("---------------------------------")
time.sleep(commandDuration)