Go Down

Topic: Converting String to const char*  (Read 13421 times) previous topic - next topic

nonlinearmind

It's not going well.  I want to be able to send "strings" of letters and numbers.  You've told me not to use strings and I've been asking from the beginning, how do I convert "strings" of characters into something that I can send.  I've asked that more than once.  

gfvalvo

So take a look at the file you just specified:
https://github.com/RedBearLab/nRF8001/blob/master/src/RBL_nRF8001.cpp

You'll see this:

Code: [Select]
void ble_write(unsigned char data)
{
    if(tx_buffer_len == MAX_TX_BUFF)
    {
            return;
    }
    tx_buff[tx_buffer_len] = data;
    tx_buffer_len++;
}

void ble_write_bytes(unsigned char *data, uint8_t len)
{
    for (int i = 0; i < len; i++)
        ble_write(data[i]);
}



Now you know. You can supply either a 'char' or a 'char *' and a 'uint8_t' string length.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

AWOL

It's not going well.  I want to be able to send "strings" of letters and numbers.  You've told me not to use strings and I've been asking from the beginning, how do I convert "strings" of characters into something that I can send.  I've asked that more than once.   
No, we've told you not to use Strings.

nonlinearmind

When you say "look at reply #7, it isn't helpful, without elaboration, when in reply #8, I show where I tried it and I still get a conversion error.  Though it is :

 
SimpleChat:45: error: invalid conversion from 'const char*' to 'char'



gfvalvo

#19
Dec 17, 2016, 12:50 am Last Edit: Dec 17, 2016, 12:57 am by gfvalvo
You didn't try what I suggested. You forgot the '[]'. Read it again.

But, it doesn't matter. You can see from the code snippet I just posted what the parameters need to be.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

AWOL

So, despite what you wrote, that isn't the same error message as in the original post, is it?
Why don't you post your code and the error message you got?

nonlinearmind

Ok, thanks gfvalvo... that helps.   But I still don't know how to "prepare" a string of data so that it can be sent via one of those functions.  Earlier I had tried doing this:

Code: [Select]
void transmit(String testString){uint8_t sendbuffer[20];
          testString.getBytes(sendbuffer, 20);
           char sendbuffersize = min( 20, testString.length());
      //I also tried:      uint8_t sendbuffersize = min( 20,testString.length());

          Serial.print(F("\n* Sending -> \"")); Serial.print((char *)sendbuffer); Serial.print("        ");Serial.print(sendbuffersize);Serial.println("");

          // write the data
    ble_write_bytes(sendbuffer, sendbuffersize);}


And it worked, but wasn't truncating the data once it reached the buffer size.  It bunched consecutive "strings" together like this:

testing1234testingAB
CDtesting5678testing
EFGH


rather than:

testing1234
testingABCD
testing5678
testingEFGH


So that's where I am with it right now and where I'm stuck.  

AWOL

Can you see the difference between "string" and "String"?

It would help if you posted your code.

I think this has been mentioned.

nonlinearmind

gfvalvo  Honestly, I tried it with [] and it didn't work and I thought that it might be where my variable name belongs, so I tried that.  Sometimes stuff that seems obvious to folks who have been doing this for years isn't to those of us who haven't.   I'm not asking for anyone to write my code for me or anything, but with little context to go on beyond a sentence or two, it leaves me left trying all sorts of things.  

nonlinearmind

You keep telling me to post my code and I have all along the way.  In my first post, I posted my original code.

Code: [Select]

#include <SPI.h>
#include <boards.h>
#include <RBL_nRF8001.h>

void setup()
{  
  // Default pins set to 9 and 8 for REQN and RDYN
  // Set your REQN and RDYN here before ble_begin() if you need
  //ble_set_pins(3, 2);
  
  // Set your BLE Shield name here, max. length 10
  //ble_set_name("My Name");
  
  // Init. and start BLE library.
  ble_begin();
  
  // Enable serial debug
  Serial.begin(57600);
}

unsigned char buf[16] = {0};
unsigned char len = 0;

void loop()
{
  if ( ble_available() )
  {
    while ( ble_available() )
      Serial.write(ble_read());
      
    Serial.println();
  }
  
  if ( Serial.available() )
  {
    delay(5);
    
    while ( Serial.available() ){
    
      //////This line from the example works
         ble_write( Serial.read() );

    //////These two lines of mine don't
        String testString = "test content";
        
        ble_write( testString );
    //////////////
  }
  
  }
  
  ble_do_events();
}


 Then in my second post I posted code from an attempt to convert my strings into chars and explained what I was getting.  

Code: [Select]


transmit("testing1234");

transmit("testingABCD");

transmit("testing5678");

transmit("testingEFGH");



void transmit(String testString){uint8_t sendbuffer[20];
          testString.getBytes(sendbuffer, 20);
           char sendbuffersize = min( 20, testString.length());
      //I also tried:      uint8_t sendbuffersize = min( 20,testString.length());

          Serial.print(F("\n* Sending -> \"")); Serial.print((char *)sendbuffer); Serial.print("        ");Serial.print(sendbuffersize);Serial.println("");

          // write the data
    ble_write_bytes(sendbuffer, sendbuffersize);}

 

Then I posted the same function I was trying to get working a second time in reply 21.  I posted a link to the library in 13.  Please be specific about what code I need to post and I will.


gfvalvo

#25
Dec 17, 2016, 02:51 am Last Edit: Dec 17, 2016, 03:07 am by gfvalvo
You should post the COMPLETE sketch of the version that almost works the way you want. Heck, it compiles and actually gives pretty much the output you're looking for. Your other version won't even compile!!! Without the full sketch, I can't see exactly what you're doing. I'm not at all familiar with this library, but you should probably call ble_do_events() after EVERY call to transmit().

Next, ditch the use of String type all together. The message that we've been trying to get across is that String != string. One is C++ and very complicated. The other is c and is just a char [] (or char * if you prefer).

Your transmit() function becomes:

Code: [Select]
void transmit(char *xmitString) {
  ble_write_bytes(xmitString, strlen(xmitString));
}

I'm not sure why your transmitted strings are being concatenated. Maybe you just need line breaks in them. Don't know what the receiving device wants try ending them with one of:

"testing1234\n"
"testing1234\n\r"
"testing1234\r\n"

Also, if you look in RBL_nRF8001.cpp, you'll see:

Code: [Select]
#define MAX_TX_BUFF 64


So, don't try to send a string longer than that (including line breaks and \0 NULL terminator).

Finally warning: I don't have your hardware or library installed. So, this is all just guidance. You might have to do some modifications.


No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

gfvalvo

Code: [Select]
#define MAX_TX_BUFF 64
So, don't try to send a string longer than that (including line breaks and \0 NULL terminator).

Actually, I was wrong. I don't think strlen() counts the \0 NULL terminator.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Go Up