Offline
Newbie
Karma: 0
Posts: 4
|
 |
« on: January 14, 2013, 09:36:03 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #1 on: January 14, 2013, 09:46:23 am » |
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'; } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #2 on: January 14, 2013, 12:20:30 pm » |
Ah, thank you!  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]); }
|
|
|
|
« Last Edit: January 14, 2013, 12:27:24 pm by blacky777 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #3 on: January 14, 2013, 12:32:15 pm » |
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
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #4 on: January 14, 2013, 02:24:45 pm » |
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  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 }
}
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #5 on: January 14, 2013, 02:32:48 pm » |
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);
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6553
Arduino rocks
|
 |
« Reply #6 on: January 14, 2013, 07:34:04 pm » |
Do you have an example of the strings being sent, and are the strings terminated with a special character or cr/lf?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #7 on: January 16, 2013, 09:45:24 am » |
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
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #8 on: January 16, 2013, 10:42:45 am » |
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.
|
|
|
|
« 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
Tesla Member
Karma: 50
Posts: 6553
Arduino rocks
|
 |
« Reply #9 on: January 16, 2013, 07:35:13 pm » |
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 } } }
|
|
|
|
|
Logged
|
|
|
|
|
|