root / arduino-1.0 / libraries / SoftwareSerial / examples / TwoPortReceive / TwoPortReceive.ino @ 58d82c77
History | View | Annotate | Download (1.96 KB)
1 | 58d82c77 | Tom Mullins | /* |
---|---|---|---|
2 | Software serial multple serial test |
||
3 | |||
4 | Receives from the two software serial ports, |
||
5 | sends to the hardware serial port. |
||
6 | |||
7 | In order to listen on a software port, you call port.listen(). |
||
8 | When using two software serial ports, you have to switch ports |
||
9 | by listen()ing on each one in turn. Pick a logical time to switch |
||
10 | ports, like the end of an expected transmission, or when the |
||
11 | buffer is empty. This example switches ports when there is nothing |
||
12 | more to read from a port |
||
13 | |||
14 | The circuit: |
||
15 | Two devices which communicate serially are needed. |
||
16 | * First serial device's TX attached to digital pin 2, RX to pin 3 |
||
17 | * Second serial device's TX attached to digital pin 4, RX to pin 5 |
||
18 | |||
19 | created 18 Apr. 2011 |
||
20 | by Tom Igoe |
||
21 | based on Mikal Hart's twoPortRXExample |
||
22 | |||
23 | This example code is in the public domain. |
||
24 | |||
25 | */ |
||
26 | |||
27 | #include <SoftwareSerial.h> |
||
28 | // software serial #1: TX = digital pin 2, RX = digital pin 3 |
||
29 | SoftwareSerial portOne(2, 3); |
||
30 | |||
31 | // software serial #2: TX = digital pin 4, RX = digital pin 5 |
||
32 | SoftwareSerial portTwo(4, 5); |
||
33 | |||
34 | void setup() |
||
35 | { |
||
36 | // Start the hardware serial port |
||
37 | Serial.begin(9600); |
||
38 | |||
39 | // Start each software serial port |
||
40 | portOne.begin(9600); |
||
41 | portTwo.begin(9600); |
||
42 | } |
||
43 | |||
44 | void loop() |
||
45 | { |
||
46 | // By default, the last intialized port is listening. |
||
47 | // when you want to listen on a port, explicitly select it: |
||
48 | portOne.listen(); |
||
49 | Serial.println("Data from port one:"); |
||
50 | // while there is data coming in, read it |
||
51 | // and send to the hardware serial port: |
||
52 | while (portOne.available() > 0) { |
||
53 | char inByte = portOne.read(); |
||
54 | Serial.write(inByte); |
||
55 | } |
||
56 | |||
57 | // blank line to separate data from the two ports: |
||
58 | Serial.println(); |
||
59 | |||
60 | // Now listen on the second port |
||
61 | portTwo.listen(); |
||
62 | // while there is data coming in, read it |
||
63 | // and send to the hardware serial port: |
||
64 | Serial.println("Data from port two:"); |
||
65 | while (portTwo.available() > 0) { |
||
66 | char inByte = portTwo.read(); |
||
67 | Serial.write(inByte); |
||
68 | } |
||
69 | |||
70 | // blank line to separate data from the two ports: |
||
71 | Serial.println(); |
||
72 | } |
||
73 | |||
74 | |||
75 | |||
76 | |||
77 |