Pages: 1 [2]   Go Down
Author Topic: Identifying a character from serial.read()  (Read 859 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 2
Posts: 407
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Regards,
Andrea
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But, more importantly, it is necessary to post code that compiles, and doesn't read characters that haven't arrived yet.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey sorry for the last one. Here the compile code.
Code:
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);
}
Logged

Thanx & Regards

Niladri

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanx & Regards

Niladri

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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:
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()>0)
  {
    Serial.print((Serial.read() == 'A'), DEC);
  }
}
(could probably lose the "DEC" too)
Logged

"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.

Pages: 1 [2]   Go Up
Jump to: