2 RX and 2 TX send data from Arduino to Android

2019-08-21 08:53发布

问题:

I have a device connected to pins RX and TX of my Arduino UNO and I need more pins RX and TX to send data via bluetooth to Android. I plan to use two pins SoftwareSerial to convert into RX and TX for use with the Bluetooth module. I suppose it's possible. I have read on the subject but most I've found is to make the connection and send data from Android to Arduino. I think with me it would be possible Amarino library. If so far everything I have planned well, I have a big doubt. How do I pass the data received by the Arduino RX pin of the device that I have connected to the TX pin of the bluetooth to send to Android? I guess I'll have to send from arduino and program it from the IDE. I hope your help, thanks!

回答1:

Sounds like you just want the code to transfer between Serial Ports

void loop() {
...
  while (Serial1.available()) {
    Serial2.write((uint8_t ) Serial1.read());
  }
  while (Serial2.available()) {
    Serial1.write((uint8_t ) Serial2.read());
  }
...

Which simply reads a byte from Serial1 if available and sends it to Serial2 until empty and vice versa. Where Serial1 and Serial2 can be replaced with your SoftwareSerial Objects. Keep in mind High through but could saturate as this example is not limiting. but typical Serial ports should be fine.