极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 20588|回复: 1

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

[复制链接]
发表于 2016-4-3 18:43:31 | 显示全部楼层 |阅读模式
本帖最后由 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;
    }
  }
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 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());
  }
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 15:05 , Processed in 0.039907 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表