2016-06-13 143 views
0

我卡住了,不知道爲什麼。我試圖通過TX和Rx引腳分別從Arduino Nano傳輸到RPi。從Arduino到RPi的串行通信通過引腳

這是我到目前爲止的代碼:

Arduino的TX:

void setup(){ 
    Serial.begin(9600); 
} 

void loop(){ 
    bProgramLoop = 1 
    while(bProgramLoop == 1){ 
    Serial.write(1); 
    } 
} 

樹莓方:

import serial 
oSer = serial.Serial("/dev/ttyAMA0",baudrate=9600,timeout=1) 

while True: 
    sSerialInput = oSer.read(1) 
    #sSerialInput = oSer.readline() 
    print sSerialInput 

裝草圖到阿爾杜伊諾和啓動python腳本,覆盆子後根本不讀任何東西。

注:

  • 我已經經由分壓器和一些跨接線連接與Tx銷從阿爾杜伊諾從RPI的Rx引腳。
  • 我已經試過 「的ReadLine()」 也一樣,但沒有運氣

有什麼建議?

回答

0

我發現了我遇到的問題的解決方案。我仍然是一個合適的noob,但據我瞭解,通過Tx引腳發送串行數據是有問題的,因爲它被usb端口使用,然後到您的PC。

因此,解決方案是使用SoftwareSerial庫。我已經修改了我的兩個代碼草圖如下:

的Arduino:

#include <SoftwareSerial.h> 
SoftwareSerial mySerial(4,3); // (RX,TX) - Pin Setup for any digital pins you want as Rx or Tx 

void setup(){ 
    Serial.begin(9600); 
    mySerial.begin(57600); // initialize serial communication with serial pin 
} 

void loop(){ 
    bProgramLoop = 1 
    while(bProgramLoop == 1){ 
    mySerial.write("Anything"); 
    } 
} 

覆盆子:

import serial 
oSer = serial.Serial("/dev/ttyAMA0",baudrate=9600,timeout=1) 

while True: 
    sSerialInput = oSer.readline() 
    print sSerialInput 

因爲它是我能夠從Arduino的到RPI傳輸數據。 但是,我仍然有問題將字符串轉換爲整數或浮點數。

查看此帖子的詳細信息:Python readline() returns string that wont convert to int or float