This is single way communication with the PC

Dependencies:   mbed

main.cpp

Committer:
akverma
Date:
2016-12-28
Revision:
1:dad12410fd2d
Parent:
0:5d7b0ed9dcc9

File content as of revision 1:dad12410fd2d:

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
uint8_t data;
Serial pc(USBTX, USBRX);
 
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() {
    uint8_t read_value;
    pc.printf("Hello World!\n");
    while(1)
    {
        read_value = take_read();
        pc.printf("I got '%X'\n", read_value);
    }
}
/* 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.*/