cancel
Showing results for 
Search instead for 
Did you mean: 

I have an L475 Discovery IoT node, I want to collect sensor data from the board and save it in a text file in my laptop which is USB connected to the board. I have tried Fprint and fwrite but it doesn't seem to work.

IBo.1
Associate
fp=fopen("Issamos.csv","w+");
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	temp_value = BSP_TSENSOR_ReadTemp();
	int tmpInt1 = temp_value;
	float tmpFrac = temp_value - tmpInt1;
	int tmpInt2 = trunc(tmpFrac * 100000);
	snprintf(str_tmp,100," TEMPERATURE = %d.%02d\n\r", tmpInt1, tmpInt2);
 
	HAL_UART_Transmit(&huart1,( uint8_t * )str_tmp,sizeof(str_tmp),1000);
	fwrite(str_tmp, 100, 1,fp);
 
	 fclose(fp);
 

as you see I am collecting the data and using snprintf to display it in true studio, but writing it to a file doesn't work

3 REPLIES 3
KnarfB
Principal III

You cannot open a file on the host easily. But, you can transfer data via the serial connection included in the ST-LINK which is huart1

on the board. In order to do so, you implement a function _write in main.c:

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int file, char *ptr, int len)
{
	HAL_UART_Transmit(&huart1, ptr, len, HAL_MAX_DELAY );
	return len;
}
/* USER CODE END 0 */

This function will be used by printf (instead of the "weak" do-nothing implementaion of _write in syscalls.c). From now on, you may use printf as you wish and the outputis transferred to the host. It can be read in a terminal program like TeraTerm or others by opening the ST-LINK serial port.

See STMs "STM32 microcontroller debug toolbox" document for more details.

In theory, it might be possible to implement other file related functions from syscalls.c too (like _open) with some protocols and stubs on the other side ("semihosting"). Don't know of a readily available solution though.

Dear Knarf,

Many thanks for the answer, I am not from an embedded system background and I am just starting.

As I said before, I am currently sending the data from the board to turbostudio terminal and I can see the data which seems correct.

I now, want to find a way to either save the data while streaming on mu pc, or copy it from the terminal to a file and then I would do some processing on it.

Could you please tell me how the function you gave me could help

Regards

Most terminal programs (PuTTY, RealTerm, Termite, others) have a mode which logs the console output directly to a file. I would look into a feature similar to that.

If you feel a post has answered your question, please click "Accept as Solution".