Use of SerialEvent()
hi,
i'd use serialevent() capture incoming software serial data cannot seem it.
the first of 2 following codes works it's not using serialevent() option.
but second code fails , it's using serialevent() option.
what doing wrong? and, practice using serialevent()? perhaps, don't need it.
tia
i'd use serialevent() capture incoming software serial data cannot seem it.
the first of 2 following codes works it's not using serialevent() option.
but second code fails , it's using serialevent() option.
what doing wrong? and, practice using serialevent()? perhaps, don't need it.
tia
code: [select]
/* incoming data:
t0123456789
s012345678901234567
ip192.168.0.12
*/
#include <softwareserial.h>
softwareserial esp_serial(10, 11); // rx, tx
string epoch;
void readepoch(void);
void setup() {
// open serial communications , wait port open:
serial.begin(9600);
esp_serial.begin(9600);
delay(10);
}
void loop()
{
readepoch();
serial.println(epoch); //works!! prints 0123456789
delay(1000);
}
void readepoch() {
string readstring;
while (esp_serial.available()) {
delay(10); //small delay allow input buffer fill
char c = esp_serial.read(); //gets 1 byte serial buffer
readstring += c;
if (c == '\n') {
break; //breaks out of capture loop print readstring
}
}
readstring.trim(); // trim white space off string if any
// if first char = t read unix time
if (readstring.length() > 9 && readstring.startswith("t")) {
epoch = readstring.substring(1, 11);
}
if (readstring.length() > 12 && readstring.startswith("s")) {
// more code here
}
readstring = "";
}
code: [select]
/* incoming data:
t0123456789
s012345678901234567
ip192.168.0.12
*/
#include <softwareserial.h>
softwareserial esp_serial(10, 11); // rx, tx
string inputstring = ""; // string hold incoming data
boolean stringcomplete = false; // whether string complete
void setup() {
// initialize serial:
serial.begin(9600);
esp_serial.begin(9600);
// reserve 40 bytes inputstring:
inputstring.reserve(40);
delay(10);
}
void loop() {
// print string when newline arrives:
if (stringcomplete) {
serial.println(inputstring);
// clear string:
inputstring = "";
stringcomplete = false;
}
}
/*
serialevent occurs whenever new data comes in the
hardware serial rx. routine run between each
time loop() runs, using delay inside loop can delay
response. multiple bytes of data may available.
*/
void serialevent() {
while (esp_serial.available()) {
// new byte:
char inchar = (char)esp_serial.read();
// add inputstring:
inputstring += inchar;
serial.println(inputstring); //totally empty!!
// if incoming character newline, set flag
// main loop can it:
if (inchar == '\n') {
stringcomplete = true;
}
}
}
serialevent() doesn't work software serial.
it not necessary use serialevent(). can have "good code" use , equally "good" code doesn't use it.
for larger programs, seems write own, because can control runs inside large loop. standard serialevent() can run @ end of loop().
it not necessary use serialevent(). can have "good code" use , equally "good" code doesn't use it.
for larger programs, seems write own, because can control runs inside large loop. standard serialevent() can run @ end of loop().
Arduino Forum > Using Arduino > Programming Questions > Use of SerialEvent()
arduino
Comments
Post a Comment