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'
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.
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?
testSerial.py
import serial
mac='/dev/cu.usbmodem14302'
ser = serial.Serial(port,115200,timeout = None)
ser.write(b'1')
time.sleep(1)
ser.close()
receiveSerial.js
serial.setBaudRate(BaudRate.BaudRate115200)
basic.forever(function () {
str = serial.readString()
if (str.indexOf("1") >= 0) {
basic.showIcon(IconNames.Heart)
}
})
Recommended Posts