Go Down

Topic: send array values from CPP to main loop  (Read 68 times) previous topic - next topic

bvlet

Hi!
I've been working on my Arduino skills lately and I must say that I don't disappoint myself. However, now I ran into a problem I don't manage to solve.

I'm receiving modbus messages over an ESP8266. So far so good. Then I translate the Hex codes to ASCII and then a string. Also no problem.
But here it comes: The function that I can use to send the received text to the serial monitor, runs in the ModbusTCP.CPP file.


Code: [Select]
void ModbusTCP::printMB(char *s, int n) {

  int i;

  Serial.print(s);
  for (i = 17 ; i < n;) {
    Serial.write(mb_adu[i+1]); 
    Serial.write(mb_adu[i]);
 
    i=i+2;
  }
}


My OLED display is being controlled from the main (void) loop like this:

Code: [Select]
void loop(void)
{
  // Process MODBUS requests on every scan
  m.run();

    display.clearDisplay();
    delay(10);
    display.setFont();
    display.setTextSize(2);
    display.setCursor(0,20);
    display.println("MODBUS values");
    display.display();
}


So now I need a way to take the values that I can display in the serial monitor to  the main sketch, where I can then prnt them on the OLED display.

Does anyone have a clue how this is possible?
I already tried to make the array mb_adu a global variable, but that doesn't work...

Thanks in advance for your help!

 

pert

Create the buffer in your sketch then pass it to the printMB function, which puts the message in that buffer:
Code: [Select]
void setup() {
  Serial.begin(9600);
  while (!Serial) {}
  char buffer[8];
  fillBuffer(buffer);
  Serial.println(buffer);
}

void loop() {}

void fillBuffer(char buffer[]) {
  strcpy(buffer, "message");
}

Go Up