How to combine number of strings in a unsigned char array

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want to combine multiple strings into an array and then send it over serial. 

I have following method to send data.

 puts("AT+CWJAP="); //put the string over serial port (in Codevision AVR)
 putchar('"');  
 putchar(SSID[0]); 
 putchar(SSID[1]); 
 putchar(SSID[3]);
 putchar(SSID[4]);
 putchar(SSID[5]);
 putchar(SSID[6]);
 putchar(SSID[7]);
 putchar(SSID[8]);
 putchar(SSID[9]); 
 putchar('"');
 putchar(','); 
 putchar('"');

I have SSID char type array contains my router AP name, which I Put from Keyboard on graphics display. see image 

SSID Entetrd using keyboard

 

I want all things into a string like : 

string send= "AT+CWJAP="+ee_SSID+","+ee_PASSWORD+"\r\n"; // send all at once

 

I tried puts(ee_SSID); but that doesnt work well it repeats words twice or more

 

 

the response on serial terminal is

 

 

Last Edited: Fri. Dec 29, 2017 - 09:27 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you looked at printf() or sprintf()?
There’s other standard C library functions like strcat() and strcpy()

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm not sure you want puts because of the newline.

 

Look see (copied & pasted from cplusplus.com):

int puts ( const char * str );

Write string to stdout

Writes the C string pointed by str to the standard output (stdout) and appends a newline character ('\n').

The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This terminating null-character is not copied to the stream.

Notice that puts not only differs from fputs in that it uses stdout as destination, but it also appends a newline character at the end automatically (which fputs does not)

 

Oh! and you really ought to show us the definition of ee_SSID & ee_PASSWORD and judging by their variable names show us how they are initialised also. I suspect there may be a zero termination missing.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok. I am not using \r \n at end or nothing else like. and same function is used which is used for SSID. But then why password is repeated. But quotation is not repeated . And there is no loop or repeatation in function. I think it is because of array Password is used. But then same array type and length is used for ee_SSID and it is not repeated as you can see in image above

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You've shown us the code used to print the first bit of the message. Which incidently has a bug: putchar(SSID[2]);  is missing. The terminal shows REIANC instead of RELIANC. Also SSID is only 7 chars, yet you print 9 (or after bugfix, will print 10)

 

Show us the code for outputting the remainder of the message, along with the definition of ee_SSID & ee_PASSWORD and how they are initialised.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh... thanks that's why L is not printed.
I will pos the code soon.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For tiny chips like this, I would strongly recommend against using the "string" type, which implies a kind of data allocation and processing which is a really bad fit for something like AVR.

 

Look at printf.

 

printf("AT+CWJAP=%s,%s\r\n", ee_SSID, ee_PASSWORD);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hera is my  set wifi function code:

 

//definition of arrays:------------------
 char ee_SSID [10];  
 char ee_pass [10];
 char SSID[10];
 char PASS[10];
//------------------------------------------------------------

