File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
build/shared/examples/4.Communication/SerialEvent Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Serial Event example
3
+
4
+ When new serial data arrives, this sketch adds it to a String.
5
+ When a newline is received, the loop prints the string and
6
+ clears it.
7
+
8
+ A good test for this is to try it with a GPS receiver
9
+ that sends out NMEA 0183 sentences.
10
+
11
+ Created 9 May 2011
12
+ by Tom Igoe
13
+
14
+ This example code is in the public domain.
15
+
16
+ http://www.arduino.cc/en/Tutorial/SerialEvent
17
+
18
+ */
19
+
20
+ String inputString = " " ; // a string to hold incoming data
21
+ boolean stringComplete = false ; // whether the string is complete
22
+
23
+ void setup () {
24
+ // initialize serial:
25
+ Serial.begin (9600 );
26
+ // reserve 200 bytes for the inputString:
27
+ inputString.reserve (200 );
28
+ }
29
+
30
+ void loop () {
31
+ // print the string when a newline arrives:
32
+ if (stringComplete) {
33
+ Serial.println (inputString);
34
+ // clear the string:
35
+ inputString = " " ;
36
+ stringComplete = false ;
37
+ }
38
+ }
39
+
40
+ /*
41
+ SerialEvent occurs whenever a new byte comes in the
42
+ hardware serial RX. Don't do complex things here, as thge
43
+ processor halts the regular program to run this routine:
44
+ */
45
+ void serialEvent () {
46
+ // get the new byte:
47
+ char inChar = (char )Serial.read ();
48
+ // add it to the inputString:
49
+ inputString += inChar;
50
+ // if the incoming character is a newline, set a flag
51
+ // so the main loop can do something about it:
52
+ if (inChar == ' \n ' ) {
53
+ stringComplete = true ;
54
+ }
55
+ }
56
+
57
+
You can’t perform that action at this time.
0 commit comments