Go Down

Topic: Read serial input startswith (Read 1 time) previous topic - next topic

rogierv

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: [Select]
//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.

AWOL

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

GoForSmoke

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?

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

rogierv

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

AWOL

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

Grumpy_Mike

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

rogierv

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

AWOL

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

guix

#8
Dec 13, 2012, 04:57 pm Last Edit: Dec 13, 2012, 05:01 pm by guix Reason: 1
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: [Select]

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 :)

GoForSmoke

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.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

PeterH

#10
Dec 13, 2012, 09:36 pm Last Edit: Dec 13, 2012, 10:27 pm by PeterH Reason: 1

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?
I only provide help via the forum - please do not contact me for private consultancy.

zoomkat

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: [Select]

// 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="";
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up