void set_wifi( )
{     unsigned char SrlCntr;
      glcd_clear();
    delay_ms(5);

    glcd_rectround(0,0,128,13,1); //x,y position (start), Width, Hight  FOR TITLE
     //glcd_line(93,14,93,64); //start_pos, Y_start, End_Pos, Y_Start  

     glcd_outtextxy(2,3,"<- Wi-Fi Setup");
     timer_en=1;
     glcd_rectangle(0,53,128,93); //x,y position (start), Width, Hight   FOR error msg
     glcd_outtextxy(100,55,"Exit");  //
    parameter_changed=0;
    scancode1=0;
    Y=17;
    glcd_putimagef(2,17,menu_pointer,GLCD_PUTCOPY);    // pointer
    glcd_outtextxy(9,17,"SSID: ");
    glcd_outtextxy(9,28,"PASS: ");
    get_Serial();
   for (SrlCntr=0;SrlCntr<10;SrlCntr++)
            {
            SSID [SrlCntr] = Serial_No [SrlCntr] ;
            }
    scancode1=0;
    deblank1(SSID);  // used to remove blank spaces in string

   Y=28;
    glcd_outtextxy(2,17," ");    // degree img
    glcd_putimagef(2,Y,menu_pointer,GLCD_PUTCOPY);    // pointer img
    glcd_outtextxy(9,Y,"PASS: ");
    get_Serial();
    for (SrlCntr=0;SrlCntr<10;SrlCntr++)
            {
            PASS [SrlCntr] = Serial_No [SrlCntr] ;
            }
     deblank2(PASS); // used to remove blank spaces in string

      glcd_outtextxy(2,28," ");
   glcd_putimagef(2,39,menu_pointer,GLCD_PUTCOPY);    // pointer img
   glcd_outtextxy(9,39,"Connect: ");
   on_off=0;
   ask_yes_no(); // ask for connect

 if(yes_no)//yes
  { 

    puts("AT+CWJAP="); putchar('"');  putchar(ee_SSID[0]); putchar(ee_SSID[1]); putchar(ee_SSID[2]); putchar(ee_SSID[3]);putchar(ee_SSID[4]);putchar(ee_SSID[5]);
    putchar(ee_SSID[6]);putchar(ee_SSID[7]);putchar(ee_SSID[8]);putchar(ee_SSID[9]); putchar('"');putchar(','); putchar('"');  // now added putchar(ee_SSID[2]);

    putchar(ee_pass[0]); putchar(ee_pass[1]); putchar(ee_pass[2]); putchar(ee_pass[3]); putchar(ee_pass[4]); putchar(ee_pass[5]); putchar(ee_pass[6]);
    putchar(ee_pass[7]); putchar(ee_pass[8]); putchar(ee_pass[9]); putchar('"');  puts("\r\n");

  }

    if (rx_counter>3)
    {
     glcd_clear();
     delay_ms(5);
     glcd_rectround(0,0,128,13,1); //x,y position (start), Width, Hight  FOR TITLE

     glcd_outtextxy(2,3,"<- Wi-Fi Setup");
     timer_en=1;
     glcd_rectangle(0,53,128,93); //x,y position (start), Width, Hight
     glcd_outtextxy(100,55,"Exit");
     glcd_outtextxy(9,17,"RX: ");

            exit_menu=0;
            menu_exit = exit_menu_delay * 2;
            received_char=0;
            received_char=getchar();
            glcd_putchar(received_char);

   } 

}

 

Last Edited: Tue. Dec 26, 2017 - 08:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

after modifying if (yes_no) statement body

if(yes_no)//yes
  { 

    puts("AT+CWJAP="); putchar('"');  putchar(ee_SSID[0]); putchar(ee_SSID[1]); putchar(ee_SSID[2]); putchar(ee_SSID[3]);putchar(ee_SSID[4]);putchar(ee_SSID[5]);
    putchar(ee_SSID[6]);putchar(ee_SSID[7]);putchar(ee_SSID[8]);putchar(ee_SSID[9]); putchar('"');putchar(','); putchar('"');  // now added putchar(ee_SSID[2]);

    putchar(ee_pass[0]); putchar(ee_pass[1]); putchar(ee_pass[2]); putchar(ee_pass[3]); putchar(ee_pass[4]); putchar(ee_pass[5]); putchar(ee_pass[6]);
    putchar(ee_pass[7]); putchar(ee_pass[8]); putchar(ee_pass[9]); putchar('"');  puts("\r\n");

  }

i changed it to

 if(yes_no)//sensor off
{ 
 
 printf("AT+CWJAP=%s,%s\r\n", ee_SSID, ee_pass) ;   

}

and the result is:

 

output:

 

password is still repeated twice using printf statement, same as puts and putchar. why ee_pass is get filled twice? and how to add " in print f statement. my complete string is:

AT+CWJAP="ee_SSID","ee_pass"

How to print  " quotation mark on serial if i used puts or printf function? i cant only send it using putchar ('"'); 

 it is part of string.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To print double quote character you must "escape" it like this.

if(yes_no) { //sensor off
    printf("AT+CWJAP=\"%s\",\"%s\"\r\n", ee_SSID, ee_pass);
}

OK. You showed the definition of arrays:

