1+ // Constants
2+ #define BAUD_RATE 9600
3+
4+ // Variables
5+ String receivedData = " " ;
6+ unsigned long lastReceiveTime = 0 ;
7+ const unsigned long DISPLAY_TIMEOUT = 500 ; // ms
8+
9+ void setup () {
10+ Serial.begin (BAUD_RATE); // Initialize serial communication with computer
11+ Serial1.begin (BAUD_RATE); // Initialize serial communication with chip
12+
13+ Serial.println (" Chip Serial Monitor Started" );
14+ Serial.println (" Waiting for data..." );
15+ }
16+
17+ void loop () {
18+ // Read data from the chip
19+ while (Serial1.available () > 0 ) {
20+ char inByte = Serial1.read ();
21+ receivedData += inByte;
22+ lastReceiveTime = millis ();
23+ }
24+
25+ // Print complete messages after timeout
26+ if (receivedData.length () > 0 && (millis () - lastReceiveTime > DISPLAY_TIMEOUT)) {
27+ Serial.print (" Received: " );
28+
29+ // Print as ASCII
30+ Serial.print (" ASCII: \" " );
31+ Serial.print (receivedData);
32+ Serial.print (" \" | HEX: " );
33+
34+ // Print as HEX
35+ for (int i = 0 ; i < receivedData.length (); i++) {
36+ char c = receivedData.charAt (i);
37+ if (c < 0x10 ) Serial.print (" 0" );
38+ Serial.print (c, HEX);
39+ Serial.print (" " );
40+ }
41+
42+ Serial.println ();
43+ receivedData = " " ;
44+ }
45+
46+ // Send data to chip if entered in Serial Monitor
47+ if (Serial.available () > 0 ) {
48+ String input = Serial.readStringUntil (' \n ' );
49+ Serial1.println (input);
50+ Serial.println (" Sent: " + input);
51+ }
52+ }
0 commit comments