import serial
from serial.tools import list_ports
from serial import Serial
port = list(list_ports.comports())
for p in port:
print(p.device)
ser = serial.Serial(
# Serial Port to read the data from
port='/dev/cu.usbserial-110',baudrate = 115200,
#Applying Parity Checking (none in this case)
parity=serial.PARITY_NONE,
# Pattern of Bits to be read
stopbits=serial.STOPBITS_ONE,
# Total number of bits to be read
bytesize=serial.EIGHTBITS,
# Number of serial commands to accept before timing out
timeout=1)
# Pause the program for 1 second to avoid overworking the serial port
while 1:
x=ser.readline()
print(x)
La salida:
/dev/cu.wlan-debug
/dev/cu.OnePlusBudsZ
/dev/cu.Bluetooth-Incoming-Port
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
File /opt/homebrew/Caskroom/miniforge/base/envs/py3/lib/python3.10/site-packages/serial/serialposix.py:322, in Serial.open(self)
321 try:
--> 322 self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
323 except OSError as msg:
FileNotFoundError: [Errno 2] No such file or directory: '/dev/cu.usbserial-110'
During handling of the above exception, another exception occurred:
SerialException Traceback (most recent call last)
Input In [34], in <cell line: 1>()
----> 1 ser = serial.Serial(
2 # Serial Port to read the data from
3 port='/dev/cu.usbserial-110',baudrate = 115200,
4 #Applying Parity Checking (none in this case)
5 parity=serial.PARITY_NONE,
6 # Pattern of Bits to be read
7 stopbits=serial.STOPBITS_ONE,
8 # Total number of bits to be read
9 bytesize=serial.EIGHTBITS,
10 # Number of serial commands to accept before timing out
11 timeout=1)
12 # Pause the program for 1 second to avoid overworking the serial port
13 while 1:
File /opt/homebrew/Caskroom/miniforge/base/envs/py3/lib/python3.10/site-packages/serial/serialutil.py:244, in SerialBase.__init__(self, port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, write_timeout, dsrdtr, inter_byte_timeout, exclusive, **kwargs)
241 raise ValueError('unexpected keyword arguments: {!r}'.format(kwargs))
243 if port is not None:
--> 244 self.open()
File /opt/homebrew/Caskroom/miniforge/base/envs/py3/lib/python3.10/site-packages/serial/serialposix.py:325, in Serial.open(self)
323 except OSError as msg:
324 self.fd = None
--> 325 raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
326 #~ fcntl.fcntl(self.fd, fcntl.F_SETFL, 0) # set blocking
328 self.pipe_abort_read_r, self.pipe_abort_read_w = None, None
SerialException: [Errno 2] could not open port /dev/cu.usbserial-110: [Errno 2] No such file or directory: '/dev/cu.usbserial-110'
Quería acceder a una cámara web a través del puerto serie usando Python.
Anteriormente, había hecho esto y la lista de puertos mostraba esto /dev/cu.usbserial-110
como uno de los puertos. Ahora, cuando vuelvo a ejecutar el programa después de un mes, no puedo encontrar este puerto y muestra FileNotFoundError.
Soy un principiante en Python y necesito su ayuda.