-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_connect.py
75 lines (52 loc) · 1.67 KB
/
serial_connect.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
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 20 14:03:43 2022
@author: ACER
"""
import time
import serial
import serial.tools.list_ports
def find_ports(display=False):
""" A function to find aval. COM Ports """
ports = serial.tools.list_ports.comports()
ports_found = []
for port, desc, hwid in sorted(ports):
if display:
print()
print(f"Port Name : {port}")
print(f"Port Desc : {desc}")
print(f"Port ID : {hwid}")
print()
ports_found.append(port)
if ports_found:
return ports_found
raise TypeError("No Device Error, Empty COM Port!!\n")
def select_port(display=False):
""" Function to choose the COM port """
port_list = find_ports(display)
if port_list:
if len(port_list) == 1:
port = port_list[0]
else:
print(
f"Multiple ports detected, select the desired one\n {port_list} \n")
port = input("Enter the selected port: ")
if port not in port_list:
raise NameError("Terminating, port not found. Try Again!!")
return port
def connect_port(display=False, baud=9600):
""" Function to connect MCU over selected serial COM port """
port = select_port(display)
mcu_connected = serial.Serial(port=port, baudrate=baud)
return mcu_connected
if __name__ == "__main__":
try:
mcu = connect_port()
while 1:
mcu.write('1'.encode())
time.sleep(5)
mcu.write('0'.encode())
time.sleep(5)
except serial.serialutil.SerialException as e:
print(f"The Error: {e}")
# mcu.close()