Änderungen

Aus Hackerspace Ffm
Wechseln zu: Navigation, Suche

Raspberry Pi Advanced

1.411 Byte hinzugefügt, 13:02, 19. Jul. 2017
/* GPIO Serial */
Serial.write(Serial.read());
}
}
</pre>
Und hier ein etwas komplexerer Kommandoparser:<br>
Die Kommandos sind a und b gefolgt von einem Integer wie z.B. ''a21''
<pre>
void receive_serial_cmd(void) {
static uint8_t cmd[18]; // command buffer
static uint8_t cmdcount = 0; // position in the buffer of the received byte
uint8_t c; // received byte
while(Serial.available()) {
c = Serial.read();
if(c > ' ') cmd[cmdcount++] = c;
if((c == 8) && (cmdcount > 0)) cmdcount--; // deals with backspaces, if a person on the other side types
if((c == 0x0d) || (c == 0x0a) || (cmdcount > 16)) { // end of command, gets interpreted now
cmd[cmdcount] = 0; // clear the last byte in cmd buffer
if(cmdcount > 0) { // prevent empty cmd buffer parsing
switch(cmd[0]) {
case 'a':
if((cmdcount > 2) && (cmdcount < 7)) {
int temp = atoi((const char *)&cmd[1]);
Serial.print("a:");
Serial.println(temp);
}
break;
case 'b':
if((cmdcount > 2) && (cmdcount < 7)) {
int temp = atoi((const char *)&cmd[1]);
Serial.print("b:");
Serial.println(temp);
}
break;
}
}
cmdcount = 0;
}
}
}
</pre>
688
Bearbeitungen