基于Nucleo F411RE和光流模块的通讯实验,串口1接收到数据之后解析,并从串口2输出光流模块计算结果
Fork of Nucleo_serial by
Diff: main.cpp
- Revision:
- 4:5ac470115649
- Parent:
- 2:08c13f9a3d5c
- Child:
- 5:5fd053d79f06
--- a/main.cpp Wed Jun 07 13:16:58 2017 +0000 +++ b/main.cpp Sun Apr 08 14:49:52 2018 +0000 @@ -1,21 +1,90 @@ #include "mbed.h" -//------------------------------------ -// Hyperterminal configuration -// 9600 bauds, 8-bit data, no parity -//------------------------------------ - Serial pc(SERIAL_TX, SERIAL_RX); +DigitalOut led01(LED1); -DigitalOut myled(LED1); +int i; // for cycles + char cmd_on[] = "on"; + char cmd_off[] = "off"; + +char *getCmd(); +void processCmd(char *cmd); + +Thread t1; -int main() + /////////////////////////////////////////////////////////////////////////////// +void tSerial_body() +{ + while(1){ + if (pc.readable()) // if there is an character to read from the device + { + char *SerialCommand = getCmd(); + processCmd(SerialCommand); + free(SerialCommand); + } + } +} + /////////////////////////////////////////////////////////////////////////////// +char *getCmd() { - int i = 1; - pc.printf("Hello World !\n"); - while(1) { - wait(1); - pc.printf("This program runs since %d seconds.\n", i++); - myled = !myled; + char buff[128]; + unsigned char ch; + int buffIndex = 0; + memset(buff, 0, sizeof(buff)); // clean the buffer + + + char *cmd = (char*)malloc(128); + if (!cmd) + return NULL; + do + { + ch = pc.getc(); // read it + if (buffIndex < 128){ // just to avoid buffer overflow + buff[buffIndex] = ch; // put it into the value array and increment the index + buffIndex++; + } + } + while (ch != '\n' && ch != '\r' ); + buff[buffIndex]='\0'; // add the end-signalling char + + if(strlen(buff) != 0){ + + for (i = 0 ; i < sizeof(buff) ; i++){ + if (buff[i] == '\n' || buff[i] == '\r'){ + cmd[i] = '\0'; + return cmd; + } + cmd[i] = buff[i]; + } + } + return NULL; +} + /////////////////////////////////////////////////////////////////////////////// +void processCmd(char *cmd) +{ + pc.printf("rec: \"%s\"\n\r", cmd); + if(strcmp(cmd,cmd_on) == 0){ + led01 = 1; + pc.printf("Turning the led on.\n\r"); + } else if (strcmp(cmd,cmd_off) == 0) { + led01 = 0; + pc.printf("Turning the led off.\n\r"); + } } + +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + t1.start(tSerial_body); + + led01 = 1; + + pc.printf("\n\r App started______Banik pyco\n\r"); + int counter = 1; + while(1) + { + pc.printf("seconds: %d\n\r", counter++); + wait(1); + } +} \ No newline at end of file