Tengo Arduino conectado a un módulo bluetooth HC-06.
No quiero enviar datos de alguna aplicación de Android que escribí al Arduino y dejar que Arduino haga cosas dependiendo de los datos recibidos. Hasta ahora estoy enviando valores enteros entre 0 y 1024.
Desafortunadamente, mySerial.read() no terminará aquí. Lee los datos como una gran cadena.
Los pines RX y TX de mis módulos están conectados a los pines RX y TX de mi Arduino. (La consola no mostró ningún dato cuando los conecté a los puertos 10 y 11, así que pensé que así es como SoftwareSerial define el puerto RX y TX de Arduino...)
Mi boceto:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
char character;
void setup()
{
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() // run over and over
{
String Data = "";
while(mySerial.available()) {
character = mySerial.read();
Data.concat(character);
// Serial.println(Data);
Serial.println("foo");
}
Esto generará:
154888143201270341421501588671750825906975101110241005969922864792711629540451369290213140600
No puedo leer estos datos correctamente y no puedo analizar los datos en alguna función para manejarlos, porque mySerial.read() no llegará a su fin. Debe haber una mejor manera de recibir o enviar estos datos.
Mi fuente de flujo de salida de Bluetooth:
public void sendPWM(int progress) {
OutputStream tmpOut = null;
try {
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("error", "NO OUTPUT STREAM", e);
e.printStackTrace();
return;
}
try {
// byte[] buffer = stringToBytesUTFCustom("1");
byte[] buffer = String.valueOf(progress).getBytes();
tmpOut.write(buffer);
// tmpOut.flush(); /*I have turned this ON and OFF, to see if it
// makes any difference*/
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("error", "EXCEPTION DURING WRITE", e);
e.printStackTrace();
}
}
No cierro el outputStream, porque esto también cancelará la conexión con el HC-06 y tengo que enviar muchos datos. No puedo permitir que el usuario se vuelva a conectar al módulo cada vez que envía datos. ¿Me puede ayudar aquí?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String Data = "";
void setup()
{
mySerial.begin(9600);
}
void loop() // run over and over
{
while (mySerial.available())
{
char character = mySerial.read(); // Receive a single character from the software serial port
Data.concat(character); // Add the received character to the receive buffer
if (character == '\n')
{
Serial.print("Received: ");
Serial.println(Data);
// Add your code to parse the received line here....
// Clear receive buffer so we're ready to receive the next line
Data = "";
}
}
}
\n
...Sé que es un poco viejo, pero lo tengo funcionando.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String data = "";
void setup()
{
mySerial.begin(9600);
}
void loop() // run over and over
{
while(mySerial.available()==0)
{}
char character;
while(mySerial.available()>0)
{
character = mySerial.read();
mySerial.write(character);
}
data += character;
if (character == 13) {
mySerial.print("Received: ");
mySerial.println(data);
data = "";
}
}
PedroJ
lobo
+"\r\n"
a mi cadena. Esto solo agregará un descanso y no se detendrá.mySerial.read();
fm_andreas
lobo
fm_andreas