Problems debugging the library (c file)

Hey everyone,
hope you're all safe and sound (as possible) during these dark times.

  • Brief

I'm using the ArduinoModbus library and had no problem most of the time. However, recently, I tried to communicate with different hardware and the Arduino as the Slave/Server to have its input registers read. The code, Modbus RTU Server Kitchen Sink with little modifications, receives the message right (added print function to the .cpp) but doesn't the Slave/Server reply as it should.

(Slave/Server) Serial Monitor

21:23:06.865 -> Received: 2A040000000137D1 
21:23:06.902 -> RETURN: modbus_reply: 7

(Master/Client) Serial Monitor

Master send:2A040000000137D1
Master received:
  • Problem

Due to most past of the core code being in c I can't use Serial.print() to debug it and looks like print and printf doesn't work (at least not for me).

Have anyone managed to print something in the Serial Monitor with those two functions inside a .c file? What is the best way to debug those files?

  • Additional Information

  • SAMD based board

  • Arduino IDE 1.8.13

A couple possible alternatives to Serial.print() for debugging:

If you have a CMSIS-DAP debug probe, you can do true hardware debugging. The Arduino Pro IDE has support for this.

Use an LED to signal information.

Share a char buffer with the c++ world?

-jim lee

This seems to do what you want
(actually the suggestion above to share a char[] looks simplier) Well perhaps not as it does not print immediately, but a char[] could be used for store debug while in the method and be printed on exit.

CcodePrint.ino

#include "Cfile.h"

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  for (int i=10; i>0; i--) {
    Serial.print(i); Serial.print(' ');
  }
  Serial.println();
  dummyMethod();
}

void loop() {
  // put your main code here, to run repeatedly:

}

PrintSupport.cpp

#include "PrintSupport.h"

#include <Arduino.h>


void print(char* str) {
   Serial.println(str);
}

PrintSupport.h

#ifdef __cplusplus
extern "C" {
#endif
  void print(char* str);

#ifdef __cplusplus
     }
#endif

Cfile.h Your Modbus c file

#include "PrintSupport.h"

void dummyMethod() {
  char str[] = "test";

  print(str);
}

Cfile.h

#ifdef __cplusplus
extern "C" {
#endif
void dummyMethod();
#ifdef __cplusplus
}
#endif

Output is

10 9 8 7 6 5 4 3 2 1 
test

pert:
A couple possible alternatives to Serial.print() for debugging:

If you have a CMSIS-DAP debug probe, you can do true hardware debugging. The Arduino Pro IDE has support for this.

Use an LED to signal information.

Nice! I didn't know about the Arduino Pro IDE and I'll absolutely check it. ;p

jimLee:
Share a char buffer with the c++ world?

-jim lee

I've changed some functions parameters to receive a char* so I could print them outside the C functions, however as the code goes deeper the more functions I had to change, so I decided against it...

drmpf:
This seems to do what you want
(actually the suggestion above to share a char[] looks simplier) Well perhaps not as it does not print immediately, but a char[] could be used for store debug while in the method and be printed on exit.

[....]

Output is

10 9 8 7 6 5 4 3 2 1 

test

Thanks, i did what you told me and it worked like a charm! Just finished debugging and found the problem. I also used the method you taught me and created some nice printing functions :)!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.