2026-03-30 6:15 AM - last edited on 2026-03-31 2:32 AM by Gyessine
Title edited to clarify that this is about accessing a file located on a Host PC
hello ST Community,
I'm trying to open a binary file with the fopen function for a STM32L5 microcontroller, but it always gives me a null value. The code is like this:
char filepath[256];
sprintf(filepath,"%s","C:\\fileBIN.bin");
FILE *file;
file = fopen(filepath,"rb");
if(file == NULL)
{
printf("Error occurred opening file. \r\n");
}
else
{
printf("File opened successfully. \r\n");
}
It always gives me a null value but the path exists and is correct. What's wrong?
Thanks for supports
Solved! Go to Solution.
2026-03-30 7:50 AM
As explained, you can't do that - the microcontroller has no access to the Host PC's file system
@Domy_ST wrote:I get the file path by receiving it via UART.
Rather than sending the path via UART, send the data that you require!
2026-03-30 7:57 AM
the purpose is that I can not pass data directly via UART but only .bin files and it is the microcontroller's job to read the file contents
2026-03-30 8:12 AM
@Domy_ST wrote:I can not pass data directly via UART Of course you can!
Of course you can!
Sending the filename is just data - if you can send the file name, then you can equally send the content from the file.
@Domy_ST wrote:it is the microcontroller's job to read the file contents
But, again, the microcontroller does not have any access to the PC's file system!
I think we're still stuck in an X-Y problem here: why do you want the SMT32 to read this file?
What is the purpose of the SMT32 reading this file?
2026-03-30 8:15 AM
@Andrew Neil Is it possible to send data via uart starting from the file name/path? Is there a code example?
2026-03-30 8:20 AM
You would need something on the PC to send it.
You could write something yourself, or use something ready made; eg, CubeProgrammer or a Terminal app.
You still haven't said why you want to do it - so impossible to be specific.
2026-03-30 8:25 AM
as I specified in the previous post, I would like to make a sort of bootloader and then update the software to flash
2026-03-30 8:37 AM - edited 2026-03-30 8:46 AM
You never said that's what you're trying to achieve in this thread!
In that case, you will need to have something on the PC which sends the data to the Target.
The PC pushes to the Target - the Target doesn't pull from the PC.
CubeProgrammer can do this, and is ideal for use with the STM32 System Bootloader:
AN2606: Introduction to system memory boot mode on STM32 MCUs
AN3155: How to use USART protocol in bootloader on STM32 MCUs
If you're making a custom bootloader, then you'll have to define your own protocol; eg,
AN4657: STM32 in-application programming (IAP) using the USART - uses the YMODEM protocol, which is widely supported by terminal emulators & serial comms libraries/frameworks.
There's also ST Open Bootloader - also works with CubeProgrammer
2026-03-30 10:16 AM
I need to develop a custom bootloader. I'll study the YMODEM protocol and let you know. Thanks a lot for the support.
2026-03-30 10:28 AM
You're welcome.
If your issue is resolved, please mark the solution on the post which provided the answer (not this one!)
2026-03-31 12:34 AM
Opening a file on the host or doing other host-related system calls is called semi-hosting and need special steps:
How to use semihosting with STM32CubeIDE and STM32 - STMicroelectronics Community
Semihosting - SEGGER Knowledge Base
Haven't used it myself for a long time however.
The downside is, that this requires a driver on the host and is said to be slow.
hth
KnarfB