Go Down

Topic: arduino mega serial talking with strings (Read 392 times) previous topic - next topic

feveran

Nov 16, 2012, 03:06 pm Last Edit: Nov 16, 2012, 03:29 pm by feveran Reason: 1
hi all,
i am trying to communicate a serial device (pic 12f) with arduino mega.
i connected device to serialport3. when i send command 'g' to serialport3, device blinks a led and sends a sentence like "ok now\n\r".
i send 'g' command to the device by sending 'a' command to arduino. also i want to see the device's reply from arduino serial monitor.
i see the led blinks but i couldn't read the sentence.
i am trying to modify serial event skecth, where problem can be?

Code: [Select]
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

inputString.reserve(200);
void setup() {

 Serial.begin(9600);
 Serial3.begin(9600);
 // reserve 200 bytes for the inputString:
 inputString.reserve(200);
}
void loop() {
 /*  
  if (Serial.available())
  {
  char ser = Serial.read();
  if(ser == 'a'){
  Serial3.write('g');
 
  }
 
  }
  }
  */
 if (stringComplete) {
   Serial.println(inputString);
   // clear the string:
   inputString = "";
   stringComplete = false;
 }
}

void serialEvent() {
 while (Serial3.available()) {
   // get the new byte:
   char inChar = (char)Serial3.read();
   // add it to the inputString:
   inputString += inChar;
   // if the incoming character is a newline, set a flag
   // so the main loop can do something about it:
   if (inChar == '\n') {
     stringComplete = true;
   }
 }
}


Cybernetician

#1
Nov 16, 2012, 03:16 pm Last Edit: Nov 16, 2012, 03:39 pm by AWOL Reason: 1
[font=Verdana][[/font]code][font=Verdana][[/font]/code] use insert code.

Moderator edit: Dummy code tags corrected.
From Idea To Invention

PaulS

Quote
arduino mega serial talking with strings

Code: [Select]
String inputString = "";         // a string to hold incoming data
There is a world of difference between a string and a String. The sooner that you learn what that one little letter difference means, the better.

Quote
i send 'g' command to the device by sending 'a' command

I call by brother Tom by the name of George...

Code: [Select]
  /* 
   if (Serial.available())
   {
   char ser = Serial.read();
   if(ser == 'a'){
   Serial3.write('g');
   
   }
 
   }
   }
   */

Thanks for posting all this... Is your delete key broken?

Code: [Select]
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);

You need to reserve 200 characters to hold a one letter command? Are you serious?

Quote
but i couldn't read the sentence.

What sentence? The only code to output anything is commented out.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy