-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.py
133 lines (114 loc) · 3.99 KB
/
serial.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
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
import serial
import smtplib, ssl
# Configuracion del puerto serial
com_serial = serial.Serial('/dev/ttyUSB0', timeout=1, baudrate=115200, bytesize=8, parity='N', stopbits=1, xonxoff=False, rtscts=False, dsrdtr=False)
# Declaracion de listas
status = []
statusraw = []
statisticsraw = []
statistics = []
statdata=[]
# Declaracion de comandos
CMD_PC_CTRL_START = [0x02,0x02,0x01,0x01,0x03,0xFB]
CMD_PC_CTRL_STOP = [0x02,0x01,0x02,0x03,0xFC]
CMD_PC_CTRL_STATISTICS = [0x02,0x02,0x20,0x00,0x03,0xDD]
# Declarcion de listas para interpretar los datos
BosilloR = ['Libre','Ocupado']
Puerta = ['Cerrada','Abierta']
Modos = ['Mezcla','Denominacion','Cuentanotas','Cara','Orientacion','Issue','Serial','Separate','Barcode','Barcode + Efectivo','','Dissue']
Lote = ['100','50','25','20','10','Batch Apagado','Batch por numero personalizado','Batch por monto']
#Send CMD_PC_CTRL_STOP
com_serial.write(serial.to_bytes(CMD_PC_CTRL_STOP))
#Send CMD_PC_CTRL_START
com_serial.write(serial.to_bytes(CMD_PC_CTRL_START))
# Lectura de datos recibidos DEVICE STATUS y SENSOR STATUS
read_byte = com_serial.read()
statusraw.append(read_byte)
status.append(int.from_bytes(read_byte, "big"))
while read_byte is not None:
read_byte = com_serial.read()
if read_byte == b'':
read_byte = None
break
statusraw.append(read_byte)
status.append(int.from_bytes(read_byte, "big"))
# Separacion de los resultados leidos en DEVICE y SENSOR
l = len(statusraw)
length1 = status[1]
device = status[0:length1+4]
sensor = status[length1+4:l]
# Envio de solicitud de STATISTICS
com_serial.write(serial.to_bytes(CMD_PC_CTRL_STATISTICS))
# Lectura de datos recibidos STATISTICS
read_byte = com_serial.read()
statistics.append(int.from_bytes(read_byte, "big"))
statisticsraw.append(read_byte)
while read_byte is not None:
read_byte = com_serial.read()
if read_byte == b'':
read_byte = None
break
statisticsraw.append(read_byte)
statistics.append(int.from_bytes(read_byte, "big"))
# Envio solicitud CMD_PC_CTRL_STOP
com_serial.write(serial.to_bytes(CMD_PC_CTRL_STOP))
# Tratamiento de datos DEVICE STATUS
ndata = device[1]-1
actualcurr = device[3]
dataindex = device[4]
modeindex = device[5]
batchindex = device[6]
batchn1 = device[7]
batchn2 = device[8]
currn = device[9]
currencies = []
i = 10
for j in range(currn):
currencies.append('')
for k in range(3):
currencies[j] = currencies[j]+chr(device[10+j*3+k])
i = i + 1
ndenom = []
for j in range(currn-1):
ndenom.append(device[i])
i = i + 1
cant_batch = []
for j in range(ndata-i+3):
cant_batch.append(device[i])
i = i + 1
# Tratamiento datos SENSOR STATUS
sensores = [0,0,0,0,0,0,0,0]
sensorstatus = sensor[3]
i=0
while sensorstatus // 2 != 0:
sensores[i]=sensorstatus % 2
sensorstatus = sensorstatus // 2
i = i + 1
if sensorstatus == 1:
sensores[i]=sensorstatus % 2
# Tratamiento datos STATISTICS
statdata = statistics[3:statistics[1]+2]
totalcount = (statdata[0]<<24 | statdata[1]<<16 | statdata[2]<<8 | statdata[3])
# Envio notificacion correo
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "" # Enter your address
receiver_email = "" # Enter receiver address
password = ""
message = """\
Subject: "Notificacion CBM"
Total de billetes contados: """+str(totalcount)
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
print ('Moneda Actual:' + currencies[actualcurr])
print ('Modo de conteo:' + Modos[modeindex])
print ('Modo de batch:' + Lote[batchindex])
# print (totalcount)
# print ('Cantidad de monedas instaladas: ' + str(currn-2))
# print ('Estado de bolsillo de rechazo: ' + BosilloR[sensores[7]])
# print ('Estado de bandeja de entrada: ' + BosilloR[sensores[5]])
# print ('Estado de apilador: ' + BosilloR[sensores[6]])
# print ('Estado de puerta superior: ' + Puerta[sensores[0]])
# print ('Estado de inferior: ' + Puerta[sensores[2]])