String.startsWith in big routine

Hello,

I have got a weird problem with my Arduino Uno R3.

I perform a serial read and store the chars in a String variable. After an ‘\n’ or ‘\0’ sign I call a routing to check the content.

There are a lot of String.startsWith()-calls in it:

String serialbuffer = ""; //global
serialbuffer.reserve(200); //setup

{//loop
	if(Serial.available())
	{
		char in = (char) Serial.read();
	  
		if((in>=' ')&&(in<='~')) serialbuffer += in;    
		if ((in == '\n') || (in == '\0'))
		{
			checkcontent();
			serialbuffer = "";
		}
	}
}

void checkcontent()
{
	if (serialbuffer.startsWith("GETPARAM", 0)){serialbuffer.startsWith, SPI.transfer, Serial.print, digitalWrite}
	else if (serialbuffer.startsWith("SETPARAM", 0)){...}
	else if (serialbuffer.startsWith("RUN", 0)){...}
	else if (serialbuffer.startsWith("HELP", 0)){...}
	.
	.
	.
	else Serial.println("ERROR!");
}

If there are more than 15 calls of the method startsWith, I only get the “ERROR!”-String reported.
The content of serialbuffer sould be ok, because i printed it out.

Could this be a String problem?
Can a simple char-Array solve this problem?

The binary sketch-size behaves 11.972 Bytes of max. 32.256 Bytes.

Thanks in advance for your help.

You're calling checkcommand() but your function is called checkcontent(). Does this even compile?

This is just a sample code because noone wants to read 500 lines of code.

I will edit my first post if i can, ty for the hint.

I will edit my first post if i can

Never, ever, do that again. If you're a ditz, leave it for the world to see. Otherwise, the people who reply look like ditzs. And no one who has pointed out your mistake, and then been made to look like a ditz will ever be inclined to help you again.

	if (serialbuffer.startsWith("GETPARAM", 0)){serialbuffer.startsWith, SPI.transfer, Serial.print, digitalWrite}

Istheresomereasonyoucan'tuseacarriagereturnorsomespaces? What do you think serialbuffer.startsWith is doing, in the body of the statement? Nothing! What do you think SPI.transfer is doing? Nothing! None of the other statements do anything, either.

posi90:
If there are more than 15 calls of the method startsWith, I only get the "ERROR!"-String reported.

If you're asking people to diagnose a problem with your sketch you need to post the entire sketch. If your sketch contains a lot of code that is unrelated to the problem then remove that to produce a sketch which demonstrates the problem in the simplest way you can think of, and post that in its entirety. Quite often the act of producing that sketch will reveal some false assumption or misunderstanding which will enable you to answer your own question, but if not you've saved us all a lot of time wading through irrelevant code.

Can a simple char-Array solve this problem?

Yes and then you can use strncmp.

Dear guys,

I am new in this forum, but I have never had any problems that people could not read my C++ code-snippets.
Maybe here are other customs I do not know.

Again, this is an example code just to show you where there error occures.

I wanted to post you the full code, but then I get an error “The message exceeds the maximum allowed length (9500 characters)”.

Here is my code shortened again. I hope it can show you the problem now, what my first snippet couldn’t:

#include <SPI.h>
#define makeLong(hi, low)  (((long) hi) << 16 | (low))

String serialbuffer="";
const int CSPin = 11;

void setup()
{
	serialbuffer.reserve(200);
	Serial.begin(9600);
	SPI.begin();
	SPI.setDataMode(SPI_MODE3);
	SPI.setBitOrder(MSBFIRST);
	pinMode(CSPin, OUTPUT);
	digitalWrite(CSPin, HIGH);
}

void loop()
{
	if(Serial.available())
	{
		char in = (char) Serial.read();
  
		if((in>=' ')&&(in<='~')) serialbuffer += in;    
		if ((in == '\n') || (in == '\0'))
		{
			serialbuffer+='\0';
			serialbuffer.toUpperCase();
			checkcommand();
			serialbuffer = "";
		}
	}
}

void checkcontent()
{
	if (serialbuffer.startsWith("SOFTSTOP", 0))
	{
		digitalWrite(CSPin, LOW);
		SPI.transfer(0xB0);
		digitalWrite(CSPin, HIGH);
		Serial.println("SOFTSTOP");
	}
	else if (serialbuffer.startsWith("HARDSTOP", 0))
	{
		digitalWrite(CSPin, LOW);
		SPI.transfer(0xB8);
		digitalWrite(CSPin, HIGH);
		Serial.println("HARDSTOP");
	}
	else if (serialbuffer.startsWith("SOFTHIZ", 0))
	{
		digitalWrite(CSPin, LOW);
		SPI.transfer(0xA0);
		digitalWrite(CSPin, HIGH);
		Serial.println("SOFTHIZ");
	}
	else if (serialbuffer.startsWith("HARDHIZ", 0))
	{
		digitalWrite(CSPin, LOW);
		SPI.transfer(0xA8);
		digitalWrite(CSPin, HIGH);
		Serial.println("HARDHIZ");
	}
	else if (serialbuffer.startsWith("GETSTATUS", 0))
	{
		byte MSByte, LSByte;
		digitalWrite(CSPin, LOW);
		SPI.transfer(0xD0);
		MSByte = SPI.transfer(0x00);
		LSByte = SPI.transfer(0x00);
		digitalWrite(CSPin, HIGH);
		Serial.print("GETSTATUS 0x");
		Serial.println(word(MSByte, LSByte), HEX);
	}
	else if(serialbuffer.startsWith("HELP", 0))
	{
	  Serial.println("HELP: blubb");
	}
	else
	{
	  Serial.print("ERROR: ");
	  Serial.println(&serialbuffer[0]);
	}
}

There are more command checks in my main programm.

I have replaced now all String functions and coded it to (char*). Now it work but I do not know why.

I wanted to post you the full code, but then I get an error "The message exceeds the maximum allowed length (9500 characters)".

You can add attachments.

How to use this forum

Now it work but I do not know why.

Because Strings waste memory which is in short supply. When you quit wasting memory, then out-of-memory conditions are less likely.

I'm not sure what happens when you append a null character to a String object (line #27) but at best it's unnecessary - at worst it might confuse some subsequent processing leading to unexpected behaviour.

I'm not sure what the result of this expression is when serialbuffer is a String object but at best it's unnecessary (you can print the String object directly) - at worst you may end up referencing memory which is outside the bounds of the String's buffer leading to unexpected behaviour.

&serialbuffer[0]

The String class offers minimal benefits over just using plain old c-strings (null-terminated char arrays) and introduces several issues of its own, so if you have re-written your code to eliminate use of the String class I would regard that as a good move. However, the most recent code you posted does still use the String class.

posi90:
Again, this is an example code just to show you where there error occures.

But it still won't compile, as you've made the exact same error again. You're calling a function that doesn't exist.