6 years, 9 months ago.

how to send char but receive string on L073RZ using UART?

Troubleshooting!

Hi anyone mind helping me to take a look and help me correct it? cuz i have been trying to change my receiving of char into string but can't seem to do it. The code below is how my current working code looks like.

main.cpp

#include "mbed.h"

//--------------------------------------
//| Hyperterminal configuration         |
//| 115200 bauds, 8-bit data, no parity |
//--------------------------------------

Serial pc(SERIAL_TX, SERIAL_RX); //PC to STM32 Board
//Serial transmit(PC_4,PC_5); // STM32 writing/reading with Arduino
Serial uart(PC_4,PC_5);     

//DigitalOut myled(LED1);
DigitalIn BlueButton(USER_BUTTON);  // This is Blue-Button and is on NUCLEO-L153RE
DigitalOut pc_activity(LED1);
DigitalOut uart_activity(LED2);
 
#define Pressed 0
#define NotPressed 1

int main() {
    pc.printf("Select one of the following...");
    pc.printf("\r\n");
    pc.printf("i = deice type and version number");
    pc.printf("\r\n");
    pc.printf("r = take a single pH reading");
    pc.printf("\r\n");
    
    uart.baud(115200);                          //Setting of baud rate
 
    while(1) {
        if(pc.readable()) {                     //If PC port got reading/get input of char
            uart.putc(pc.getc());               //Sending char to arduino
            pc.printf("Message Sent!\r\n");
            pc_activity = !pc_activity;         //LED indication
        }
        if(uart.readable()) {                   //UART excute only when receive reading 
            pc.printf("Message Received:");
            pc.printf("\r\n");
            pc.putc(uart.getc());               //Receving char from arduino
            pc.printf("\r\n");
            uart_activity = !uart_activity;     //LED indication
        }
    }
}

1 Answer

6 years, 9 months ago.

Hi there,

scanf() might be what you're looking for:

char buffer[20];
if (uart.readable()) {
    pc.putc(uart.scanf("%s", &buffer));
}

However, the above code snippet will only work if you are also receiving a "carriage return" character (\r) from your uart.

- Jenny

Troubleshooting!

Hey hi Jenny,

I tried using scanf() but is not working. i currently using "carriage return" on my arduino side but for my STM i also using "carriage return". Now trying to send byte from arduino to STM but i can receive but cannot display on the terminal. Because i am actually trying to receive bytes in array to display on terminal. The code below is what my current code looks like.

while(1) {
        if(pc.readable()) {                     //If PC port got reading/get input of char
            uart.putc(pc.getc());               //Sending char to arduino
            pc.printf("Message Sent!\r\n");
            //pc_activity = !pc_activity;         //LED indication
        }
        if(uart.readable()) {                   //UART excute only when receive reading            
            
            pc.printf("in while getc\n");//test
            int i=0;
              while(uart.getc() != 'E')
            {
        pc.printf("in \n");//test
               pc.putc(uart.getc());
                    buffer[i]=uart.getc();
                    wait(0.5);
                    i++;
                
                
    
            }
            pc.printf("uart is %s",buffer);
          /*  
            pc.printf("Message Received:");
            pc.printf("\r\n");
            pc.putc(uart.getc());               //Receving char from arduino
            pc.printf("\r\n");
            uart_activity = !uart_activity;     //LED indication
            */
        }
    }

-Mel

posted by melvin awesome 19 Jul 2017