5 years, 5 months ago.

Send data between PC Monitor and Bluetooth through the serial port

Hi, I would like to send commands from the PC to the bluetooth HC-05 and vice versa through the serial port. I have created the serial port as a Serial, but I would not know how to do the exchange of readings and writings. I have the following:

PinName TX = PC_6;
PinName RX = PA_12;

Serial my_serial(TX, RX);

void config(){
    //if(my_serial.availab){
        Serial.write(my_serial.read());
    //}else{
       }
}

What I am doing is to configure the bluetooth. how could I do it?

Thanks!!

1 Answer

5 years, 5 months ago.

Hi Jose,

We have an example for serial pass-through between PC and device UART. https://os.mbed.com/docs/v5.10/apis/serial.html#serial-examples

It needs two UARTs to achieve this application, one is for communicating with PC, and the other is for communicating with device, so you also need to check the platform in use is equipped with two UARTs.

Different devices support different baud rate, so you may need to configure the serial object to correct baudrate.

#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
Serial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX);  // tx, rx
int main() {
    device.baud(38400); // default baud rate of HC05
    while (1) {
        // Add the serial pass-through code here
    }
}

Please let me know if you have any questions!

- Desmond, team Mbed

Good again, sorry but I think I did not explain myself well. What I want to do is that through an app that I have when I press a button I send the character assigned to that button by the serial port so that my program depending on the received character does one thing or another. I have been trying to debug the code and for this I had to install the Software System Workbench for STM32 but I have problems importing the MBED project. I have made some changes to my code and attach it here:

#include "mbed.h"

PinName TX = PC_6;
PinName RX = PA_12;

Serial pc(TX, RX);

int nuevoDato=0; 
char dato; 

int main() {
    pc.baud(9600);
    pc.attach(&recibeDatos);
    while(1){
        if(nuevoDato==1){
            nuevoDato=0;
            switch(dato) {
                case 'A':
                avanza();
                break;
                case 'H':
                home();
                break;
            }
        }
    }
}

void recibeDatos(){
    dato = pc.getc();
    nuevoDato=1;
}

This code does not execute anything I do not know if it is missing something to receive the character by the serial port and depending on the received character that does one thing or another. Or I do not know if it can also be for the Baud that has the Bluetooth configured that does not match the main and can give problems.

Thank you!!

posted by Jose Romero 14 Nov 2018

Hi Jose,

I will suggest you to use RawSerial class, which is more suitable for use interrupt handler with the RTOS. Let me know how it goes.

- Desmond, team Mbed

posted by Desmond Chen 15 Nov 2018