Go Down

Topic: char concat.. a stupid question... (Read 560 times) previous topic - next topic

aq_mishu

guys
i know it is a totally stupid question... but i'm stuck here...

Code: [Select]
void bmsCommunication() {
 
    // wait for a new client:
    EthernetClient client = server.available();
    memset(socketString, 0, sizeof(socketString));
    // when the client sends the first byte, say hello:
    if (client) {
      if (!alreadyConnected) {
        // clear out the input buffer:
        client.flush();
        DEBUG_PRINT(F("New Session Estublished!"));
        //Serial.println(client.remoteIP());
        client.write("ACK");
        alreadyConnected = true;
      }
 
      // Read a line at a time
      memset(socketString, 0, sizeof(socketString));   // Clear contents of Buffer
      boolean lineEnd = false;
      while (!lineEnd) {
        while (client.available() > 0) {
          char c = client.read();
          if (c != '\n' && c != '\r') {
            strcat(socketString, c);
            //socketString.concat(c);
          }
          if (c == '\n') {
            lineEnd = true;
            client.flush();
          }
         }
       }
       
      DEBUG_PRINT("Remote said:");
      DEBUG_PRINTLN(socketString);
      if (strcmp(socketString, "ping") == 0) {
      //if (socketString == "ping") {
        server.print(F("<"));
        for(int i=0; i<10; i++){
          server.print(sbcStrRS232_tx[i], DEC);
          //Serial.print(F(";"));
        }
        server.println(F(">"));
       
        //server.print(outputReport());
        DEBUG_PRINT("I said:");
        DEBUG_PRINTLN(outputReport());
      }
    }
}


this is the function i am trying to use... earlier this was made of String and a perfect usable one... now I have converted the socketString in to a char. hence i need to read the data from ethernet to this, so that I can then do analysis.

Also The part after ping, I'm actually confused. All I need there is to send a <1000000> type data, where the data will be generated from a bunch of char data in another function and then it needs to be a single data[] and then needs to be sent to a vb.net socket program.

ideas??


TonyWilk


I'll bet that code does not compile.

Explain the error(s) you get and post the full code please


Yours,
  TonyWilk

BulldogLowell

Code: [Select]
// Read a line at a time
      memset(socketString, 0, sizeof(socketString));   // Clear contents of Buffer
      boolean lineEnd = false;
      while (!lineEnd) {
        while (client.available() > 0) {
          char c = client.read();
          if (c != '\n' && c != '\r') {
            strcat(socketString, c);
            //socketString.concat(c);
          }


you also seem to have nothing preventing you from writing past the end of the socketString array.

aq_mishu

I'll bet that code does not compile.

Explain the error(s) you get and post the full code please


Yours,
  TonyWilk

that compiles... but non responsive... i  mean vb app tries to send and nothing happens... :-p


Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

TonyWilk

Hmm, that's odd.

The compiler only gives a warning:
Quote
warning: invalid conversion from 'char' to 'const char*' [-fpermissive]

   strcat( mystr, c );
but does complete and downloads. Who knew ?


Anyway...  strcat() is a function for concatenating two strings NOT a character to a string.

Yours,
  TonyWilk

aq_mishu




man, all i wanna do is:

while (client.available() > 0) {

}

here it needs to read and put the things in a char array [say char readStream[13] ] and that should be until a VBCRLF found (means \n). Once I can do that, using char (not string as I found like others, string is the thing to be avoided), then I can read the command "ping" and then can send the "pong".


Now, the pong is basically a bunch of chars again, data[0] till data [10] and that needs to be made as data[] = <10000000>

This I can do using serial, but can't do using ethernet and morely, i need to send a single string, not a stream... [since it is ethernet, not serial where i cound do "until \n" ]


jremington

#7
Jan 17, 2018, 10:09 pm Last Edit: Jan 17, 2018, 10:10 pm by jremington
Here is one way to append a character to a C string:

Code: [Select]

 int n=strlen(socketString);
 socketString[n++] = c; //add character to end
 socketString[n]=0; //terminate extended string
 

TonyWilk


This will add a character to a string buffer, it will also make sure you don't run off the end of the buffer

Code: [Select]

void strcat_c( char *dest, int destlen, char c )
{
  int len= strlen(dest);
  if( len < (destlen-1) ){
    dest[len++]= c;
    dest[len]= '\0';
  }
}

// e.g.
  strcat_c( SocketString, sizeof(SocketString), c );



Yours,
  TonyWilk

ah... looks like jremington beat me to it there :)

aq_mishu

Sorrry, can you help me by writing this in a whole?? I've been working for last 48hr in a vb.net with struggling in serial read and then switching to ethernet to solve a pathetic issue. and head is blocked... time is running short...

May you plz...  a piece of code??


jremington

Quote
This parhaps??
Not quite. You really should check for the end of the "socketString" buffer, as suggested in reply #8, otherwise your program will eventually crash.

aq_mishu

This will add a character to a string buffer, it will also make sure you don't run off the end of the buffer

Code: [Select]

void strcat_c( char *dest, int destlen, char c )
{
  int len= strlen(dest);
  if( len < (destlen-1) ){
    dest[len++]= c;
    dest[len]= '\0';
  }
}

// e.g.
  strcat_c( SocketString, sizeof(SocketString), c );



Yours,
  TonyWilk

ah... looks like jremington beat me to it there :)

