Go Down

Topic: Identifying a character from serial.read() (Read 1 time) previous topic - next topic

gnusso

Good Morning,
I think is necessary to do a for cicle, scan char by char and check the condition ...

Regards,
Andrea

AWOL

But, more importantly, it is necessary to post code that compiles, and doesn't read characters that haven't arrived yet.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

And, it's important to answer the questions that are asked. If you have any control over the sender, you can make your job so much easier. For instance, if the sender could me made to send "<A1234B4567C8901D3456>" instead of "A1234B4567C8901D3456", you could use code like this to read the data whenever it gets there:
                                                                                                                               
Code: [Select]

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Then, where it says "Process the packet", you could separate out the A, B, C, and D values.

newbee_2

Hey sorry for the last one. Here the compile code.
Code: [Select]

char serial_data[100];
int index = 0;
char data;
void setup()
{
 Serial.begin(9600);

}

void loop()
{
 if(Serial.available()>0)
 {
  data = Serial.read();
//    if(ch[0] == "A" )
//    {
  serial_data[index++] = data;
         
//      char frstchar = buff[0];
    if(serial_data[0] == 'A')
//    for(int i = 0; i<=20; i++)
   {

    Serial.print(1, DEC);
//     delay(500);
   }
  else
         Serial.print(0, DEC);
 }
 delay(500);
}
Thanx & Regards

Niladri

newbee_2

Ok Mr. PaulS I will make the packet in a way you have describe(because i am also the sender).
Thanx & Regards

Niladri

AWOL

Code: [Select]
    serial_data[index++] = data;
Try to acquire the habit of not allowing your code to write over memory that it doesn't own.

Your code could have been much simpler:
Code: [Select]
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()>0)
  {
    Serial.print((Serial.read() == 'A'), DEC);
  }
}
(could probably lose the "DEC" too)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up