2

I am working on programming an Arduino board in C, and thus cannot print unless I do serial communication to an external terminal window.

Because of this I have developed a printAll method:

void printAll(char * str) {
  int i;
  for(i = 0; str[i] != 0x00; i++) {
    PSerial_write('0', str[i]);
  }
}

I also have a variable:

char input[12];
input[0] = 'h';
input[1] = 'e';
input[2] = 'l';
input[3] = 'p';

What I would like to do is pass this array into the printAll method (but the printAll method takes a char*).

I have tried to do:

printAll(&input[0]);

But nothing gets printed! But when I step through and print each character of the input array I get:

help<0><0><0><0><0><0><0><0>

Can anyone explain why this is not working? Thanks!

***NOTE: the printAll method works perfectly fine when used like so:

printAll("Hello World!");

Overall my code looks like this:

char input[12];

int main(void) {
  start();
}

void start() {
  while(1) {
    printAll("Please enter in a command!\r");
    printAll("Please type 'help' for instructions!\r");
    char input[12];
    readInput(input);
    printAll("Trying to print the char array stuff....\r");
    printAll(input);
    if (input == "help") printHelp();
    else if (input == "set") {
      if (setStatus) printSet();
      else printAll("Controller not authorized to print.\n");
    }
    else if (input == "set on") setStatus = true;
    else if (input == "set off") setStatus = false;
    else if (input == "set hex=on") hexFormat = true;
    else if (input == "set hex=off") hexFormat = false;
    else if (input == "set tlow") tlow = getNumber(input);
    else if (input == "set thigh") thigh = getNumber(input);
    else if (input == "set period") period = getNumber(input);
    x_yield();
  }
}

void readInput() {
  char c = PSerial_read('0'); //reads character from user
  while (c != '\r') {
    //while the character isnt 'enter'
    input[currIndex] = c;
    c = PSerial_read('0');
    currIndex++;
  }
  int y;
  for(y = 0; y < 12; y++) {
    PSerial_write('0', input[y]);
    //go through input and print each character
  }
  PSerial_write('0', '\r');
  //add new line to print log
  currIndex = 0; //reset for next input (overwrites current stuff)
}

Right now no matter what I input, it just asks for more input, and never prints the array out after the input method returns.

dda
  • 6,030
  • 2
  • 25
  • 34
skyleguy
  • 979
  • 3
  • 19
  • 35

3 Answers3

2

The code that you've sent is mixed and does not compile. The code suggests that you have two input variables, one global and one local to main. readInput() reads the global one and printAll() the local one (or vice versa, depending on which code was changed). Remove the global input, that should not be used anyway and pass the same input variable to both readInput() and printAll().

hdante
  • 7,685
  • 3
  • 31
  • 36
  • yep that'll do it. idk how i missed that.thanks for the good eyes! – skyleguy Apr 22 '17 at 02:43
  • while I have you though... it correctly prints what I type in, yet if it matches the keywords in the if statement blocks, it doesnt execute them. Am I not using the right comparison to check equality between input and those string literals? JK I just did a quick search and found strcmp does the job – skyleguy Apr 22 '17 at 02:47
  • if (strncmp(input, "help", sizeof(input)) == 0) printHelp(); – hdante Apr 22 '17 at 02:50
  • 1
    Compile with "-Wall -Wextra" to see more warnings – hdante Apr 22 '17 at 02:50
1

Notice that the string literal "Hello World!" is a char[]. The fact that it prints corectly meanse that something else is wrong with your code. Please post the whole code you're trying to run.

Also, notice that char[] is converted automatically to char * when referenced. This is called array decaying into pointer. See:

Why do arrays in C decay to pointers?

So, both:

printAll(&input[0]);

And

printAll(input);

Do the same thing: they convert input to a char *.

EDIT: from your larger code I can at least see that you're not adding a '\0' character after reading and are not sending a '\r' when printing. String comparisons are also incorrect and should be done with strcmp().

Community
  • 1
  • 1
trollkill
  • 44
  • 3
  • edited it. This is all I have so far. I do have the methods made that the if/else if statements use but the code doesnt get that far so I see no need to post them yet – skyleguy Apr 22 '17 at 01:21
  • I've added some comments, but I suggest starting with a clean code and only defining the array and printing it. – trollkill Apr 22 '17 at 01:30
1

I don't know much about Arduinos, so bear with me here.

In your readInput function, you didn't include any parameters for the input array, which means that you're using the global variable declared at the beginning of your code in this function. However in your start function, you declare another input array and pass it to readInput, even though the function doesn't take any arguments.

This new input will actually "hide" the global input variable for the function it was declared in. This means that start uses this local input even though readInput uses the global input. In other words, your actually using two different variables, not one.

To fix the problem, try removing either the char input[12] in start, or remove the global char input[12] at the beginning of your code and add it as a parameter for readInput: void readInput(char * input).

Note: Even though you didn't include any parameters in the readInput, the C compiler won't complain if pass arguments unless you place void in the parenthesis: void readInput(void).

  • i did not know that about the c compiler! that'll be useful in the future thanks! – skyleguy Apr 22 '17 at 02:44
  • while I have you though... it correctly prints what I type in, yet if it matches the keywords in the if statement blocks, it doesnt execute them. Am I not using the right comparison to check equality between input and those string literals? JK I just did a quick search and found strcmp does the job – skyleguy Apr 22 '17 at 02:47
  • 2
    When you compare a string, you have to use the `strcmp()` function. Using the `==` operator will treat the array as a pointer. – Cerulean Chelonii Apr 22 '17 at 02:49