//definition of arrays:------------------
 char ee_SSID [10];  
 char ee_pass [10];
 char SSID[10];
 char PASS[10];
//------------------------------------------------------------

But unfortunately there is still no code for showing us how these strings get filled up. Until that appears, no-one but the ultra clairvoyant can help.

 

If you don't want to post all that code or cannot, then you'll have to find the bug on your own. You could start your own bug search by printing ee_SSID & ee_pass to the LCD via  glcd_outtextxy(0, 0, ee_SSID); and glcd_outtextxy(0, 0, ee_pass); or similar.

 

---

BTW: What Simulation Software is that ?

I will be made redundant on 31-Dec and will have plenty of time for playing with such things.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:

BTW: What Simulation Software is that ?

 

Looks like Proteus from Labcentre

 

N.Winterbottom wrote:

I will be made redundant on 31-Dec and will have plenty of time for playing with such things.

 

Bugger.

"This forum helps those that help themselves."

"How have you proved that your chip is running at xxMHz?" - Me

"If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, it's proteus from labcentre. And thanks for help. I will show the code to fill array. Sorry for late reply.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But my question is still unsolved. Same function is used to fill array. Then why ee_pass array is filled twice and ee_SSID not?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Without seeing the code that actually fills in the arrays, it's really hard to tell. The most likely answer, I think, is that the arrays start out with junk in them, or end up with that junk getting into them at some other point, and then the "fill" just writes in a few values but doesn't actually nul-terminate the array, so it just keeps printing. The %s specifier in printf will print until it finds a nul byte. Possibly try

 

memset(ee_pass, '\0', 10);

 

before you do the thing which is supposed to fill it, and see if that helps. If it doesn't, then the thing filling it is writing the wrong bytes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is code for filling array and removing blank spaces in string and terminating the string

 