hi, the "parhaps" was posted before i saw updated reply...

Now, if this above code is okey, then how can i fit it in my case??? [I understand, but brain is clogged]

TonyWilk

Now, if this above code is okey, then how can i fit it in my case???
It's golden; just replace your original

strcat(socketString, c);

with

strcat_c( socketString, sizeof(socketString), c );


And when you get the next problem... POST THE WHOLE CODE !

Yours,
  TonyWilk

aq_mishu

OKey~~~ So I am an idiot... a stupid!! I was spending the whole night on coding and then in the morning my wife discovered that the arduino shield's RJ45 was not connected  and I'm struggling with the code...

Anyway, that function i hope works... (should be, logically, dont see any reason for NOT)

Now comes the second part... I need to construct the final array as <100110110> type something. my code can do it for Serial output. But for ethernet, i need it... and that should give a single throw shot... I'm attaching my codes here... So you guys will be able to get it...

Code: [Select]
void outputReport(){
  sbcStrRS232_tx[0] = 1;
  if(askedToOpen){sbcStrRS232_tx[1] = 1;} else{sbcStrRS232_tx[1] = 0;}
  if(isOpening){sbcStrRS232_tx[2]= 1;} else{sbcStrRS232_tx[2] = 0;}
  if(isOpened){sbcStrRS232_tx[3] = 1;} else{sbcStrRS232_tx[3] = 0;}
  if(askedToClose){sbcStrRS232_tx[4] = 1;} else{sbcStrRS232_tx[4] = 0;}
  if(isClosing){sbcStrRS232_tx[5] = 1;} else{sbcStrRS232_tx[5] = 0;}
  if(isClosed){sbcStrRS232_tx[6] = 1;} else{sbcStrRS232_tx[6] = 0;}

  Serial1.print(F("<"));
  for(int i=0; i<10; i++){
    Serial1.print(sbcStrRS232_tx[i], DEC);
    //Serial.print(F(";"));
  }
  Serial1.println(F(">"));
}


I can call it whenever i need when i need serial output. But for ethernet output, I cant do it...

Code: [Select]
if (strcmp(socketString, "ping") == 0) {
      //if (socketString == "ping") {
        server.print(F("<"));
        for(int i=0; i<10; i++){
          server.print(sbcStrRS232_tx[i], DEC);
          //Serial.print(F(";"));
        }
        server.println(F(">"));
       
        //server.print(outputReport());
        DEBUG_PRINT("I said:");
        outputReport();
        //DEBUG_PRINTLN(outputReport());
      }


Note: The code is now working, the issue is I need to send it in a single shot. And always I wish to avoid "String class"...

PaulS

Quote
the issue is I need to send it in a single shot.
"it" is a pronoun, and appears to refer to the noun "code". That does not make sense. What is "it" that you need to "send in a single shot"?
The art of getting good answers lies in asking good questions.

Go Up