How do you send chars/printfs over serial in mbed 6?

Im trying to port my old mbed code to mbed 6 and it completly broke my serial communication.

Im using buffered serial like so:

static BufferedSerial pc(P0_2, P0_3);
char pc_buf[3] = {};

pc.set_baud(9600);

bool debug_on = true;

void commandManager(char c[]){
    switch(c[0]){
        case 'w':
            //Confirm command is recieved and is being executed for teleop purposes
            pc.write(pc_buf, c[0]);
            fwd();
            break;
        default:
            break;
    }
}

int main(){
    while(1) {
        char tempcommand = pc.read(pc_buf, sizeof(pc_buf));
        
        if(debug_on == true){
            printf("pc_buf result is: ");
            pc.write(pc_buf, tempcommand);
        }   
        commandManager(&tempcommand);
}
}

when I try to enter w, or any other commands I have in my command manager, the letter im typing is printed back to be with pc.write(I think) but none of the printf(s) are printed to the console,

UNLESS i spam type keys, then multiple printf(s) are printed back at once, but they don’t contain the tempcommand they are supposed to print with, making me think the tempcommand chars are not being read.

my setup worked pre-mbed 6 with mbed 4> code, but after I tried to port it to mbed 6, it now fails like above. what am I doing wrong?

Well for one thing, what you have as char tempcommand is actually getting set by pc.read() to the number of characters read. I think you want to be using pc_buf in most of the places you’re using tempcommand.

Hello,

according to BufferedSerial - API references and tutorials | Mbed OS 6 Documentation the method BufferedSerial::read(…) not return the char type but size_t type. So how it is in description

Return The number of bytes read, 0 at end of file, negative error on failure

The second parameter of the size_t read(void *buffer, size_t length) method is size of buffer.

Also the read(...) method does not have to take the whole string so you need to collect all parts and put then together.

Example
#include "mbed.h"

#define BUFFSIZE 10
#define DEBUGON 1

static BufferedSerial pc(CONSOLE_TX, CONSOLE_RX); // your pins

int main(){
    printf("Example\n");

    size_t totalSize = 0;
    size_t size = 0;
    char buff[BUFFSIZE] = {};
    char c;
    
    while(1) {
        if((size = pc.read(&c, sizeof(char)))){
            // collect data until a specific char is coming, in this case the LF
            if(c == '\n' ){
                buff[totalSize] = '\0';
                debug_if(DEBUGON,"Input: %s\n", buff); /*debug printf https://os.mbed.com/docs/mbed-os/v6.12/apis/debug.html*/
                totalSize = 0;
                switch(buff[0]){
                    case 'A':
                        debug_if(DEBUGON,"Cmd A\n");
                        // echo of cmd as ack
                        pc.write(&buff[0], sizeof(buff[0]));
                        // echo of whole cmd string
                        //pc.write(buff, strlen(buff));
                        
                        // do something for A cmd HERE
                        break;
                    default:
                        debug_if(DEBUGON,"Cmd is not valid\n");
                        break;
                } 
            }else{
                buff[totalSize] = c; 
                totalSize += size;
            }
        }
    }
}

BR, Jan

1 Like

Thank you for the sample code, I took bits and peices of it and ended up with this which reads and writes like my pre mbed6 code!

    switch(c[0]){
        case 'w':

            //Confirm command is recieved and is being executed for teleop purposes
            pc.write(&c[0], sizeof(char));
            fwd();
            //command = '\0';
            break;
        default:
            break;
    }
int main(){

    size_t totalSize = 0;
    char buff[BUFFSIZE] = {};
    char c;
    buff[totalSize] = '\0';
    while (1){
        pc.read(&c, sizeof(char));

        //im too lazy to clear buffer, so I do this instead.
        pc.write(&buff[0], sizeof(char));
        commandManager(&c);
    }
}