void get_Serial(void)
{    unsigned char cntr;
     sens_set=0;Serial_count10=0;
     ee_Serial_count=10;
     for (cntr=0; cntr<10;cntr++)
                 {Serial_No[cntr]=' ';}
        Serial_count=10;
        sens_set=0;
        kbhit=0;
        scancode1=0;
        dly_over=1;
        first_time=1;
        menu_exit=exit_menu_delay;

 //---------------NEW ADDED--------------------------

     do{

                 first_time=1;
               if(sens_set==0)
               {
                     glcd_putcharxy(53,Y,Serial_No[0]); 

                }
                else if(sens_set==1)
                {
                    glcd_putcharxy(59,Y,Serial_No[1]);     

                  }
                else if(sens_set==2)
                {    

                     glcd_putcharxy(65,Y,Serial_No[2]);     

                }
                else if(sens_set==3)
                {

                    glcd_putcharxy(71,Y,Serial_No[3]);     

                }
                else if(sens_set==4)
                {

                   glcd_putcharxy(77,Y,Serial_No[4]);     

                }
                else if(sens_set==5)
                {

                 glcd_putcharxy(83,Y,Serial_No[5]);     

                }
             else if(sens_set==6)
                {

                  glcd_putcharxy(89,Y,Serial_No[6]);     

                }
             else if(sens_set==7)
                {

                 glcd_putcharxy(95,Y,Serial_No[7]);     

                }

              else if(sens_set==8)
                {

                  glcd_putcharxy(101,Y,Serial_No[8]);     

                }

             else if(sens_set==9)
                {

                glcd_putcharxy(107,Y,Serial_No[9]);     

                }                

            if(kbhit)
            {
                kbhit=0;

                 fast_key=0;
              if(scancode1=='^')  //UP KEY FOR A TO Z CHARACTOR
                  {
                    parameter_changed=1;
                    first_time=1;
                    timer_en=0;

                    if(sens_set==0)
                    {
                        Serial_No[0]++; if(Serial_No[0]>0x5A)
                    {
                       Serial_No[0]=0x41;
                    }
                        if( Serial_No[0]<0x41)
                        Serial_No[0]=0x41; delay_ms(200);
                    }
                    else if(sens_set==1)
                    {
                        Serial_No[1]++;  if(Serial_No[1]>0x5A)
                    {
                      Serial_No[1]=0x41;
                    }
                        if( Serial_No[1]<0x41)
                        Serial_No[1]=0x41; delay_ms(200);
                    }
                    else if(sens_set==2)
                    {
                       Serial_No[2]++; if(Serial_No[2]>0x5A)
                    {
                       Serial_No[2]=0x41;
                    }
                        if( Serial_No[2]<0x41)
                        Serial_No[2]=0x41; delay_ms(200);
                    }
                    else if(sens_set==3)
                    {
                        Serial_No[3]++; if(Serial_No[3]>0x5A)
                    {
                       Serial_No[3]=0x41;
                    }
                        if( Serial_No[3]<0x41)
                        Serial_No[3]=0x41; delay_ms(200);
                    }

                    else if(sens_set==4)
                    {
                        Serial_No[4]++;   if(Serial_No[4]>0x5A)
                    {
                       Serial_No[4]=0x41;
                    }
                        if( Serial_No[4]<0x41)
                        Serial_No[4]=0x41;  delay_ms(200);
                    }
                    else if(sens_set==5)
                    {
                       Serial_No[5]++; if(Serial_No[5]>0x5A)
                    {
                       Serial_No[5]=0x41;
                    }
                        if( Serial_No[5]<0x41)
                        Serial_No[5]=0x41; delay_ms(200);
                    }
                    else if(sens_set==6)
                    {
                       Serial_No[6]++; if(Serial_No[6]>0x5A)
                    {
                       Serial_No[6]=0x41;
                    }
                        if( Serial_No[6]<0x41)
                        Serial_No[6]=0x41; delay_ms(200);
                    }                    

                    else if(sens_set==7)
                    {
                       Serial_No[7]++; if(Serial_No[7]>0x5A)
                    {
                       Serial_No[7]=0x41;
                    }
                        if( Serial_No[7]<0x41)
                        Serial_No[7]=0x41; delay_ms(200);
                    }
                    else if(sens_set==8)
                    {
                       Serial_No[8]++; if(Serial_No[8]>0x5A)
                    {
                       Serial_No[8]=0x41;
                    }
                        if( Serial_No[8]<0x41)
                        Serial_No[8]=0x41; delay_ms(200);
                    }

                    else if(sens_set==9)
                    {
                       Serial_No[9]++; if(Serial_No[9]>0x5A)
                    {
                       Serial_No[9]=0x41;
                    }
                        if( Serial_No[9]<0x41)
                        Serial_No[9]=0x41; delay_ms(200);
                    }
                    timer_en=1;

                }
                if(scancode1=='v')  //DOWN KEY FOR 0 TO 9
                {
                timer_en=0;
                 parameter_changed=1;
                    first_time=1;
                    if(sens_set==0)
                    {
                       Serial_No[0]++;  if(Serial_No[0]<0x30)
                    {
                      Serial_No[0]=0x39;
                    }
                     if (Serial_No[0]>0x39)
                     Serial_No[0]=0x30;  delay_ms(200);
                    }
                    else if(sens_set==1)
                    {
                       Serial_No[1]++;   if(Serial_No[1]<0x30)
                    {
                       Serial_No[1]=0x39;
                    }
                    if (Serial_No[1]>0x39)
                     Serial_No[1]=0x30; delay_ms(200);
                    }
                    else if(sens_set==2)
                    {
                        Serial_No[2]++;   if(Serial_No[2]<0x30)
                    {
                      Serial_No[2]=0x39;
                    }
                    if (Serial_No[2]>0x39)
                     Serial_No[2]=0x30; delay_ms(200);
                    }
                    else if(sens_set==3)
                    {
                        Serial_No[3]++;   if(Serial_No[3]<0x30)
                    {
                       Serial_No[3]=0x39;
                    }
                    if (Serial_No[3]>0x39)
                     Serial_No[3]=0x30;  delay_ms(200);
                    }

                    else if(sens_set==4)
                    {
                        Serial_No[4]++;  if(Serial_No[4]<0x30)
                    {
                       Serial_No[4]=0x39;
                    }
                    if (Serial_No[4]>0x39)
                     Serial_No[4]=0x30; delay_ms(200);
                    }
                    else if(sens_set==5)
                    {
                        Serial_No[5]++;  if(Serial_No[5]<0x30)
                    {
                       Serial_No[5]=0x39;
                    }
                     if (Serial_No[5]>0x39)
                     Serial_No[5]=0x30;  delay_ms(200);
                    }
                   else if(sens_set==6)
                    {
                        Serial_No[6]++;  if(Serial_No[6]<0x30)
                    {
                       Serial_No[6]=0x39;
                    }
                     if (Serial_No[6]>0x39)
                     Serial_No[6]=0x30;  delay_ms(200);
                    }

                   else if(sens_set==7)
                    {
                        Serial_No[7]++;  if(Serial_No[7]<0x30)
                    {
                       Serial_No[7]=0x39;
                    }
                     if (Serial_No[7]>0x39)
                     Serial_No[7]=0x30;  delay_ms(200);
                    }
                   else if(sens_set==8)
                    {
                        Serial_No[8]++;  if(Serial_No[8]<0x30)
                    {
                       Serial_No[8]=0x39;
                    }
                     if (Serial_No[8]>0x39)
                     Serial_No[8]=0x30;  delay_ms(200);
                    }
                   else if(sens_set==9)
                    {
                        Serial_No[9]++;  if(Serial_No[9]<0x30)
                    {
                       Serial_No[9]=0x39;
                    }
                     if (Serial_No[9]>0x39)
                     Serial_No[9]=0x30;  delay_ms(200);
                    }

                    timer_en=1;
                }

                if(scancode1=='M') MENU/ ENTER KEY
                {
                  sens_set++;
                  Serial_count--;
                  Serial_count10++;

                  scancode1=0;
                }


                if(sens_set>9)
                {
                  scancode1=0;
                }
              }

}while(!menu_exit &&(Serial_count10<ee_Serial_count ));

}


