Changing a serial input into a usable string

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.

#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

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:

#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';
  }
}

Ah, thank you! :grin:

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?

 int i;
  for (i = 0; i < *size of the buffer*; i = i + 1) {
  Serial.println(inData[i]);
}

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.

PaulS:

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 :blush:

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.

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
  }

}

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:

Serial.print(inData);

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

zoomkat:
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 ‘>’

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?

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.

Simple serial code to capture strings seperated by a comma.

//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
    }
  }
}