3 years, 8 months ago.

serial code question - code not working properly

Hello, I have this following serial interrupt code (receiving data from an arduino). The data comes in format "!1110011100101010@!1110011100101010@"

so start byte is ! and end marker is @

I am trying to match the two sets together and if they match then make a variable valid=1

problem is that I am getting valid=1 BUT no data gets printed on my screen with pc.putc commands as below. to me it seems the arrays are left blank and then of course they match and valid comes as 1. please help me correct this.

void arduinodata(){
        // get the incoming byte
        char byteIn = arduino.getc();
        if (start==1) start==2;
        if (byteIn=='!'){counter=0; start=1;finish=0;}
        if (byteIn=='@'){counter=0; start=0;finish=1;}
        if (start==2){
            data[counter]=byteIn;
            counter++;
            }
        if (finish==1){
            if (count==1){
                valid=1;
                for (char i=0;i<16;i++){
                    if (data1[i]!=data[i]) valid=0;
                    }
                if (valid==1) pc.printf("Data Matched\n");
                if (valid==1) {
                    pc.putc(data[0]);
                    pc.putc(data[1]);
                    pc.putc(data[2]);
                    pc.putc(data[3]);
                    pc.putc(data[4]);
                    pc.putc(data[5]);
                    pc.putc(data[6]);
                    pc.putc(data[7]);
                    pc.putc(data[8]);
                    pc.putc(data[9]);
                    pc.putc(data[10]);
                    pc.putc(data[11]);
                    pc.putc(data[12]);
                    pc.putc(data[13]);
                    pc.putc(data[14]);
                    pc.putc(data[15]);
                    } 
                count++;
                }
            if (count==0){
                for (char i=0;i<16;i++){
                    data1[i]=data[i];
                    }   
                count++;
                }            
            }
            if (count>=2) count=0;
}

I've changed the code a bit. Now my code looks like:

void arduinodata(){
            char byteIn = arduino.getc();
            if (byteIn=='!'){counter=0; start=1;finish=0;done=0;}
            if (byteIn=='@'){start=0;finish=1;copy++;}
            if (copy>=2)copy=0;
            if (start){
                if (copy==0){
                    data1[counter]=byteIn;
                    counter++;
                    } else {
                        data2[counter]=byteIn;
                        counter++;
                        }
                }
            if (finish==1 and done==0){
                pc.printf("Dat1: ");
                for (int i=0;i<16;i++){
                    pc.putc(data1[i]);
                    }
                pc.putc(10);
                pc.printf("Dat2: ");
                for (int i=0;i<16;i++){
                    pc.putc(data2[i]);
                    }
                pc.putc(10);
                pc.putc(10);
                done=1;
                }
}

from arduino I am sending the following to it:

        Serial3.print("!");
         Serial3.print(state1,BIN);
         Serial3.print("@");
         Serial3.print("!");         
         Serial3.print(state1,BIN);
         Serial3.print("@");

And this is what I am getting on my terminal screen:

Dat1: !10000001100.... Dat2: ................

So data2 is not getting filled for some reason.

posted by D J 09 Apr 2016
Comment on this question

3 Answers

3 years, 8 months ago.

may be you can try :

 if (valid==1) 
{
   for (int k=0; k<16; k++)
   {
      if (data[k])
        pc.printf("1");
     else
        pc.printf("0");
   }
}

or in a shorter way :

 if (valid==1) 
{
   for (int k=0; k<16; k++)
     printf("%d", data[k]?1:0);
}

which is the same but more elegant...

3 years, 8 months ago.

The problem is earlier in the code - you are not filling the array with sequentially received bytes. You read the serial port once only in the first line of the function, thereafter using that same byte repeatedly.

I'd guess you'd want to loop reading the serial until you get that sync byte "!", & only then then fill the array with data[counter]=arduino.getc().

It is interrupt driven, so it only enters the loop for 1 character, this part I am quite sure is working. Even if I put it in loop, it will only pick one byte as there is no buffer which it is reading from.

posted by D J 09 Apr 2016

Ah, sorry I see that now.

posted by David Lowe 11 Apr 2016
3 years, 8 months ago.

Try this:

void arduinodata(){

            char byteIn = arduino.getc();

            if (byteIn=='!'){counter=0; start=1;finish=0;done=0;}

            if (byteIn=='@'){start=0;finish=1;copy++; }

// removed if (copy >= 2)

            if (start){
                if (copy==0){
                    data1[counter]=byteIn;
                    counter++;
                    } else {
                        data2[counter]=byteIn;
                        counter++;
                        }
                }
            if ((finish==1) && (copy==2)){ // changed from done to copy.
                pc.printf("Dat1: ");
                for (int i=0;i<16;i++){
                    pc.putc(data1[i]);
                    }
                pc.putc(10);
                pc.printf("Dat2: ");
                for (int i=0;i<16;i++){
                    pc.putc(data2[i]);
                    }
                pc.putc(10);
                pc.putc(10);
                done=1;
                copy=0;
                }
}

I've not looked in detail at your first version but line 4 is an obvious bug, it should be start = 2 not start == 2


You need to log in to post a question