PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.5KB

  1. /*
  2. Software serial multple serial test
  3. Receives from the two software serial ports,
  4. sends to the hardware serial port.
  5. In order to listen on a software port, you call port.listen().
  6. When using two software serial ports, you have to switch ports
  7. by listen()ing on each one in turn. Pick a logical time to switch
  8. ports, like the end of an expected transmission, or when the
  9. buffer is empty. This example switches ports when there is nothing
  10. more to read from a port
  11. The circuit:
  12. Two devices which communicate serially are needed.
  13. * First serial device's TX attached to digital pin 2, RX to pin 3
  14. * Second serial device's TX attached to digital pin 4, RX to pin 5
  15. Note:
  16. Not all pins on the Mega and Mega 2560 support change interrupts,
  17. so only the following can be used for RX:
  18. 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
  19. Not all pins on the Leonardo support change interrupts,
  20. so only the following can be used for RX:
  21. 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
  22. created 18 Apr. 2011
  23. modified 25 May 2012
  24. by Tom Igoe
  25. based on Mikal Hart's twoPortRXExample
  26. This example code is in the public domain.
  27. */
  28. #include <SoftwareSerial.h>
  29. // software serial #1: TX = digital pin 10, RX = digital pin 11
  30. SoftwareSerial portOne(10,11);
  31. // software serial #2: TX = digital pin 8, RX = digital pin 9
  32. // on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
  33. SoftwareSerial portTwo(8,9);
  34. void setup()
  35. {
  36. // Open serial communications and wait for port to open:
  37. Serial.begin(9600);
  38. while (!Serial) {
  39. ; // wait for serial port to connect. Needed for Leonardo only
  40. }
  41. // Start each software serial port
  42. portOne.begin(9600);
  43. portTwo.begin(9600);
  44. }
  45. void loop()
  46. {
  47. // By default, the last intialized port is listening.
  48. // when you want to listen on a port, explicitly select it:
  49. portOne.listen();
  50. Serial.println("Data from port one:");
  51. // while there is data coming in, read it
  52. // and send to the hardware serial port:
  53. while (portOne.available() > 0) {
  54. char inByte = portOne.read();
  55. Serial.write(inByte);
  56. }
  57. // blank line to separate data from the two ports:
  58. Serial.println();
  59. // Now listen on the second port
  60. portTwo.listen();
  61. // while there is data coming in, read it
  62. // and send to the hardware serial port:
  63. Serial.println("Data from port two:");
  64. while (portTwo.available() > 0) {
  65. char inByte = portTwo.read();
  66. Serial.write(inByte);
  67. }
  68. // blank line to separate data from the two ports:
  69. Serial.println();
  70. }