convert string into function

Hello guys,
Im currently creating a HMI wich allow control of the arduino card by the PC thanks to Visual Studio (c#).
My current problem is that Im sending a message to the arduino on the serial port with Visual Studio,
this message is well received by the arduino and Im able to stack it into a string :

string dir;
[…]

if(test[0]==‘D’ && APC==1)

{

dir=Serial.readStringUntil(’\n’);

dir();

}

[…]

The string is the exact same stuff I wanted to have,

but I dont know how to convert that string into a function…

I dont know how to explain it but I created functions on a .h + .cpp wich got the exact same names that the messages I sends and I want those messages to launch the functions without using a thousands if.

example of what I did in pics…

1.PNG

2.PNG

3.PNG

4.PNG

You can’t convert either a String or a string into a function.

You can call a function based on the value of a string, but I wouldn’t even consider using a String.

That's exactly what I want to do, Using the value of my strings to call my function wich is in the .h & .cpp. But I have no idea how to do it...

if (strcmp (inString, "dir") == 0) {
  dir ();
}

I get it, that still force me to use a thousands if. Thanks anyway, I'll try to figure out another simple way to do it.

augustingarel:
I get it, that still force me to use a thousands if.

No just one, in a for loop.
Just don’t forget to break when you find it !

Maybe a binary search?

I dont get it,
At first my idea was to find the good function to “launch” in my .h basing on the message I receive.
I’ll have literraly hundreds maybe thousands of functions in my .h (pic n°2)
and i want to start the good one with the message I receive (pic n°1)

2.PNG

3.PNG

C/C++ (except possibly for very rare interpreted versions) is a compiled language, which means that names like ‘dir’, ‘lecturedef’, ‘effacementdef’ and so on, that you have declared in your program, are stripped away and converted to machine addresses at compile time. They are not available to the program in any way, shape or form at run time. There are some alternative suggestions posted that you have to investigate, because your approach is simply not possible.

An array of structs, each struct consisting of a string, and a function pointer.

Also useful in this context, is the preprocessor’s stringify operator, #

I dont know how to convert that string into a function...

The Arduino does not need to receive the actual name of a function or even any text. What the user sees and selects in the HMI on the PC need not have any resemblance to the command sent to and acted upon by the Arduino apart from being unique

For instance, a user could select the dir command, the PC could send say 99 and on receipt of 99 the Arduino could execute the dir command. That way all you need on the PC is an array of function pointers indexed by the function number, but my mind boggles at the thought of there being thousands of them

UKHeliBob: For instance, a user could select the dir command, the PC could send say 99 and on receipt of 99 the Arduino could execute the dir command. That way all you need on the PC is an array of function pointers indexed by the function number, but my mind boggles at the thought of there being thousands of them

or even a switch case statement...

Anyway, the thing is that in any case I will have as much if/case/any stuff than function...

We could be more helpful if you told us what you are actually planning to use this feature for.

augustingarel:
Anyway, the thing is that in any case I will have as much if/case/any stuff than function…

No. See above.