简体   繁体   中英

Arduino and Python communication errors

Background: I am working on a project that is supposed to have an Arduino mega send data from ultra sonic sensors to the computer via USB and I keep getting the same errors. I have checked the device manager and I am sure that the Arduino is on the correct port.

My python errors:

Traceback (most recent call last):
File "----", line 6, in <module>
ser = serial.Serial('COM4', 115200)
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 38, in __init__
SerialBase.__init__(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\serial\serialutil.py", line 282, in __init__
self.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 66, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM4': WindowsError(5, 'Access is denied.')
[Finished in 0.1s]

My Arduino error:

[Stino - Start building "---"...]
Sketch uses 4,922 bytes (1.9%) of program storage space. Maximum is 258,048 bytes.
Global variables use 727 bytes (8.9%) of dynamic memory, leaving 7,469 bytes for local variables. Maximum is 8,196 bytes.
[Stino - Done building "----" in 0.1s.]
[Stino - Start uploading...]
avrdude: ser_open(): can't open device "\\.\COM4": Access is denied.
avrdude: ser_drain(): read error: The handle is invalid.
[Stino - Exit with error code 1.]

I have been working at this for awhile now and I still do not understand how to fix this. Please help.

The error is very basic here. Your arduino COM port is not COM4 . You can see this by the fact you are not even able to upload your program. Inspect the Ports section in your Device Manager to find out which is the right one.
Upd: Or if it is COM4, as suggested in the other answer, it is opened by some other program. It can be another Arduino IDE or your previously ran python program attempts that are still in the memory. Check the task manager in order to find and kill the suspects.

A common cause for this kind of error, if you are sure "COM4" is correct, is that another program currently is using the port and not allowing python to access it. Close any other programs trying to talk to the arduino on COM4 and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM