Loading...
Pages: [1]   Go Down
Author Topic: Changing a serial input into a usable string  (Read 438 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone

I purchased a JY-MCU (V.1.05) bluetooth module for my Arduino Uno as I was looking to send a message from the Amarino app on an Android device to the serial monitor. However I was going to make use of the received message later by sending it to an LED matrix to display a scrolling message.

The code I have below allows me to send a message to the monitor however I was looking to convert the said message to a string. The code from a similar topic ( http://arduino.cc/forum/index.php/topic,45629.0.html ) does not seem to change anything. I was looking to receive a message and print it afterwards print the string which should be identical to the message.

Code:
#include <MeetAndroid.h>

/*
  Software serial multple serial test
 
 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.
 
 The circuit:
 * RX is digital pin 2 (connect to TX of other device)
 * TX is digital pin 3 (connect to RX of other device)
 */
#include <SoftwareSerial.h>
int ledPin = 13;

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() 
{
    pinMode(ledPin, OUTPUT);
 
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Correct baud rate selected [9600]. Connection established");
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() // run over and over
{
  digitalWrite(ledPin, HIGH);
 
  if (mySerial.available())
    Serial.write(mySerial.read());

  if (Serial.available())
    mySerial.write(Serial.read());
   
}


Thank you
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 316
Posts: 35526
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was looking to receive a message and print it afterwards print the string which should be identical to the message.
You need to know when the message starts (a start of packet marker) and when it ends (an end of packet marker). You need to save the characters read into an array, not just print them out again.

Look at how this code deals with properly delimited packets:
                                                                                                                               
Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, thank you!  smiley-mr-green

I think I understand. I feed individual characters into an array/buffer (inData) and use a standard method to print all values in the buffer?

Code:
int i;
  for (i = 0; i < *size of the buffer*; i = i + 1) {
  Serial.println(inData[i]);
}
« Last Edit: January 14, 2013, 12:27:24 pm by blacky777 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 316
Posts: 35526
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I feed individual characters into an array/buffer (inData) and use a standard method to print all values in the buffer?
Yes, though why you want to print
e
a
c
h

c
h
a
r
a
c
t
e
r

on a new line is a mystery.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I feed individual characters into an array/buffer (inData) and use a standard method to print all values in the buffer?
Yes, though why you want to print
e
a
c
h

c
h
a
r
a
c
t
e
r

on a new line is a mystery.

meant print instead of println  smiley-red

Hmm, strange. When I add the loop to print the first 5 characters of the array I only get blanks now. Can you suggest why this would be?

This is my void loop method. I only loop once for the first input.

Code:
void loop() // run over and over
{
  digitalWrite(ledPin, HIGH);

  if (mySerial.available())
    Serial.write(mySerial.read());

  if (Serial.available())
    mySerial.write(Serial.read());

  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
      index = 0;
      inData[index] = '\0';
      started = true;
      ended = false;
    }
    else if(inChar == EOP)
    {
      ended = true;
      break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }

if(runOnce){
    int i;
    for (i = 0; i < 5; i = i + 1) {
      Serial.print(inData[i]);
    }
    runOnce--; //runOnce is set to 1 initially, this makes it zero
  }

}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 316
Posts: 35526
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This is my void loop method. I only loop once for the first input.
Which is what? When loop() executes, there may not be any data available to read. Or, there may be one byte. It is unlikely that there will be more than one.

inData is a NULL terminated array of chars. Conveniently, Serial.print() knows how to print a NULL terminated array of chars in one call:
Code:
Serial.print(inData);
Logged

0
Offline Offline
Tesla Member
***
Karma: 50
Posts: 6553
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have an example of the strings being sent, and are the strings terminated with a special character or cr/lf?
Logged

Why I like my 2005 rio yellow Honda S2000 with the top down, and more!
GOOGLE ADVANCED FORUM SEARCH BELOW!  
Go to:  http://www.google.com/advanced_search?hl=en
put in key search words,
use site or domain:  http://arduino.cc/forum
or in a google search box put key words site:http://arduino.cc/forum

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have an example of the strings being sent, and are the strings terminated with a special character or cr/lf?

Im just looking to send something like "Hello" from the Amarino app, the serial monitor should show Hello (as serial input) then Hello again as the printed contents of a buffer.

 I am encapsulating the strings in '<' and '>'  
« Last Edit: January 16, 2013, 10:14:28 am by blacky777 » Logged

Queens, New York
Offline Offline
Edison Member
*
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What you can try is to maybe echo the incoming data from amarino and send it to the serial monitor. Have you tried that already?

Quote
I am encapsulating the strings in '<' and '>'

Well what you can try is use an IF statement to look for '<' and everything after that, will go into either a char array[] or a string. Strings are easier to collect data, but tend to cause problems if they are being asked to do too much. Char arrays[] are just as useful but require a bit more code to extract and clear the data afterwards. They also dont have any problems down the line, unless the array is not big enough to hold all the data, in which case you simply increase the array size (array[20]: if not big enough => array[50]).

As for the end of the incoming data, same thing, use an IF statement to look for '>' and everything after will NOT go into the string/array.
« Last Edit: January 16, 2013, 08:17:04 pm by HazardsMind » Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

0
Offline Offline
Tesla Member
***
Karma: 50
Posts: 6553
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple serial code to capture strings seperated by a comma.

Code:
//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

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

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input     
     } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Logged

Why I like my 2005 rio yellow Honda S2000 with the top down, and more!
GOOGLE ADVANCED FORUM SEARCH BELOW!  
Go to:  http://www.google.com/advanced_search?hl=en
put in key search words,
use site or domain:  http://arduino.cc/forum
or in a google search box put key words site:http://arduino.cc/forum

Pages: [1]   Go Up
Print
 
Jump to: