K64F RawSerial (Or Serial) Troubles

17 May 2015

Hello,

So, I ran into a problem. My Serial callback function that I attached is not working. I'm using it on pins USBTX/USBRX.

I saw a few posts about this with using mbed-RTOS, which I'm using. Some suggested using RawSerial. I switched to RawSerial and that did fix my problem with it hanging, however, RawSerial only see's one character of my string that I passed in.

I tried looping on the readable() function and tried calling getc() consecutive times, both only showed like 1 character was there, the first one I typed in a string.

I tried a couple of other 3rd party libs that were published with no good results. I'm looking for a way so I can type serial input and read it on my device. If I start a new project and use Serial, it all works great.

Is there a way to get the entire string buffer from RawSerial?

Thanks Jeff.

17 May 2015

Can you post the code you are using? (between <<code>> and <</code>>)

29 May 2015

Hi Erik,

Ok, I created a new project, I don't understand what I'm doing wrong. I am sending a string through my serial program Termite 3.1. I send ABC and it outputs:

char 0: A char 1: [00] char 2: [00] char 3: [00] char 4: [00] char 5: [00] char 6: [00] char 7: [00] char 8: [00] char 9: [00]

Any idea on how to get the full ABC or any string I type in??

simple serial

#include "mbed.h"

DigitalOut myled(LED_GREEN);
Serial pc(USBTX, USBRX, "modser");

void test()
    {
    char buf[10];
    
    memset(buf, 0, sizeof(char)*10);
    int idx = 0;
    
    while(pc.readable())
        {
        if (idx < 10)
            buf[idx++] = pc.getc();
        else
            pc.getc();
        }
    
    for(int i = 0;i < 10;i++)
        pc.printf("char %i: %c\n",i, buf[i]);
    }

int main()
{
    pc.baud(38400);
    pc.attach(&test);
    
    pc.printf("Hello World!\n");

    while (true) {

        wait(0.5f); // wait a small period of time
        //pc.printf("%d \n", i); // print the value of variable i
        //i++; // increment the variable
        myled = !myled; // toggle a led
    }
}
29 May 2015

The problem is that your function test is called immediatly when the first char is received. It will keep receiving while pc is readable, however you need to take into account that this code runs much faster than the speed of Serial. So when the first char is received, it will check if any other chars are in the receive buffer, which they won't be since 'B' hasn't been sent yet, and it will print everything it received for now, which is only 'A'.

When you send 'B' next it should give you that output, but now only with 'B' at the start.

If you want to receive everything first, you need to make a global buf and idx, and make some kind of code which only prints if once it is done sending. For example only print it when it received \r or \n (depends on your terminal settings), so only when you hit enter.

29 May 2015

Hi Erik,

Thanks for the response. However, I never get the B and C characters outputting. If I send "ABC" it only outputs A (essentially). If I understand what you're saying, shouldn't have I seen:

char 0: A char 1: [00] char 2: [00] char 3: [00] char 4: [00] char 5: [00] char 6: [00] char 7: [00] char 8: [00] char 9: [00]

char 0: B char 1: [00] char 2: [00] char 3: [00] char 4: [00] char 5: [00] char 6: [00] char 7: [00] char 8: [00] char 9: [00]

char 0: C char 1: [00] char 2: [00] char 3: [00] char 4: [00] char 5: [00] char 6: [00] char 7: [00] char 8: [00] char 9: [00]

Instead of just

char 0: A char 1: [00] char 2: [00] char 3: [00] char 4: [00] char 5: [00] char 6: [00] char 7: [00] char 8: [00] char 9: [00]

Is that correct?

Also question #2 (related): Notice in my while loop I have a pc.printf commented out, when that was commented in, my outputting would stop after a while when I was typing things in, does having printf's in my test() function and in the main conflict somehow? Are they running in 2 threads?

Thanks! Jeff.

31 May 2015

Erik,

Nevermind! I solved this issue. You were correct, apparently, doing any printf's in between the character stream causes enough delay, I think, to skip those characters. So, once I took the printf's out and just buffered all characters until a \r, that worked perfectly.

Thanks for the help! Jeff.