void deblank1(char* input) // USED TO REMOVE BLANK SPACES AND TERMINATING STRING
{   unsigned char i,j;

for (i = 0, j = 0; i<=strlen(input); i++,j++)
{
    if (input[i]!=' ')
       Serial_No[j]=input[i];
    else //enter code here
        j--;
}
Serial_No[j]='\0';

}

 

Last Edited: Thu. Dec 28, 2017 - 03:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thankx all for help its solved:

 

 

 

And result is:

 

 

Solution is:

 

N.Winterbottom wrote:

To print double quote character you must "escape" it like this.

if(yes_no) { //sensor off

    printf("AT+CWJAP=\"%s\",\"%s\"\r\n", ee_SSID, ee_pass); // this is solution 
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It doesn't seem likely that adding quotes would have changed whether or not the password was printed twice. Also, your deblank code probably wrong, although it's impossible to tell without more context. But it looks like you're populating a 10-byte array with all characters set to space, and then copying from that array to Serial_no, based on strlen(input). But if the input wasn't already nul-terminated, strlen(input) is garbage. I'm guessing, in fact, this is what bit you somehow -- that you had two 10-byte arrays and ended up copying from past the end of one of them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the_real_seebs wrote:

with all characters set to space, and then copying from that array to Serial_no, based on strlen(input). 

sorry for confusion,

Actually, the code last I posted was modified, after filling array( Serial_No) with space, I am sending Serial_No to function 'deblank' where all remaining spaces are removed and null termination \0 is added in same array Serial_No , then I copy the data from Serial_No to my required array i.e. ee_SSID or ee_pass. and then using printf it is sent.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, your deblank function is wrong. It's calling strlen() on a thing which isn't null-terminated. So if there were additional text in memory past the end of the array, before the first nul byte, you'd copy that into the array.
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the_real_seebs wrote:
if there were additional text (sic) in memory past the end of the array, before the first nul byte,

Actually, it wouldn't have to be specifically text - any non-zero bytes will do ...

 

you'd copy that into the array

So not only could you be copying too far - you could also be copying non-text bytes!

 

surprise

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...