Question about read/write at serial port.

22 May 2017

Hello guys!

I am trying to connect for Nucleo a some device. Device send and read command like [0xaa, 0x70, 0x00, 0xcc, 0x33, 0xc3, 0x3c].

I managed to send the commands, but I do not have to read it.

I would like to read them and display them on the USB for the analysis of these commands. Unfortunately I can not understand what exactly I am doing wrong.

my code

#include "mbed.h"

Serial USB_SP(USBTX,USBRX,NULL,115200); // tx, rx
Serial STONE_SP(PC_10,PC_11,NULL,115200); // tx, rx

DigitalOut myled(LED1);
DigitalIn mybutton(USER_BUTTON);


void LoadFirstScreen(void)
{
    char s[] = {0xaa, 0x70, 0x00, 0xcc, 0x33, 0xc3, 0x3c};
    STONE_SP.puts(s);
}

void LoadLogo(void)
{
    char s[] = {0xaa, 0x70, 0x00, 0xcc, 0x33, 0xc3, 0x3c};
    char temp;

    STONE_SP.puts(s);
    for (int i=0; i<250; i++) {  //Load Logo
        temp = s[2];
        temp = temp++;
        s[2] = temp;
        STONE_SP.puts(s);
        wait(0.01);
    }
}

int main()
{
    LoadFirstScreen();

    while(1) {

        myled = 1;
        wait(0.5);

        USB_SP.printf("The commands received are\n");

        if (mybutton == 0) { // Button is pressed
            LoadLogo();
        }
//------------------------------------------------------


        USB_SP.printf("%d,", STONE_SP.readable());
        while (STONE_SP.readable()>0){
            USB_SP.putc(STONE_SP.getc());
            wait_ms(2);
        } 
        wait_ms(10);  

        myled = 0;
        wait(0.5);

    }


}

22 May 2017

Oh, Found the problem!

Just delete "wait(0.5);' And, It's ok, I finally can read commands from device.

23 May 2017

If you want to use the default mbed serial interface on the Nucleo board you just use printf. It is configured as part of mbed-os initialization - you don't have to make a new object. The mbed interface chip on the Nucleo board already acts as a USB virtual serial port for you.

Delete:

Serial USB_SP(USBTX,USBRX,NULL,115200); // tx, rx

And replace all

USB_SP.printf(..);

With:

printf(..);

The use of STONE_SP, looks basically right though.

I don't have a quick answer for you on the reading. But polling the serial port to ask it if there is new data, does not look like the best solution, especially if you are only doing it every 0.5seconds. Who knows how much data might have come in while you weren't looking. When reading data, I think the normal thing to do is to set up an interrupt that runs as soon as new data is received. That way you handle it immediately and don't overflow the buffer. I would look into attaching callback to a read interrupt. Attach function is defined in SerialBase.h.

    /** Attach a function to call whenever a serial interrupt is generated
     *
     *  @param fptr A pointer to a void function, or 0 to set as none
     *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
     */
    void attach(void (*fptr)(void), IrqType type=RxIrq);

Please log in to post a reply.