Serial.read()

is there a string version of Serial.read()? i want to send simple text from computer to arduino and use that text to set an LED. so if i sent green it would read green and turn on the pin connected to the green LED.

it seems Serial.read() is only the first character.

Simple ccode that captures what is sent into a string.

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

String readString;

void setup() {
    Serial.begin(9600);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(1);  
      if (Serial.available() >0) {
        char c = Serial.read();
        readString += c;}
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      
      readString="";
      } 
   }

or you could just send the command 'g' then the character version would work just fine.

I have always used character arrays and avoided any dynamic memory allocation - because I just think its a bad idea in embedded systems, so I have not really looked at the 'String' class that came in with v 19.

Looking at the docs, I cannot see how you de-allocate the memory that the string grabs.

Zoomcat, if you typed in a 2k string would your example run out of memory and crash the board?

Zoomcat's example works reliably only as commented - with the serial monitor. You'd be hard-pressed to type 2k characters into the serial monitor's window!

Thinking about it, with Windows' stuttering handling of USB, it may not work well with strings longer than a few tens of bytes.

Well, ok, my example was contrived, but my concern remains unanswered.

How do you deallocate the memory that the string has grabbed?

Can it, in theory, just grow until you run out of memory and then the program freezes, or behaves unpredictably?

I'll give you it is a mightily convenient way to play with strings, but it seems on the face of it to be dangerous.

Thinking about it, with Windows' stuttering handling of USB, it may not work well with strings longer than a few tens of bytes.

Thats why I use a Mac ;)

The String object allocates memory using malloc(). It frees it use free(). So, the memory that it grabs IS freed.

However, the design of the String class is pretty poor. When you allocate a string, it grabs just enough memory for the characters you want to put in it at the time the string is constructed. If you then wish to add a character to the object, it allocates enough space to hold the existing string plus one character, copies the existing data to the new memory, adds the extra character, and frees the memory used by the old string.

For manipulating a few characters, it is OK. For more than a few, it is horribly inefficient. But, it IS easy to use and to explain to newbies now to use.

You'd be hard-pressed to type 2k characters into the serial monitor's window! Thinking about it, with Windows' stuttering handling of USB, it may not work well with strings longer than a few tens of bytes.

Well, one can ctrl-v paste most anything into the serial monitor up to the amount the serial monitor can hold. From my tinkering I've found there is a limit to what a string can hold, so if there is data streaming into the arduino, one needs to capture only what is desired from the data stream. Not sure about the windows USB "stuttering" as I have several devices such a cams, printers and such connected to my USB port and it handles extremely large video files and such without issue.

Not sure about the windows USB "stuttering" as I have several devices such a cams, printers and such connected to my USB port

I was thinking more of the COM port handling, rather than streaming.

I was thinking more of the COM port handling, rather than streaming.

The windows comport issues I’ve read about usually have to do with serial port flashing/programming of microcontrollers using USB to serial port adapters. The adapter drivers may have “latency” values that have to be adjusted to accomidate the microcontroller hardware timing.

zoomkat:
Simple ccode that captures what is sent into a string.

// zoomkat 8-6-10 serial I/O string test

// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
Serial.begin(9600);
        Serial.println(“serial test 0021”); // so I can keep track of what is loaded
        }

void loop() {

while (Serial.available()) {
        delay(1); 
    if (Serial.available() >0) {
        char c = Serial.read();
        readString += c;}
        }
       
      if (readString.length() >0) {
      Serial.println(readString);
     
      readString="";
      }
   }

this worked i just stuck to the single char instead due to the memory issues that could happen.

With the delay set to 10 in the code I posted, the below string was returned in tact. Using 1 for the delay produced poor results. So a string of the below size can be captured.

IR remote mods to change channels via web page (use with "zoomkat's amazing batch file" to send the status: 204 to the web server). Test the

But remember, if you change the baud rate, or decide not to use the serial monitor, this method may not work.
It relies on the fact that at 9600 baud one character takes about one millisecond to transmit, and that the serial monitor sends the characters in the dialogue only when you hit the send key.