8 years, 1 month ago.

Serial pc

Hello! I'm trying to send a string or number from one mbed to another using the Serial functions getc and gets and it doesn´t works.

include the mbed library with this snippet

#include "mbed.h"
Serial pc(USBTX, USBRX);
 
int main() {
    pc.baud(115200);
    
    char command[20]; 
    char b[20];
    char s[20];
    int a;
    
    pc.printf("The commands received are\n");
    
    while(1) {
        if (pc.readable()) {
            pc.gets(command, a=sizeof pc.readable());
            for (int i=0; i<a; i++) {
                pc.printf("%c ", command[i]);
                sprintf(b,"%c",command[i]);
                strcat(s, b);
                
            }
            pc.printf("\n");
            pc.printf("out : %c",s);
        }
    }
}

4 5 6 missing. help me please /media/uploads/plsdont/aaaaa.png

2 Answers

8 years, 1 month ago.

https://developer.mbed.org/questions/61957/Serial-looses-characters-or-just-blocks/

Suspecting that you need to insert delays in your code and also that the printf calls are messing up your timing.

Try with a delay after the while(1) / before the if line. You must allow for a delay to permit the reading of data into the UART buffer space. Then check and continue. Please review the above URL for better options and consider to use a buffered serial method.

Inserting delays to make things work is NEVER a good idea. OK, sometimes you have to due to some critical timing requirement but serial port communications isn't one of those things. If adding a delay makes your serial link work then you need to remove the delay and fix your code.

posted by Andy A 03 Mar 2016

Agreed Andy and that was the reason for the:

"Please review the above URL for better options and consider to use a buffered serial method."

The delay was only recommended as an attempt to fix the posted code.

posted by Sanjiv Bhatia 03 Mar 2016
8 years, 1 month ago.

Your gets() line looks a bit odd.

gets(char* buffer, int len) will reads until it sees a new line or has read len-1 characters (the -1 is to allow space for the null terminator).

You are setting the length to (a=sizeof pc.readable()) which has a value of true. True is defined as anything other than 0, not a good value to use for a buffer size. That line also sets a to the size in bytes of the return value of pc.readable(). pc.readable() returns an int so it will set your variable a to a value of 4.

If you change your code to be this is should work better:

while(1) {
        if (pc.readable()) {
            pc.gets(command, sizeof(command));
            a = strlen(command);
            for (int i=0; i<a; i++) {
                pc.printf("%c ", command[i]);
                sprintf(b,"%c",command[i]); 
                strcat(s, b);
               // could use b[i]=command[i]; b[i+1] = 0; in place of two lines above, would be far faster.
            }
            pc.printf("\n");
            pc.printf("out : %c",s);
        }
    }
}

You also need to add a new line to the end of the string you are sending so that gets() will work correctly.

If the string you are looking for doesn't end in a new line then you need to use getc() and read the incoming data byte by byte until either you find the end or you time out waiting for the next byte to arrive.

Thank you very much.

posted by passaworn saengngoen 05 Mar 2016