2013-11-02 34 views
2

我想使用pyserial庫來連接我的arduino到Python,但我不斷收到錯誤。我有我的arduino連接,並且arduino程序的序列沒有運行。我不斷收到此錯誤:Python pyserial - Windows錯誤(2)

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() 

任何幫助將是巨大的,謝謝!

+0

你確定COM1(又名端口0)是你實際連接到的嗎? – abarnert

回答

2

嘗試另一個COM端口。你的arduino顯然不在COM1上。

+0

謝謝。我幾乎可以肯定它是在COM1上 - 愚蠢的錯誤(*嘆*) –

0

你可以找出在Windows硬件設備管理器中使用哪個COM。

在Win7中,我的USB轉串口適配器將具有COM40/COM41。