help reading strings from serial port

Morning all,
Im pretty fresh to Arduino, but I have good embedded systems knowledge. My problem is quite simple, but after alot of effort i've come up against a barrier.
My problem is reading strings over the serial port and saving it as a local variable. Here's some example code i have:

// hardware: arduino mega

int *ptr;
int command[10];
int i = 0;
int readflag = 0;
void setup()
{
  ptr = &command[0];
  
  Serial.begin(9600);
  delay(1000);
  
  Serial.println("Hello Terminal");
  
}

void loop ()
{
  
  if (Serial.available() > 0)
   {  
       *ptr = Serial.read();
       ptr++;
       readflag = 1;
   }  
   
   if (readflag == 1)
   {
  Serial.print(command[2],BYTE);
  readflag = 0;
   }    
}

I input : 12345
and the out put is: [] [] 333

any input would be appreciated..

It may be easier for you if you don't use pointers.

You don't actually say what you want the code to do so it's a little difficult to give advice on how to do it. Assuming you want to read in 5 characters and then print the 3rd character as a byte.

// hardware: arduino mega

int command[10];

void setup()
{
 
  Serial.begin(9600);
  delay(1000);
  
  Serial.println("Hello Terminal");  
}

void loop ()
{  
  if (Serial.available() >= 5 ) // wait for 5 characters
  {  
      for(int i=0; i < 5; i++)
         command[i] = Serial.read();
   }  
  Serial.print(command[2],BYTE);
}

thanks mem, i figured it out.

i reopen that post..

i'm quite new to c syntax but
in c style i find more elegant and "c-way" the code with pointer..

why it doesn't work?

and.. i have understand more ore less everything, but not
Serial.print(command[2],BYTE);

why only [2] ???

thanks and soryy for stupid quest
a

i'm quite new to c syntax but in c style i find more elegant and "c-way" the code with pointer..

I don't think there is anything elegant about pointers, efficient perhaps, but awkward and problematic for beginners.

Serial.print(command[2],BYTE); is how to print the 3rd character as a byte.
Pointers would be required to print command as a string starting from the third byte:
Serial.print( (char*)&command[2]);

sorry guys
i'm getting out of mind with this serial comunication

i'm trying to implement a simple echo services between arduino and processing

here is my code:

arduino

#define STR 2

char buffer[STR];
void setup() {
      Serial.begin(9600);
        Serial.flush();
}

void loop() {
        int i=0;
  
      if (Serial.available() >= STR) {

                for (i=0;i<STR;i++) {
                    buffer[i]=Serial.read();
                }
                
                Serial.print("Ok i recived: ");
                Serial.println(buffer);
      }
}

and this my proceesing code

import processing.serial.*;
Serial port;
void setup() {
    port = new Serial(this,Serial.list()[0], 9600);
}
void draw() {}

void mousePressed() {
  port.write("ab");
}

with 6 click in processing applet
i obtain in serial monitor
this:
O eieOi ecvda
Oircvdab

rcieOk
k i recived: ab

if you send strings that are terminated with a carriage return you can receive them in Arduino like this:

#define STRLEN 16

char buffer[STRLEN];
int  bufferIndex = 0; 

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

void loop()
{
  if( Serial.available())
  {
    char ch = Serial.read();
    if( ch == '\r')  // is this the terminating carriage return
    {
      buffer[ bufferIndex ] = 0; // terminate the string with a 0      bufferIndex = 0;  // reset the index ready for another string
      // do something with the string
    }
    else
      buffer[ bufferIndex++ ] = ch; // add the character into the buffer
  }
}

well..
sorry mayebe my english is not good
i don't think i'm sending carriage return, i'm sending only "ab"
compiling your code che carriage return never appeared..

my problem is that i don't understan why my Serial.print string doen't look as i wroted and i don't know what i recived..

my problem is that i don't understan why my Serial.print string doen't look as i wroted and i don't know what i recived..

I think it's because you do not zero-terminate it.

#define STR 3

char buffer[STR];
void setup() {
    Serial.begin(9600);
    Serial.flush();
}

void loop() {
        int i=0;
  
      if (Serial.available() >= STR) {

                for (i=0;i<STR;i++) {
                    buffer[i]=Serial.read();
                }
                
                Serial.print("Ok i recived: ");
                Serial.println(buffer);
      }
}

previous post is SPAM

:exclamation
Thankyou mem for finally posting a solution to my serial terminal input problems- I've been slamming my head against the walls for days!

I'm trying to set up terminal input routine for interactive programs, that I could run from a Hyperterminal session on my PC, but I couldn't find a way to get my arduino to accept more than a single character of terminal input.

Your code in Reply#6 works, using '\r' for the carriage return input line terminator:

/*
Input more than a single character at a time-
 THIS WORKS WITH HYPERTERM !!
 (but not with Arduino IDE ??)
 */


#define STRLEN 16

char buffer[STRLEN];
int  bufferIndex = 0;

int  number;

void setup()
{
  Serial.begin(57600);
  Serial.println("Input up to 16 characters: ");
}

void loop()
{
  if( Serial.available())
  {
    char ch = Serial.read();
    if( ch == '\r')  // is this the terminating carriage return
    {
      buffer[ bufferIndex ] = 0; // terminate the string with a 0      
      bufferIndex = 0;  // reset the index ready for another string
      // do something with the string
      Serial.print("Buffer = ");
      Serial.println(buffer);
      number = atoi(buffer);
      Serial.print("Number = ");
      Serial.println(number);
      number = 10*number;
      Serial.print("10*Number = ");
      Serial.println(number);
    }
    else
      buffer[ bufferIndex++ ] = ch; // add the character into the buffer
  }
}

Unfortunately, using CR to terminate input lines does not work in the Arduino IDE Serial Terminal- the code above works in Hyperterminal, but not the IDE

Code that works in the IDE, but doesn't work in Hyperterminal, will look something like this:

/*
    Convert incoming Serial numerical characters to integers  
    
    WORKS in IDE
    DOES NOT WORK in Hyperterminal

*/

char NumBuff[3];
int CharRcvd;
int i=0;
long Num=0;

void setup()
{
 Serial.begin(57600);
 CharRcvd = 0;
 NumBuff[CharRcvd] = '\0';
 Serial.println("Wait for 3 digits-");
}

void loop()
{  
 while (Serial.available()) {
// if (Serial.available()) {
   
   for (i=0; i<3; i++) {
     NumBuff[i] = Serial.read();
     Serial.println(NumBuff[i]);
   }
   
   Num = atoi(NumBuff);
   
   Serial.println(Num);
   
   Serial.print("Int = ");
   Serial.println(Num);
   Num = Num * Num;
   Serial.print("Squared = ");
   Serial.println(Num);
   Serial.println();


 }
}

??Why??? does the Arduino IDE Serial Terminal require a non-standard line terminator??

It's a pain, because I can't test my Hyperterm terminal input/output code in the IDE...

the code that you are using on the IDE is not going to be reliable because it depends on three characters being immediately available.

Much better to use the Hyperterminal code but replace the \r with a character you can type in the IDE – try using # (the pound symbol). For example 123#

Change:
if( ch == '\r') // is this the terminating carriage return
to
if( ch == '#')

SPAM