2014-01-16 75 views
0

嗨,我有下面的代碼,並不斷收到此錯誤信息:名稱錯誤的Arduino

NameError:名字「系列」是沒有定義

我抄這個代碼關閉許多教程它不會工作有人可以告訴我爲什麼?

from serial import* 
ser = serial.Serial('/dev/ttyACM0', 9600) 

while 1: 
    ser.readline() 

回答

0

你的第一行必須是:

from serial import * 

你需要import*之間的空間。其次,當你以這種方式導入時,你不需要預先加入serial。它應該是:

ser = Serial('/dev/ttyACM0', 9600) 
1

當你以這種方式導入:

from serial import * 

要導入的事情,好像他們是實際的文件中聲明。從Python的文檔:

This imports all names except those beginning with an underscore (_). Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

所以,你可以撥打Serial,如:

ser = Serial('/dev/ttyACM0', 9600) 

如果你不想導入這種方式(你可能有問題覆蓋領域或已經使用的標識符)你可以使用:

import serial 

ser = serial.Serial('/dev/ttyACM0', 9600) 
+0

我試過了你說的但仍然有錯誤說串口沒有定義?我已經注意到它沒有任何改變顏色,所以我我當前的代碼是這樣的: 進口串行 SER =序列( '的/ dev/ttyACM0',9600) 而1: ser.readline() – user2996828

+0

我認爲你誤解了,一種方法是從串行導入*和串行=(...)'。另一種方式是'進口串口'和'ser = serial.Serial(...)' – Christian

+0

嘗試過這兩種方法,我仍然收到錯誤,是否有一種簡單的方法來檢查我的串口是否安裝正確? – user2996828

0

你需要安裝pySerial。如果你不這樣,試試

sudo apt-get install python-serial