I am sending data and receiving data from hercules. So, I want to receive some data from Hercules and display that data in Hercules, but I have several problems. Este es el codigo:
#include “mbed.h” #include “BufferedSerial.h”
Mutex p_serie;
//declaraciones de pines
BufferedSerial pc(USBTX, USBRX);
//prototipos de las funciones
void recibir_datos_serie(void);
void recibir_datos_serie(void){
char leer_dato[1];
while (true) {
p_serie.lock(); // Bloquear el acceso al puerto serie para evitar condiciones de carrera
if (pc.readable()) {
pc.read(leer_dato, sizeof(leer_dato));
printf(men, "\n\r");
pc.write(leer_dato, sizeof(leer_dato)); // Eco de vuelta de los datos recibidos
}
p_serie.unlock(); // Desbloquear el acceso al puerto serie
ThisThread::sleep_for(10ms); // Esperar un corto período de tiempo antes de la próxima lectura
}
}
Problems:
this phrase “Inicializando programa…”, in main(), I can only see it when I send data, and I need to see it before, when the program starts, it should be like this.
-When I send the data and show it in Hercules, it appears twice, why?
probably because you print the mem variable in the block below and because use BufferedSerial on same UART which is dedicated for default console (printf) .
It will be solved as soon as you remove this line - printf(men, "\n\r");
Values are in Hercules twice because one color is your input, what you send form Hercules to the device. And second color is echo from your device, what Hercules received from the device.
I am not sure why the sentence is missed at the beginning but your main is not in correct logic I think.
Try main like this.
#define MY_DEBUG 1 // switch debug msg ON/OFF
int main()
{
debug_if(MY_DEBUG , "Debug: Mbed start\n");
// 1. start second thread
debug_if(MY_DEBUG , "Debug: second thread start\n");
hilo_recibir_datos_serie.start(recibir_datos_serie);
// 2. let main thread run
debug_if(MY_DEBUG , "Debug: main loop start\n");
while(1){
thread_sleep_for(1000);
debug_if(DEBUG, "Debug: loop\n");
}
}
in funcion
void recibir_datos_serie(void){
sprintf(men, "Inicializando programa...\n\r");
pc.write(men, sizeof(men));
char leer_dato[1];
while (true) {
p_serie.lock(); // Bloquear el acceso al puerto serie para evitar condiciones de carrera
if (pc.readable()) {
pc.read(leer_dato, sizeof(leer_dato));
//sprintf(men, "Inicializando programa...\n\r");
pc.write(men, sizeof(men));
pc.write(leer_dato, sizeof(leer_dato)); // Eco de vuelta de los datos recibidos
}
p_serie.unlock(); // Desbloquear el acceso al puerto serie
ThisThread::sleep_for(10ms); // Esperar un corto período de tiempo antes de la próxima lectura
}
}