coppino/slip.ino
giomba 895206a046 SLIP bug: reading too fast from serial
when the escape char was read from serial, then the escaped char was
read immediately after, but it was not available, thus resulting in
no proper escape; now fixed
2019-12-23 21:45:25 +01:00

59 lines
1.1 KiB
C++

namespace slip {
void send(const char* buffer, int len) {
// Serial.write(END); /* frame-out any possible noise on the line */
for (int i = 0; i < len; ++i) {
switch (buffer[i]) {
case END:
Serial.write(ESC);
Serial.write(ESC_END);
break;
case ESC:
Serial.write(ESC);
Serial.write(ESC_ESC);
break;
default:
Serial.write(buffer[i]);
break;
}
}
Serial.write(END);
}
int recv(char* buffer, int len) {
char r;
int i = 0;
while (i < len) {
if (Serial.available()) {
r = Serial.read();
switch (r) {
case ESC:
while (Serial.available() <= 0);
r = Serial.read();
if (r == ESC_END) {
buffer[i++] = END;
} else if (r == ESC_ESC) {
buffer[i++] = ESC;
}
break;
case END:
return i;
break;
default:
buffer[i++] = r;
break;
}
}
}
return i;
}
} /* namespace slip */