Pages: [1]   Go Down
Author Topic: Read serial input startswith  (Read 841 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I created the following code by copy pasting...
But i get an error message and i cannot find out why or how...

This is the code
Code:
//serial input

char incomingByte;
char FileName;

void setup() {
  Serial.begin(115200);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it starts with IMG then set it as FileName
    if (incomingByte.startsWith("IMG")) {
      Serial.println("File Read");
      FileName=incomingByte;
    }
  }
 
 
  delay(1000);
}

I want if the serial string starts with IMG that FileName is set to the serial input.
Logged

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

A single char is just that - it couldn't possibly be "IMG".
Did you mean String instead?

Quote
I created the following code by copy pasting...
Where from?

Quote
But i get an error message
But you didn't think you wanted to share it with us?
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.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4020
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you read one byte then check to see if the 1st 3 bytes of that == "IMG".....

why, whatever could be the problem??

How about doing something crazy like get the whole message before checking and possibly saving it?

Logged

Examples can be found in your IDE.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ Awol
from different sites...

error is
sketch_dec13a:16: error: request for member 'startsWith' in 'incomingByte', which is of non-class type 'char'

@GoForSmoke ( haha i had smoke yesterday when connecting it wrong )

ok so i need to read the whole string or what is the correct name for it..?
but how do i get he whole message?
back to google again...
Logged

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

There are examples included in the IDE.
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.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31472
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@rogierv
Have you ever thought your post sounds like:-

 " i just got two bottles of medicine from the pharmacy and mixed them together. Now I feel ill, what is the problem"

AWO - Where did you get them from.

You " from different pharmacys "

If you want help you have to provide information to allow us to help you.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand but i took the text startswith from one site
some serial code from another and some pieces of an example and some from arduino pages.
but the code is no at all one a site i made it myself
so i dont think the site makes any sense...

but like all ways most people speak here about the mistakes you make in the posting and not about the mistakes you make in the code

i go on trying examples and find it out myself
Logged

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

Quote
so i dont think the site makes any sense...

but like all ways most people speak here about the mistakes you make in the posting and not about the mistakes you make in the code
I'm not sure I understand what you're saying here.

have you worked through any of the examples provided with the IDE?
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.

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, little example here, this is non blocking Serial.read's, what you type in serial monitor is stored in the char array 'input', that you can use for whatever you want, here strncmp, comparing the first 3 letters of the input.

The line ending character setting (in the serial monitor) must be set to Carriage return.

Code:
void loop()
{
  if ( Serial.available() > 0 )
  {
    static char input[64];
    static uint8_t i;
    char c = Serial.read();

    if ( c != '\r' && i < 64-1)
      input[i++] = c;

    else
    {
      input[i] = '\0';
      i = 0;
      
      if ( !strncmp( input, "IMG", 3 ) )
      {
        Serial.println("File Read");
      }
    }
  }
}

Good luck smiley
« Last Edit: December 13, 2012, 11:01:42 am by guix » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4020
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That guix code is better for Arduino than examples that use C++ Strings. There are too many of those on the official site... do I hear an Amen out there?

Rogierv, Serial data is slow as snails compared to Arduino which may loop 100's of times waiting between characters to arrive. You are writing real time code with no operating system dragging the feet. It is a matter of understanding the scale (1 character at a time, usable processor speed **enormously faster** than characters arrive) and modeling how the information should flow on that scale.

Me personally, I compare the characters each as they come in and have time to check many alternatives (If I have many) when a single character does not match and still empty cycles through loop() before the next at 115200 baud. But I think best you practice what guix gave until you have a better overview or strong need to save ram space.

Logged

Examples can be found in your IDE.

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11179
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but like all ways most people speak here about the mistakes you make in the posting and not about the mistakes you make in the code

The reason is that when you ask a question poorly, it is much harder - sometimes even impossible - to answer. The sticky "Read this before posting a programming question" does have quite a lot of helpful advice about how to ask a question well. Is it fair to say you didn't read this before posting a programming question?
« Last Edit: December 13, 2012, 04:27:26 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 116
Posts: 8942
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple test code that captures a string and checks the contents of that string for a substring that will turn the arduino board LED on and off.

Code:
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(10); 
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: