【问题标题】:Python pyserial - windows error (2)Python pyserial - windows报错(二)
【发布时间】:2013-11-02 01:09:47
【问题描述】:

我正在尝试使用 pyserial 库将我的 arduino 连接到 python,但我不断收到错误消息。我连接了我的 arduino 并且 arduino 程序的串行没有运行。我不断收到此错误:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    j = arduino()
  File "C:\Users\Dhruv and Anuj\Documents\Dhruv K. Patel\Python Files\bitSnake_source\bitSnake\glove_input.py", line 9, in __init__
    ser = serial.Serial(port, 9600)
  File "C:\Python32\lib\site-packages\serial\serialwin32.py", line 38, in __init__
    SerialBase.__init__(self, *args, **kwargs)
  File "C:\Python32\lib\site-packages\serial\serialutil.py", line 282, in __init__
    self.open()
  File "C:\Python32\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 'COM1': WindowsError(2, 'The system cannot find the file specified.')

这是我的 arduino 代码:

int speed = 10;

const int thumb = 2;
const int index = 3;
const int middle = 4;
const int ring = 5;
const int pinky = 6;

String out;


void setup(){
  pinMode(thumb,INPUT);
  pinMode(index,INPUT);
  pinMode(middle,INPUT);
  pinMode(ring,INPUT);
  pinMode(pinky,INPUT);
  Serial.begin(9600);
}

void loop(){
  boolean repeat = true;
  if(repeat == true){
    if(digitalRead(thumb) == HIGH){
      Serial.write(1);
      Serial.println(1);
      repeat = false;
    }
    if(digitalRead(index) == HIGH){
      Serial.write(2);
      Serial.println(2);
      repeat = false;
    }
    if(digitalRead(middle) == HIGH){
      Serial.write(3);
      Serial.println(3);
      repeat = false;
    }
    if(digitalRead(ring) == HIGH){
      Serial.write(4);
      Serial.println(4);
      repeat = false;
    }
    if(digitalRead(pinky) == HIGH){
      Serial.write(5);
      Serial.println(5);
      repeat = false;
    }
  }
  if(repeat == true){
    Serial.write(0);
    Serial.println(0);
  }
  delay(10);
}

还有我的python代码:

import serial
from serial import *
port = 0

class arduino:

    def __init__(self):
        #serial.tools.list_ports()
        ser = serial.Serial(port, 9600)
        self.port = str(port)

    def read(self):

        return ser.readline()

    def close(self):

        ser.close()

任何帮助都会很棒,谢谢!

【问题讨论】:

  • 您确定 COM1(即端口 0)是您实际连接的那个吗?

标签: python arduino python-idle pyserial arduino-ide


【解决方案1】:

尝试另一个 COM 端口。你的 arduino 显然不在 COM1.

【讨论】:

  • 谢谢。我几乎可以肯定它在 COM1 上 - 愚蠢的错误 (sigh)
【解决方案2】:

您可以在 Windows 硬件设备管理器中找到使用的 COM。

在 Win7 中,我的 USB 转串口适配器将具有 COM40/COM41。

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2019-05-08
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多