This is single way communication with the PC
Dependencies: mbed
Diff: main.cpp
- Revision:
- 1:dad12410fd2d
- Parent:
- 0:5d7b0ed9dcc9
diff -r 5d7b0ed9dcc9 -r dad12410fd2d main.cpp --- a/main.cpp Sun Dec 04 19:19:19 2016 +0000 +++ b/main.cpp Wed Dec 28 22:59:38 2016 +0000 @@ -2,20 +2,32 @@ DigitalOut led1(LED1); DigitalOut led2(LED2); - uint8_t data; +uint8_t data; Serial pc(USBTX, USBRX); -void call() { - // Note: you need to actually read from the serial to clear the RX interrupt - data = pc.getc(); - printf("%c\n", data); - led2 = !led2; +uint8_t take_read() +{ + char c; + int len; + char buffer[128]; + pc.gets(buffer, 1024); + len=strlen(buffer)-1; + if (buffer[len] == '\n') + buffer[len] = '\0'; + c = atoi(buffer); + return c; + //pc.printf(" I got '%s' of length '%d', in hex is '%x'\n", buffer, len, c); } int main() { - pc.attach(&call); - while (1) { - led1 = !led1; - wait(0.5); + uint8_t read_value; + pc.printf("Hello World!\n"); + while(1) + { + read_value = take_read(); + pc.printf("I got '%X'\n", read_value); } -} \ No newline at end of file +} +/* When you read 1024 bytes(max) the last byte is reserved for '\0' so only 1023 bytes will be read anyway. +Furthermore '\n' and '\r' are counted with measuring length but '\0' is not counted. +You can replace '\n' by '\0' and not send '\r' at all.*/ \ No newline at end of file