arraysarduinoxbee

Passing string to uint8_t array in arduino


I have to send an integer counter as 'num' through XBee (in API mode) from Arduino. For this purpose I have to convert this integer to String and then to uint8_t array as it is needed in API frame to transmit.

Apart from other details I've converted my integer to string like

  String str;
  int num = 0;
  char  cstr[16];

void loop() {
  num++;
  str = String(num);
  str.toCharArray(cstr,16);

  Serial.println(cstr);  // this shows the correct result, means int is converted to String correctly

  uint8_t data[] = {cstr}; // passing String to uint8_t array

  XBeeAddress64 addr64 = XBeeAddress64();
  addr64.setMsb(0x00000000);
  addr64.setLsb(0x00000000); 
  ZBTxRequest zbTx = ZBTxRequest(addr64, data, sizeof(data));
  xbee.send(zbTx);

  int count = sizeof(data);
    for (int i = 0; i < count; i++) {
      if (i == (count-1)) {
      Serial.print(data[i]);   // here it prints "104" of ASCII which is equal to "h"       
      }
    } 
}

It also transmit it as like that Serial print i.e. ('104'). Please point out if I am making any mistake and guide me if I didn't do it as required.


Solution

  • First get rid of those cstr and String:

     int num = 0;
     //char  cstr[16];
      char data[16] = {'\0'}; // Initialize data array 
    
    void setup() {
    }    
    void loop() {
    
     num++;
     //str = String(num);
    //str.toCharArray(cstr,16);
     itoa (num, data, 10); 
    
      Serial.println(data); 
      // To get the number of elements in the array use
      uint8_t datalen = strlen(data);
      // Black box see lib for details
      XBeeAddress64 addr64 = XBeeAddress64();
      addr64.setMsb(0x00000000);
      addr64.setLsb(0x00000000); 
      ZBTxRequest zbTx = ZBTxRequest(addr64, data, datalen);
      xbee.send(zbTx);
     // Print test
      Serial.println("Content of data: ");
      Serial.print(data);
      Serial.print(" datalen: ");
      Serial.print(datalen);
    }
    

    This should work as you expect.
    To copy an array you would use strcpy (or memcopy with malloc and free), to get the number of elements use strlen.

    EDIT: Here is a tested code (just copy andpaste and first replace only the include to your xbee lib:

       #include "Xbee.h"    //-> Change to your xbee lib   
    
      uint16_t num = 0;
     char xdata[16] =  {'\0'};// Initialize data array
     char numbuf[16] =  {'\0'}; // initialize number buffer
     long currentMillis = 0;
    
     void setup() {
     Serial.begin(115200);
     currentMillis = millis();
    
    }
    void loop() {
    
     if (millis() - currentMillis > 1000) { // 1 sec timer to prevent overflow due to hammering xbee with data -> tune to your stable transmission
       num++;
       itoa (num, numbuf, 10);     // Num buffer 
       strcpy(xdata, numbuf);      // Copy content of num buffer to data array 
       Serial.println(xdata);
    
     // To get the number of elements in the array use
       uint16_t datalen = strlen(xdata); 
       // Black box see lib for details
         XBeeAddress64 addr64 = XBeeAddress64();
        addr64.setMsb(0x00000000);
        addr64.setLsb(0x00000000);
        ZBTxRequest zbTx = ZBTxRequest(addr64, data, datalen);
        xbee.send(zbTx);
       // Print test
       Serial.print("Content of xdata: ");
       Serial.print(xdata);
       Serial.print(" datalen: ");
       Serial.println(datalen);
       currentMillis = millis();   // reset timer
     }
    }
    

    The result in serial monitor is:

     1
     Content of xdata: 1 datalen: 1
     2
     Content of xdata: 2 datalen: 1
     3
     Content of xdata: 3 datalen: 1
     4
     Content of xdata: 4 datalen: 1
     ....
     864
     Content of xdata: 864 datalen: 3
     865
     Content of xdata: 865 datalen: 3
     866
     Content of xdata: 866 datalen: 3
    

    So all works as expected, no 104 or similar. Please first copy paste - test it - and then your additions