2016-01-18 31 views
2

我想通過串行通信控制從Python連接到Arduino的LED。我在Arduino和Python中附加了兩個代碼。但是當我使用Python運行代碼時,我沒有收到來自LED的響應,儘管我沒有收到任何錯誤。也許我在語法上犯了一些錯誤?Arduino和Python

import serial 
import time 
arduino=serial.Serial('COM3',250000,timeout=5.0) 
m=[] 
commands=open('1.txt','r') 
lines=commands.readlines()     
for line in lines:      
    m.append(line) 
commands.close() 
s=0 
while s!=len(m): 
    m[s]=float(m[s]) 
    s+=1 
s=0 

def delay(): 
    x=0 
    y=0 
    while x!=y: 
     x+=1 
while s!=len(m): 
    c=str(m[s]) 
    if m[s]==1: 
     arduino.write(b'c') 
     time.sleep(2) 
     print('1on') 

    elif m[s]==-1: 
     arduino.write(b'c') 
     time.sleep(2) 
     print('1off') 
     delay() 
    elif m[s]==2: 
     arduino.write(b'c') 
     time.sleep(2) 
     print('2on') 

    elif m[s]==-2: 
     arduino.write(b'c') 
     time.sleep(2) 
     print('2off') 

    elif m[s]==3: 
     arduino.write(b'c') 
     time.sleep(2) 
     print('3on') 

    elif m[s]==-3: 
     arduino.write(b'c') 
     time.sleep(2) 
     print('3off') 

    s+=1 

這是在Arduino中控制Python的代碼。 Arduino的代碼如下

int led1=2; 
int led2=3; 
int led3=4; 
void setup() 
{ 
    Serial.begin(250000); 
    pinMode(led1,OUTPUT); 
    pinMode(led2,OUTPUT); 
    pinMode(led3,OUTPUT); 
} 
void loop() 
{ 
    if(Serial.available()) 
    { 
    int v=Serial.parseInt(); 
    if(v==1) 
    { 
     digitalWrite(led1,HIGH); 
     delay(1000); 
    } 
    else if(v==-1) 
    { 
     digitalWrite(led1,LOW); 
     delay(1000); 
    } 
    else if(v==2) 
    { 
     digitalWrite(led2,HIGH); 
     delay(1000); 
    } 
    else if(v==-2) 
    { 
     digitalWrite(led2,LOW); 
     delay(1000); 
    } 
    else if(v==3) 
    { 
     digitalWrite(led3,HIGH); 
     delay(1000); 
    } 
    else if(v==-3) 
    { 
     digitalWrite(led3,LOW); 
     delay(1000); 
    } 
    } 
} 

回答

3

我不知道Python的,但問題似乎是以下幾點:arduino.write(B'C')。你繼續發送「c」字符。它不應該發送任何在c變量?

+0

是啊,應該發送的數據無論在變量c –

2

正如Blurry Sterk所說,您發送字符'c'而不是變量c。除了你的代碼有太多的重複和delay函數什麼都不做

例如,你的Python代碼可能是多simplier這樣的:

import serial 
import time 
arduino=serial.Serial('COM3',250000,timeout=5.0) 
m=[] 
commands=open('1.txt','r') 

lines=commands.readlines()     
for line in lines:      
    m.append(float(line)) #Just convert to float at the moment you read it 

commands.close() 

for c in m: 
    arduino.write(str(c).encode()) 
    time.sleep(2) 
    print(abs(c), 'on' if c>0 else 'off') #First two lines are the same in every if sentence, the last one just prints the number of led (abs(c)) and if it's on or off depending if it's negative or positive 

你的Arduino的代碼可以更易讀太多,如果你使用數組,並使用絕對值訪問數組的索引,類似於E先生指出的python

+0

回溯(最近通話最後一個): 文件「C:/Users/shiva/Desktop/desktopfolders/Python/practice3.py」,14號線在 arduino.write(str(c)) 寫入文件「C:\ Users \ shiva \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ serial \ serialwin32.py」,第286行,寫入數據爲 data = to_bytes(data) 文件「C:\ Users \ shiva \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ serial \ serialutil.py」,第76行,位於to_bytes b.append(item)#這一個處理我們的模擬int和str和Python 3.x的整數 TypeError:一個整數是必需的我是得到這個錯誤。請幫助 –

+0

抱歉,我的不好,[pyserial模塊文檔](http://pyserial.readthedocs.org/en/latest/pyserial_api.html#serial.Serial.write)說'寫'期待['bytearray'](https: //docs.python.org/3/library/functions.html#bytearray)類型發送給arduino。您應該執行'arduino.write(bytearray(str(c)))' –

+0

Traceback(最近調用最後一次): 文件「C:/Users/shiva/Desktop/desktopfolders/Python/practice3.py」,第14行,在 arduino.write(bytearray(str(c))) TypeError:沒有編碼的字符串參數現在這個 –

0

中的邏輯,延遲函數什麼也不做。也許你想這樣:

def delay(y=0): 
if not isinstance(y, int): 
    raise ValueError('y must be integer') 
if y<0: 
    raise ValueError('If y is negative the loop is infinite.') 
x=0 
while x!=y: 
    x+=1 

這實際上,會延遲處理指令。這是實現這個目標的最好方法嗎?我不知道。

+1

延遲通過循環通常是一個壞主意,因爲延遲取決於處理速度。它只適用於非常低級的編程。在python中'time.sleep'通常是你想要的。即使你通過循環延遲,你也必須小心,編譯器/解釋器不會優化它。 – jofel