python - Arduino 和 Python 通信错误

标签 python c serial-port arduino

背景:我正在从事一个项目,该项目应该让 Arduino mega 通过 USB 将超声波传感器的数据发送到计算机,但我一直遇到同样的错误。我检查了设备管理器,我确信 Arduino 在正确的端口上。

我的 python 错误:

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]

我的 Arduino 错误:

[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.]

我已经为此工作了一段时间,但我仍然不明白如何解决这个问题。请帮忙。

最佳答案

这里的错误是非常基本的。您的 arduino COM 端口不是 COM4。你甚至无法上传你的程序这一事实可以看出这一点。检查设备管理器中的 Ports 部分,找出正确的部分。
更新:或者如果它是 COM4,如其他答案中所建议的,它是由其他程序打开的。它可以是另一个 Arduino IDE 或您之前运行的 python 程序尝试仍在内存中。检查任务管理器以找到并杀死嫌疑人。

关于python - Arduino 和 Python 通信错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28680743/

相关文章:

Python/Pandas 运行总计

c# - 如何在C#中使用 "libtomcrypt"

c - 从串口读取

c - 将浮点变量中的逗号分隔字符串写入串行端口

python - 你能告诉我什么时候可以看到 wx.Grid 单元格 "drag and drop"的一个很好的例子吗?

python - django 是否有像 Rails 有 Liquid 一样安全的用户可编辑模板语言?

c - 从C中的char数组中提取 '('和 ')'内部的字符

c++ - 无符号值之间的减法 - 意外结果

c# - 结合RFID读写器在arduino中接收USB串口数据

python - 在 Python 中使用 BeautifulSoup 解析数据