-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATS_SERIAL.py
More file actions
205 lines (183 loc) · 7.38 KB
/
ATS_SERIAL.py
File metadata and controls
205 lines (183 loc) · 7.38 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import serial.tools.list_ports
import MySQLdb
import threading
import time
import os
global db_host
global usuario
global clave
global base_de_datos
global queryList
queryList = []
global mysqlDefFlag
mysqlDefFlag = []
db_host = "hmiatsd.byethost31.com"
usuario = "hmiatsdb_ats"
clave = "m3c464r0n"
base_de_datos = "hmiatsdb_ats_prueba"
def serConnect():
print("Conectando puerto serie...")
while True:
try:
ser = serial.Serial('/dev/serial/by-id/usb-FTDI_USB__-__Serial-if00-port0', baudrate=38400, bytesize=serial.EIGHTBITS, stopbits = serial.STOPBITS_ONE, parity = serial.PARITY_EVEN, timeout=None, rtscts=0, write_timeout=5, dsrdtr=0, inter_byte_timeout=None , xonxoff=True)
# ser = serial.Serial('/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0', baudrate=38400, bytesize=serial.EIGHTBITS, stopbits = serial.STOPBITS_ONE, parity = serial.PARITY_EVEN, timeout=None, rtscts=0, write_timeout=5, dsrdtr=0, inter_byte_timeout=None , xonxoff=True)
ser.flushInput()
ser.flushOutput()
readLoop(ser)
break
except:
None
def readLoop(ser):
time.sleep(4)
linea = b''
iLinea = False
firstLine = True
lenLinea = 152
lCompleta = False
cntCRC = 0
lenCRC = 2
CRC = b''
crcCalc = b''
print(ser.get_settings())
ser.flush()
ser.write(b'\x01\x52')
ser.write(b'\x05')
while True:
try:
if ser.in_waiting > 0:
dato = ser.read()
for posBit in range (1, len(dato)+1):
if lCompleta == False:
if iLinea == False:
if dato[posBit-1:posBit] == b'\x02':
iLinea = True
linea = b''
cscCalc = b''
else:
if dato[posBit-1:posBit] == b'\x02':
linea = b''
csrCalc = b''
elif dato[posBit-1:posBit] != b'\x03':
linea += dato[posBit-1:posBit]
if crcCalc == b'':
crcCalc = str(dato[0][posBit-1:posBit].hex()).encode('ascii')
else:
crcCalc = xor(crcCalc, str(dato[0][posBit-1:posBit].hex()).encode('ascii'))
else:
if len(linea) == lenLinea:
lCompleta = True
else:
cntCRC += 1
CRC += dato[0][posBit-1:posBit]
if cntCRC == lenCRC:
linea += CRC
if str(CRC)[2:4] == str(crcCalc.hex()[1:2].upper() + crcCalc.hex()[3:4].upper()):
mi_query = "(CURRENT_DATE,CURRENT_TIME, x'" + str(linea)[2:len(str(linea))-1] + "')"
queryList.append(mi_query)
if len(mysqlDefFlag) == 0:
mysqlThread = threading.Thread(target=mysqlDef, daemon = True)
mysqlThread.start()
linea = b''
iLinea = False
lCompleta = False
cntCRC = 0
CRC = b''
crcCalc = b''
ser.write(b'\x05')
except:
mi_query = "(CURRENT_DATE,CURRENT_TIME, x'FFFFFFFFFFFFFF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')"
queryList.append(mi_query)
if len(mysqlDefFlag) == 0:
mysqlThread = threading.Thread(target=mysqlDef, daemon = True)
mysqlThread.start()
print ("Puerto serie desconectado")
serConnect()
def xor(ba1, ba2):
return bytes([_a ^ _b for _a, _b in zip(ba1, ba2)])
def mysqlDef():
print ("\n mysqlDef: Running \n")
mysqlDefFlag.append(1)
while True:
try:
db = MySQLdb.connect(host=db_host, user=usuario, passwd=clave, db=base_de_datos)
cursor = db.cursor()
break
except:
None
maxLenQuery = 10000
while len(queryList) > 0:
try:
long = len(queryList)
if long > maxLenQuery:
long = maxLenQuery
query = "INSERT INTO `datos_bats`(`dia_serv`, `hora_serv`, `cadena`) VALUES "
smp = False
for i in range(0, long):
if smp == False:
smp = True
query += queryList[0]
else:
query += "," + queryList[0]
queryList.pop(0)
cursor.execute(query)
print (query)
except:
while True:
try:
db = MySQLdb.connect(host=db_host, user=usuario, passwd=clave, db=base_de_datos)
cursor = db.cursor()
break
except:
None
try:
db.commit()
cursor.close()
except:
None
mysqlDefFlag.clear()
print ("\n mysqlDef: Closed \n")
def iAmAliveDef():
print ("\n iAmAliveDef: Running \n")
while True:
try:
db = MySQLdb.connect(host=db_host, user=usuario, passwd=clave, db=base_de_datos)
cursor = db.cursor()
#INFORMO: LINEA, NUMERO TREN, CHAPA TREN, MODELO, VERSION DE FIRMWARE,
query = "UPDATE `datos_tr` SET `linea` = 'PRUEBA', `num_tren` = '0', `chapa_tren` = 'M0', `modelo` = 'PRUEBA', `version_fw` = '1.2.0' WHERE 1"
cursor.execute(query)
break
except:
None
salir = False
while (salir == False):
time.sleep(60)
try:
#ACTUALIZO LA HORA PARA INDICAR QUE EL MODULO ESTA VIVO
query = "UPDATE `datos_tr` SET `hora_serv` = CURRENT_TIME, `dia_serv` = CURRENT_DATE WHERE 1"
cursor.execute(query)
#ME FIJO EL ESTADO DE COMANDO PARA VER SI DEBO EJECUTAR UN COMANDO BASH
query = "SELECT `comando` FROM `datos_tr` WHERE 1"
cursor.execute(query)
result = cursor.fetchall()
if result[0][0] == 1:
query = "UPDATE `datos_tr` SET `comando` = 8 WHERE 1"
cursor.execute(query)
break
except:
while True:
try:
db = MySQLdb.connect(host=db_host, user=usuario, passwd=clave, db=base_de_datos)
cursor = db.cursor()
break
except:
None
try:
db.commit()
cursor.close()
print ("\n iAmAliveDef: Closed \n")
os.system('reboot')
except:
None
iAmAliveThread = threading.Thread(target=iAmAliveDef, daemon = True)
iAmAliveThread.start()
serConnect()