Pages: [1]   Go Down
Author Topic: arduino mega serial talking with strings  (Read 260 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
    }
  }
}

« Last Edit: November 16, 2012, 09:29:33 am by feveran » Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[code][/code] use insert code.

Moderator edit: Dummy code tags corrected.
« Last Edit: November 16, 2012, 09:39:04 am by AWOL » Logged

From Idea To Invention

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
arduino mega serial talking with strings
Code:
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:
  /* 
   if (Serial.available())
   {
   char ser = Serial.read();
   if(ser == 'a'){
   Serial3.write('g');
   
   }
 
   }
   }
   */
Thanks for posting all this... Is your delete key broken?

Code:
  // 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.
Logged

Pages: [1]   Go Up
Jump to: