<stdio.h>, snprintf() not implemented?

Programmable Converter for RS-232 to CAN
Locked
zwlesko
Posts: 2
Joined: Tue 14. May 2024, 23:50

<stdio.h>, snprintf() not implemented?

Post by zwlesko » Wed 15. May 2024, 00:21

Hi,

I'm attempting to convert a floating point value as a string in order to send it over RS-232 ASCII serial.

Naturally, I tried using snprintf() and sprintf() from stdio.h, but it seems to make the PEAK RS-232 device hang as soon as either function is called.

As a sanity check I compiled the following code with normal GCC compiler and it worked as expected:

Code: Select all

#include <stdio.h>

int main() {
    char buffer[100];   // Buffer to hold the output string
    float myFloat = 3.14159265;

    // Use snprintf to format the float to 3 decimal places
    snprintf(buffer, sizeof(buffer), "%.3f", myFloat);

    // Print the formatted string
    printf("Formatted Float: %s\n", buffer);

    return 0;
}
I then tested with the CAN_ECHO example project. I compiled the CAN_ECHO example project, loaded it to the RS-232 device, and ensured it worked as expected.
Next, I added the code to the example project:

Code: Select all

#include <stdio.h>

Code: Select all

    char buffer[100];   // Buffer to hold the output string
    float myFloat = 3.14159265;

    // Use snprintf to format the float to 3 decimal places
    snprintf(buffer, sizeof(buffer), "%.3f", myFloat);
It compiles, but when I flashed it the device the CAN messages I sent were no longer echoing back.

Has anyone else encountered this error? Is snprint() etc. not implemented?
What is the work around for transmitting ASCII representation of floating point values?

Thanks

zwlesko
Posts: 2
Joined: Tue 14. May 2024, 23:50

Re: <stdio.h>, snprintf() not implemented?

Post by zwlesko » Wed 15. May 2024, 08:27

The problem was in my Makefile and pcan_rs_232.ld file.

I noticed snprintf() was used in some of the example projects which worked fine when I tried them.

Because I was unlucky enough to have started my project by duplicating the CAN_ECHO project, and had added in the .h and .c files as I needed them, I missed these other differences in Makefile and pcan_rs_232.ld.

In Makefile starting at line 42:

Code: Select all

# List C source files here which must be compiled in ARM-Mode.
# use file-extension c for "c-only"-files
CSRCARM += $(SRC_FOLDER)/main.c
CSRCARM += $(SRC_FOLDER)/can_user.c
CSRCARM += $(SRC_FOLDER)/ser_user.c
CSRCARM += $(SRC_FOLDER)/syscalls.c
CSRCARM += $(SRC_FOLDER)/systime.c
In pcan_rs_232.ld at line 6:

Code: Select all

/* Stack Sizes defined here, Stackaddresses provided to other Modules by
	Linkerscript */
UND_Stack_Size = 8;
ABT_Stack_Size = 8;
FIQ_Stack_Size = 8;
IRQ_Stack_Size = 256;
SVC_Stack_Size = 8;
USR_Stack_Size = 2048;

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: <stdio.h>, snprintf() not implemented?

Post by M.Heidemann » Wed 15. May 2024, 08:51

Hello!

Thank you for coming back to this after you found the solution, we appreciate it!

We try to keep our examples compact and only include dependencies that relate to the sample itself.

If you have any further questions, feel free to open up another topic.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

Locked