Go Down

Topic: [SOLVED] array problem (Read 1 time) previous topic - next topic

killertrain

Apr 07, 2011, 04:30 pm Last Edit: Apr 07, 2011, 04:56 pm by killertrain Reason: 1
hi

im working on my serial communication

Code: [Select]
void serieel(){
 
if (Serial.available() > 0) {
 char incoming [4] ;
 char mode [2] ;
 char func [2];
 char val [1] ;
   while(Serial.available() < 5) {
     ;
   }
   for (int i=0; i < 5; i++) {
     incoming[i] = Serial.read() ;
   
   }
   
     mode[0] = incoming[0] ;
     mode[1] = incoming[1] ;
     func[0] = incoming[2] ;
     func[1] = incoming[3] ;
     val[0]  = incoming[4] ;      
     


   Serial.println(incoming);  //debug
   Serial.println(mode);  //debug
   Serial.println(func);  //debug
   Serial.println(val);  //debug
   
 
 Serial.flush();
 
}

}


the problem is that i cant get it to work the way i want

the serial monitor returns:

12345            incomming
123412345      mode
3412345         func
5123412345    var

but it needs to be :

12345
12
34
5

what is going wrong?

PaulS

Quote
the serial monitor returns:

12345            incomming
123412345      mode
3412345         func
5123412345    var

How do you know which line represents which value? You should have something like:
Serial.print("incoming: [");
Serial.print(incoming);
Serial.println("]");
so that you KNOW what is being stored in each variable.

Code: [Select]
 char incoming [4] ;
You are trying to store 5 elements in an array sized to hold 4.

Code: [Select]
 Serial.flush();
Throwing away random amounts of unread data is always a good idea. NOT!

All of you character arrays need to be NULL-terminated before they will print properly. None of them currently are.
The art of getting good answers lies in asking good questions.

killertrain


Quote
the serial monitor returns:

12345            incomming
123412345      mode
3412345         func
5123412345    var

How do you know which line represents which value? You should have something like:
Serial.print("incoming: [");
Serial.print(incoming);
Serial.println("]");
so that you KNOW what is being stored in each variable.


    Serial.println(incoming);  //debug
    Serial.println(mode);  //debug
    Serial.println(func);  //debug
    Serial.println(val);  //debug

it doesnt put in a random order or something ;)


but how do i get them to null terminate??

AWOL

Quote
but how do i get them to null terminate??

Make each array one char bigger than it is now, and put '\0' as the last element.
Code: [Select]
mode[0] = incoming[0] ;
      mode[1] = incoming[1] ;
      mode [2] = '\0';

killertrain

thanks for the help, it solved half of my problem

Code: [Select]
void serieel(){
 
if (Serial.available() > 0) {
  char incoming [5] ;
  char mode [2] ;
  char func [2];
  char val [1] ;
    while(Serial.available() < 5) {
      ;
    }
    for (int i=0; i < 5; i++) {
      incoming[i] = Serial.read() ;
   
    }
   
      mode[0] = incoming[0] ;
      mode[1] = incoming[1] ;
      mode[2] = '\0';
      func[0] = incoming[2] ;
      func[1] = incoming[3] ;
      func[2] = '\0';
      val[0]  = incoming[4] ;   
      val[1] = '\0'; 
     


    Serial.println(incoming);  //debug
    Serial.println(mode);  //debug
    Serial.println(func);  //debug
    Serial.println(val);  //debug
   
 
}

}


the serial monitor returns:

  ( nothing ) ( incomming )
  (nothing )  ( mode )
34              ( func )
5               ( val )


killertrain

sorry said nothing

forgot to make the arrays 1 size bigger :$

thanks for the help

Go Up