This is single way communication with the PC
Dependencies: mbed
Revision 1:dad12410fd2d, committed 2016-12-28
- Comitter:
- akverma
- Date:
- Wed Dec 28 22:59:38 2016 +0000
- Parent:
- 0:5d7b0ed9dcc9
- Commit message:
- Serial communication both way, complete package.
Changed in this revision
backup/main_backup.cpp | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backup/main_backup.cpp Wed Dec 28 22:59:38 2016 +0000 @@ -0,0 +1,21 @@ +/*#include "mbed.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); + 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; +} + +int main() { + pc.attach(&call); + while (1) { + led1 = !led1; + wait(0.5); + } +}*/ \ No newline at end of file
--- 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