ndtjxxx 发表于 2016-4-3 18:43:31

ARDUINO在串口通信时分别使用数字0和数字1脚作为RX和TX脚?

本帖最后由 ndtjxxx 于 2016-4-3 18:45 编辑



Duemilanove的串口脚是数字0和数字1,与安卓板进行串口通讯,安卓板的串口是RX TX各占一个引脚,但ARDUINO用下面的代码,接受发送都只用一个数字0脚,怎么让RX 和TX各自使用数字0和数字1脚?

String inputString = "";      
boolean stringComplete = false


void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}


void loop() {
// print the string when a newline arrives:
if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
}
}




void serialEvent() {
while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
}
}

ndtjxxx 发表于 2016-4-3 22:42:01

自己解决了,用SOFTWAREserial

http://www.arduino.cc/en/Tutorial/SoftwareSerialExample


#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
}


Serial.println("Goodnight moon!");

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}

void loop() { // run over and over
if (mySerial.available()) {
    Serial.write(mySerial.read());
}
if (Serial.available()) {
    mySerial.write(Serial.read());
}
}
页: [1]
查看完整版本: ARDUINO在串口通信时分别使用数字0和数字1脚作为RX和TX脚?