[PySerial] What to think when a "could not open port'/ dev / cu." Error appears during serial communication on the micro: bit

This is a memorandum of remedy when the following error occurs when serial communication is performed to micro: bit using pySerial.

Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/serial/serialposix.py", line 265, in open self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) FileNotFoundError: [Errno 2] No such file or directory: '/dev/cu.usbmodem14302'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "testSerial.py", line 4, in ser = serial.Serial(port,115200,timeout=None) File "/usr/local/lib/python3.8/site-packages/serial/serialutil.py", line 240, in init self.open() File "/usr/local/lib/python3.8/site-packages/serial/serialposix.py", line 268, in open raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg)) serial.serialutil.SerialException: [Errno 2] could not open port /dev/cu.usbmodem14302: [Errno 2] No such file or directory: '/dev/cu.usbmodem14302'

Error opening Arduino serial port

It is possible that the USB port is in use because of the error could not open port. In my case it was ** because I had an Arduino serial monitor open **.

When closed, serial communication was successful.

Finally

It seems that this error can be dealt with from the perspective of ** Isn't it doing something that occupies the serial port, like the Arduino serial monitor?

Python side

testSerial.py


import serial

mac='/dev/cu.usbmodem14302'
ser = serial.Serial(port,115200,timeout = None)
ser.write(b'1')
time.sleep(1)
ser.close()

micro: bit side (JavaScript)

receiveSerial.js


serial.setBaudRate(BaudRate.BaudRate115200)
basic.forever(function () {
    str = serial.readString()
    if (str.indexOf("1") >= 0) {
        basic.showIcon(IconNames.Heart)
    }
})

Recommended Posts

[PySerial] What to think when a "could not open port'/ dev / cu." Error appears during serial communication on the micro: bit
What to do when "Type Error: must be _socket.socket, not socket" appears on GAE
[virtualbox] What to do when [Could not retrieve mirrorlist] appears when yum update is performed on CentOS7
What to do if you get "The session could not be opened" when installing CentOS on VirtualBox
Attempt communication to a specific host / port on Linux (/ dev / tcp / hostname / port)
[Docker] What to do when error Couldn't find the binary git appears
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux