从Arduino到RPi的串行通信通过引脚
问题描述:
我卡住了,不知道为什么。我试图通过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()” 也一样,但没有运气
有什么建议?
答
我发现了我遇到的问题的解决方案。我仍然是一个合适的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