![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
This is single way communication with the PC
Dependencies: mbed
main.cpp@1:dad12410fd2d, 2016-12-28 (annotated)
- Committer:
- akverma
- Date:
- Wed Dec 28 22:59:38 2016 +0000
- Revision:
- 1:dad12410fd2d
- Parent:
- 0:5d7b0ed9dcc9
Serial communication both way, complete package.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RahulSitaram | 0:5d7b0ed9dcc9 | 1 | #include "mbed.h" |
RahulSitaram | 0:5d7b0ed9dcc9 | 2 | |
RahulSitaram | 0:5d7b0ed9dcc9 | 3 | DigitalOut led1(LED1); |
RahulSitaram | 0:5d7b0ed9dcc9 | 4 | DigitalOut led2(LED2); |
akverma | 1:dad12410fd2d | 5 | uint8_t data; |
RahulSitaram | 0:5d7b0ed9dcc9 | 6 | Serial pc(USBTX, USBRX); |
RahulSitaram | 0:5d7b0ed9dcc9 | 7 | |
akverma | 1:dad12410fd2d | 8 | uint8_t take_read() |
akverma | 1:dad12410fd2d | 9 | { |
akverma | 1:dad12410fd2d | 10 | char c; |
akverma | 1:dad12410fd2d | 11 | int len; |
akverma | 1:dad12410fd2d | 12 | char buffer[128]; |
akverma | 1:dad12410fd2d | 13 | pc.gets(buffer, 1024); |
akverma | 1:dad12410fd2d | 14 | len=strlen(buffer)-1; |
akverma | 1:dad12410fd2d | 15 | if (buffer[len] == '\n') |
akverma | 1:dad12410fd2d | 16 | buffer[len] = '\0'; |
akverma | 1:dad12410fd2d | 17 | c = atoi(buffer); |
akverma | 1:dad12410fd2d | 18 | return c; |
akverma | 1:dad12410fd2d | 19 | //pc.printf(" I got '%s' of length '%d', in hex is '%x'\n", buffer, len, c); |
RahulSitaram | 0:5d7b0ed9dcc9 | 20 | } |
RahulSitaram | 0:5d7b0ed9dcc9 | 21 | |
RahulSitaram | 0:5d7b0ed9dcc9 | 22 | int main() { |
akverma | 1:dad12410fd2d | 23 | uint8_t read_value; |
akverma | 1:dad12410fd2d | 24 | pc.printf("Hello World!\n"); |
akverma | 1:dad12410fd2d | 25 | while(1) |
akverma | 1:dad12410fd2d | 26 | { |
akverma | 1:dad12410fd2d | 27 | read_value = take_read(); |
akverma | 1:dad12410fd2d | 28 | pc.printf("I got '%X'\n", read_value); |
RahulSitaram | 0:5d7b0ed9dcc9 | 29 | } |
akverma | 1:dad12410fd2d | 30 | } |
akverma | 1:dad12410fd2d | 31 | /* When you read 1024 bytes(max) the last byte is reserved for '\0' so only 1023 bytes will be read anyway. |
akverma | 1:dad12410fd2d | 32 | Furthermore '\n' and '\r' are counted with measuring length but '\0' is not counted. |
akverma | 1:dad12410fd2d | 33 | You can replace '\n' by '\0' and not send '\r' at all.*/ |