Respected folks,
I can’t get rid of ‘gibberish characters’ when monitoring “Serial1” and printing to “Serial” (Serial1 buffer to USB Serial monitor, no wires hooked to Tx, Rx pins of Serial1) … I’ve read nearly all the guides (“serial tutorial”, troubleshooting posts like this, general C++ syntax, and even the Arduino forum!).
I’m simply trying to get 2 Photons talk to each other before more ambitious plans. Any pointers would be much appreciated, especially for a confidence boost!
MY CODE:
(On transmitting Photon, could be receiver as well, device firmware = 0.7.0)
#include "Particle.h"
// Constants
const size_t READ_BUF_SIZE = 64;
const unsigned long SEND_INTERVAL_MS = 2000;
// Forward declarations
// Put function definitions here
// Global variables
char readBuf[READ_BUF_SIZE];
size_t readBufOffset = 0;
unsigned long lastSend = 0;
int counter = 0; // Can pre or post-increment
char c = '\0'; // This is 1 char, denoted by ''
String s[] = "Test"; // char s[] is a char array, String is an object; both have ""
int stat = 0;
int stat2 = 0;
unsigned long start_time = 0;
unsigned long before_flush = 0;
unsigned long after_flush = 0;
void setup() {
Serial.begin(9600); // Starts USB serial
Serial1.begin(9600, SERIAL_7E1); // Starts TTL pins TX RX
}
void serialEvent1() { // Should run separately, but holds until loop
while (Serial1.available() > 0) {
c = Serial1.read();
readBuf[readBufOffset] = c;
readBufOffset++;
if (c == '\0') { // If character is nulll
if (readBuf == '\0') { // Compares to pointer, the 1st char in array
Serial.printlnf("Buf blank");
}
else {
Serial.printlnf("Buffer: %s", readBuf); // Presuming something in buffer, prints it
readBufOffset = 0; // Resets buffer counter
}
break;
}
}
}
void loop() {
stat = Serial1.available();
start_time = millis();
// Serial.print(counter++);
Serial.printf("Test#: %d", counter++);
// Serial1.print(counter);
Serial1.printf("Test#: %d", counter);
Serial.printf(" Serial1 stat: %d", stat);
Serial.printf(" Start time: %d", start_time);
stat2 = Serial.available();
before_flush = millis();
Serial.flush();
after_flush = millis();
Serial.printf(" Serial1 stat2: %d", stat2);
Serial.printf(" Before flush: %d", before_flush);
Serial.printlnf(" After flush: %d", after_flush);
delay(4000);
// Particle.process();
}
EXAMPLE OUTPUT:
Buffer:
Test#: 1 Serial1 stat: 6 Start time: 8772 Serial1 stat2: 0 Before flush: 8773 After flush: 8773
Buffer:
Test#: 2 Serial1 stat: 8 Start time: 12775 Serial1 stat2: 0 Before flush: 12775 After flush: 12776
Buffer:
Test#: 3 Serial1 stat: 10 Start time: 16777 Serial1 stat2: 0 Before flush: 16777 After flush: 16778
Buffer:
Test#: 4 Serial1 stat: 12 Start time: 20779 Serial1 stat2: 0 Before flush: 20779 After flush: 20779
Buffer:
Test#: 5 Serial1 stat: 14 Start time: 24780 Serial1 stat2: 0 Before flush: 24780 After flush: 24781
Buffer:
Test#: 6 Serial1 stat: 16 Start time: 28782 Serial1 stat2: 0 Before flush: 28782 After flush: 28782
Buffer:
Test#: 7 Serial1 stat: 19 Start time: 32783 Serial1 stat2: 0 Before flush: 32783 After flush: 32783
Buffer:
Test#: 8 Serial1 stat: 22 Start time: 36784 Serial1 stat2: 0 Before flush: 36784 After flush: 36784
Buffer:
Test#: 9 Serial1 stat: 22 Start time: 40785 Serial1 stat2: 0 Before flush: 40785 After flush: 40785
Buffer: 8
Test#: 10 Serial1 stat: 21 Start time: 44786 Serial1 stat2: 0 Before flush: 44786 After flush: 44786
Buffer: 4
Test#: 11 Serial1 stat: 20 Start time: 48787 Serial1 stat2: 0 Before flush: 48787 After flush: 48787
Buffer: tc
Test#: 12 Serial1 stat: 19 Start time: 52789 Serial1 stat2: 0 Before flush: 52789 After flush: 52789
Buffer: t~
Test#: 13 Serial1 stat: 18 Start time: 56791 Serial1 stat2: 0 Before flush: 56791 After flush: 56791
Buffer: VZ{@e
Test#: 14 Serial1 stat: 14 Start time: 60793 Serial1 stat2: 0 Before flush: 60793 After flush: 60794
Buffer: t:
Test#: 15 Serial1 stat: 14 Start time: 64795 Serial1 stat2: 0 Before flush: 64795 After flush: 64796
Buffer:
Test#: 16 Serial1 stat: 15 Start time: 68797 Serial1 stat2: 0 Before flush: 68797 After flush: 68797
Buffer: t
Test#: 17 Serial1 stat: 18 Start time: 72798 Serial1 stat2: 0 Before flush: 72798 After flush: 72798
Buffer: V
Test#: 18 Serial1 stat: 21 Start time: 76799 Serial1 stat2: 0 Before flush: 76799 After flush: 76799