Using an external platform to develop apps

I have become very frustrated with trying to cope with learning to code in the C++ that the Arduino uses. Strings and chars and other issues. Could I create a simple sketch which would respond to commands from Visual Basic via a second serial port - all the VB program would need to do is send a command to set the state of a pin, and receive and respond to messages that a pin's state has changed. All the complicated logic could be done in VB, which I am familiar with after 30+ years of coding in it. Sorry to sound so negative, but I seem to be spending so much time trying to learn a new environment in my later years. Thanks in advance

You don't need to know C++ or even C for that matter, everything can be done with just the Arduino functions.
Have a look at the ReadASCIIstring example.
Look under Files-->Examples-->04.Communications

1 Like

Of course you could, but however you do it there will be a learning curve

Have you tried doing what you want using the Arduino IDE ?

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in https://forum.arduino.cc/c/development-tools/uncategorized/184.

Hi @ronb1949. There is a standardized protocol named Firmata which allow controlling an Arduino board via an application running on a host computer:

That is the original Firmata Arduino library. They later created another Arduino library named "ConfigurableFirmata", which I believe is a bit more advanced than the original:

The libraries include several Arduino sketches which can be uploaded to the Arduino board to cause the board to be controllable via the Firmata protocol. So this would allow you to use the Arduino board without having to write any C++ code.

There are multiple client libraries for various programming languages that make it easier to use the protocol in the PC application. There aren't any Visual Basic libraries on the list maintained by the Firmata project:

https://github.com/firmata/protocol#firmata-client-libraries

However, I did find a website where someone shares a Firmata library for Visual Basic .NET that they created:

I don't know enough about Visual Basic to be able to say whether this might be useful to you, but I think it is at least worth a look.

Thanks. I have been using Arduino for many years and have created several successful projects. This one - for controlling a model rail layout - is much more complicated. I am getting messages such as
" invalid conversion from 'const char*' to 'char' [-fpermissive]"
whatever that means. I want to call a function and pass it a string. It seems I can only do that by setting a char variable, declared before the setup function, to the string and then reading that variable in the function. Even that seems to be complicated. I am very familair with VB. It lets me have a statement like "Call mysub("Print this") and the sub will output "Print this" to the console. Not so in Arduino!

Rubbish !


void setup()
{
    Serial.begin(115200);
    printThis("Hello");
    char aVariable[] = "World";
    printThis(aVariable);
}

void loop()
{
}

void printThis(char* whatToPrint)
{
    Serial.println(whatToPrint);
}

My code:

char fnParam1[50];
fnParam1 = "Setup - sglLineState set to CL";
      setSigs_and_relay();
........
void   setSigs_and_relay();   
Serial.println(fnParam1);

Please post a complete but small sketch that illustrates the problem. The snippet that you posted raises more questions than it answers

1 Like

Here is a demo sketch and the Output:

char fnParam1[50];

void setup() {

  fnParam1 = "Setup - sglLineState set to CL";
  setSigs_and_relay();
}

void setSigs_and_relay() { 

Serial.println(fnParam1);
}

C:\Users\shrdl\ModelTramwayNew\Demo\Demo.ino: In function 'void setup()':
C:\Users\shrdl\ModelTramwayNew\Demo\Demo.ino:5:14: error: incompatible types in assignment of 'const char [31]' to 'char [50]'
fnParam1 = "Setup - sglLineState set to CL";
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

exit status 1

Compilation error: incompatible types in assignment of 'const char [31]' to 'char [50]'

is not valid in C/C++. Change this to

char fnParam1[] = "Setup - sglLineState set to CL";

or use strcpy()

char fnParam1[50];
strcpy(fnParam1,"Setup - sglLineState set to CL");

Thanks to all your help, I think I've finally got the hang of Arduino/C++ string issues. Beats me why it seems to be so difficult to cope with string variables in C. Anyway, here's my demonstration code - any comment would be most welcome (I had to spend several hours editing my original program):

