-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial.py
More file actions
56 lines (41 loc) · 1.21 KB
/
Serial.py
File metadata and controls
56 lines (41 loc) · 1.21 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
### Abril 2024. ###
import serial
import serial.tools.list_ports
import time
puerto = ''
valor = 0
lectura = 1
intentos = 5
def lista_puertos():
lista_puertos = serial.tools.list_ports.comports()
print('Encontrados', len(lista_puertos), 'puertos.')
for i in range(len(lista_puertos)):
print(lista_puertos[i].name)
def conectarse_a_puerto():
try:
global serial_XIAO
serial_XIAO = serial.Serial(puerto, 115200)
time.sleep(1)
except:
print('*** No se pudo conectar a', puerto, '***')
time.sleep(2)
def lee_datos(lectura):
try:
valor = serial_XIAO.readline().decode('ascii')
valor = valor[slice(0,len(valor)-1)]
print(valor)
lectura = 1
except:
print('Error ('+ str(lectura) + ') leyendo puerto ' + puerto, end=' ')
conectarse_a_puerto()
lectura += 1
finally:
return lectura
##################################################################
print('\n\n*** UP THE IRONS! ***\n')
lista_puertos()
puerto = input('Puerto a leer: ')
conectarse_a_puerto()
while lectura < intentos + 1:
lectura = lee_datos(lectura)
time.sleep(1)