char fnParam[4][45]; // an array of 4 string elements each with 45 characters
char var1 = 'a';    // a single-charcater variable

void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.println();
  Serial.print("var1 = ");
  Serial.println(var1);
  var1 = 'z';
  
  
  Serial.print("var1 = ");
  Serial.println(var1);
  
  strcpy(fnParam[0],"A parameter to the function");
  strcpy(fnParam[1],"Another parameter");

  dofunc();
}

void loop() {

}


void dofunc() { 
  Serial.println("running dofunc");
  Serial.println(fnParam[0]);
  Serial.println(fnParam[1]);
}

Whilst string handling in C/C++ can seem complicated it is very flexible and memory can be used very sparingly if you do it right, which is an advantage bearing in mind the memory constraints of most microprocessors

As to your code, despite what the strings say neither fnParam[0] or fnParam[1] are actually parameters passed to the dofunc() function. They are actually global variables and are available without being passed to the function

See the code in post #8 for an example of how to pass a string parameter to a function. Written properly you could avoid the need to do the strcpy() and pass the string directly to the function which makes the use of the function more flexible

char var1 = 'a';      // a single-charcater variable

void setup()
{
    Serial.begin(115200);
    delay(2000);
    Serial.println();
    Serial.print("var1 = ");
    Serial.println(var1);
    var1 = 'z';

    Serial.print("var1 = ");
    Serial.println(var1);

    dofunc("A parameter to the function", "Another parameter");
		dofunc("A third parameter", "yet another parameter");
		
}

void loop()
{
}

void dofunc(char *p1, char *p2)
{
    Serial.println("\nrunning dofunc");
    Serial.println(p1);
    Serial.println(p2);
}

Thanks. I'll have a look at post no. 8,

Look at #13 too for a direct comparison with your sketch

I copied this example from a previous diescussion:

char *LibrosA[] = {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};
const byte NAMES_IN_USE = 5;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  for (byte i = 0; i < NAMES_IN_USE; i++)
  {
    Serial.println(LibrosA[i]);
  }

  while (1) {} // endless loop to stop program
}

The post says it should output:
Libro 1
Libro 2
Libro 3
Libro 4
Libro 5

but I get
C:\Users\shrdl\ModelTramwayNew\CharExample\CharExample.ino:1:73: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
char LibrosA[] = {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};
^
C:\Users\shrdl\ModelTramwayNew\CharExample\CharExample.ino:1:73: warning: ISO C++ forbids converting a string constant to 'char
' [-Wwrite-strings]
C:\Users\shrdl\ModelTramwayNew\CharExample\CharExample.ino:1:73: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\shrdl\ModelTramwayNew\CharExample\CharExample.ino:1:73: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\shrdl\ModelTramwayNew\CharExample\CharExample.ino:1:73: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

This C++/Arduino stuff is driving me mad! The doco doesn't seem to help

I just need an array that holds several strings like the above- it can't be that hard!
Sorry about the ranting but I've spent several days now trying to undo the effects of initially using String.
Thanks.

When you do this

char *  LibrosA[] = { "Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5" };

the compiler allocates space to save the strings pointed to but there is a chance that later in the sketch you might change the strings and make them longer, hence the warning messages

However, if you do this

const char *  LibrosA[] = { "Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5" };

then the compiler knows that the strings cannot be changed later so no warnings are issued

Try it and see

Thanks, but I DO need to change the strings later!

Why do you need an array of strings rather than individual variables or even no variables at all ?

Your VB example

does not use a variable so why use one in your sketch ?

I am really struggling with this.

#include <string.h>
void setup()
{
  Serial.begin(9600);
  delay(5000);
  
  Serial.println("here2");
  Serial.println(memcmp("ABC\0", "B\0",30));
  Serial.println(strcmp("ABC\0", "B\0"));
}

void loop()
{
}

This simplest program gives the following output:
here2
-1
-1

I would expect the result to be 2 each time, or 1 if the first characters are 0.
I've changed from using String because I was getting corrupted variables. I thought using char would fix it. What am I doing wrong?
Thanks