My last question is if I could had been broke my Arduino Due... because I comunicated a microcontroller with my Arduino Due. This microcontroller works to 5V and the arduino Due 3,3V.. If I sends a data from TX0 and arduino receives this data to TX1 this data is showed a lot of times and make rare things.
I send you my probe code (this is the very importan part of my complete code)... This program send automatically a data and if you conect TX0 with RX1 you must see the same data from serial port (in the program screen arduino). I need a lot of help with this problem... I am very desperate
I don't know if I broke this board. or my code is not exactly good. THANKS!!!
#include <Scheduler.h>
//TAMAÑO STRINGS (BUFFERS) DEL PUERTO SERIE//
static int buffer_0 = 64, buffer_1 = 64;
//STRINGS DEL PUERTO SERIE//
String inputString_0 = "", inputString_1 = "";
//FLAGS DEL PUERTO SERIE QUE INDICARÁN SI SE HAN RECIBIDO DATOS O NO//
boolean stringComplete_0 = false, stringComplete_1 = false;
char dato_periodico[208] = {0};//El dato periodico a formar del estilo "$PER,1000,0970,0800,0700...nnnn\r\n" tendrá una longitud de 207 bytes con los 40 datos entre sensores de luz y temperatura, la cabecera, las comas y el retorno de carro y fin de linea.
char dato_labview[] = "$ADQ,P00,CH0\r"; ;//Permite guardar la trama recibida desde el labview ("ADQ,Pxx,CHx\r\n")
char dato_pic_arduino[20] = {0};//Permite guardar la trama recibida desde cualqueir sensor ("ADQ,Pxx,CHx,nnnn\r\n") donde nnnn es el resultado del ADC
char dato_barrido[14] = "$ADQ,P00,CH0\r"; //Dato que se envia a los sensores para realiar el barrido. Permite tener una referencia del dato y solo ir cambiando los números de Pxx y CHx
char vector_numeros[11] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};//Vector de números para modificar el "dato_barrido"
char cabecera[5]={0};
//INDICES BUCLES FOR//
int canal_sensor = 0;
int unidades_sensor = 0;
int decenas_sensor = 0;
int x = 0;
int o = 0;
int p = 0;
int m = 0;
int n = 0;
int i = 0;
//INDICES FUNCIONES LECTURA PUERTO SERIE//
int P_R0 = 0;
int P_R1 = 0;
//VARIABLES PARA GUARDAR EL INICIO Y FINAL DEL COMIENZO DEL DATO DENTRO DEL STRING VARIABLE DEL PUERTO SERIE//
int inicio0 = 0;
int final0 = 0;
int inicio1 = 0;
int final1 = 0;
//FLAGS//
boolean flag_RecepcionPuertoSerie0 = false;
boolean flag_RecepcionPuertoSerie1 = false;
boolean writeComplete_1 = false;
boolean writeComplete_0 = false;
void setup() {
pinMode (13, OUTPUT); //Éste pin permite activar el envio o recepcion de datos por el MAX485
digitalWrite(13, LOW);//Inicialmente se tiene en ESCUCHA
Scheduler.startLoop(RecepcionPuertoSerie0);
Scheduler.startLoop(RecepcionPuertoSerie1);
//INICIO PUERTO SERIE0 Y PUERTO SERIE1//
Serial.begin(9600);
Serial1.begin(9600);
//buffers para el puerto serie//
inputString_0.reserve(buffer_0);
inputString_1.reserve(buffer_1);
//se limpia el puerto serie inicialmente//
Serial.flush();
Serial1.flush();
}
void loop() {
for (canal_sensor = 0; canal_sensor < 2; canal_sensor++) {//Éste primer FOR permite cambiar el canal CHx (CH0 ó CH1)
dato_barrido[11] = vector_numeros[canal_sensor];// el dato a cambiar está en la posición 11 del dato_barrido
for (decenas_sensor = 0; decenas_sensor < 2; decenas_sensor++) {//Éste segundo FOR permite cambiar el número de sensor DECENAS (P0x o P1x)
dato_barrido[6] = vector_numeros[decenas_sensor]; //el dato a cambiar es´ta en la posición 6 del dato_barrido
for (unidades_sensor = 0; unidades_sensor < 10; unidades_sensor++) {//Éste tercer FOR permite cambiar el número de sensor UNIDADES (Px0,Px1,Px2...Px9)
dato_barrido[7] = vector_numeros[unidades_sensor]; //el dato a cambiar está en la posición 7 del dato barrido
digitalWrite(13, HIGH);// Ponemos el MAX485 en MODO ESCRITURA
Serial.print(dato_barrido);// SE ENVIA POR EL PUERTO SERIE 0 el dato de solicitud para que el sensor correspondiente responda
digitalWrite(13, LOW);//Ponemos el MAX485 en MODO ESCUCHA
delay(8000);//Esperamos un tiempo suficientemente grande que el dato se procese en el PIC y éste lo envie por su puerto serie
//MontajeTramaPeriodica();
}
}
}
}
void RecepcionPuertoSerie0() {
if (Serial.available()) {
char inChar_0 = (char)Serial.read();
inputString_0 += inChar_0;
if ((inChar_0 == '\r') || (inputString_0.length() == buffer_0)) {
stringComplete_0 = true;
}
if (stringComplete_0 == true) { //Solo se realiza si hay algun dato disponible del puerto serie0
for (P_R0 = 0; P_R0 < buffer_0; P_R0++) {
if (inputString_0[P_R0] == '
) { //se comprueba si hay algun dato dentro del string del puerto serie0 que contenga "$" al inicio
inicio0 = P_R0; //copiamos ubiación dentro del string INICIO
for (P_R0 = inicio0; P_R0 < buffer_0; P_R0++) {
if (inputString_0[P_R0] == '\r') { //se comprueba si hay algun dato dentro del string del puerto serie0 que contenga "\r" al final
final0 = P_R0; //copiamos ubicación FIN
stringComplete_0 = false;
writeComplete_0 = true;
break; //rompemos ya el bule
}
}
break;//rompemos ya el bucle
}
}
}
if (writeComplete_0 == true) {
for (n = inicio0; n <= final0; n++) { //se indica el INICIO y FIN donde se encuentra la trama dentro del string del puerto serie0
dato_pic_arduino[m] = inputString_0[n]; //se van copiando los datos del string en otra avriable
m++;
}
writeComplete_0 = false;
flag_RecepcionPuertoSerie0 = true;
Serial.print(dato_pic_arduino);
inputString_0 = ""; //se reinicia el string que guarda datos en el puerto serie0
n = 0;
m = 0;
}
}
yield();
}
void RecepcionPuertoSerie1() {
if (Serial1.available()) {
char inChar_1 = (char)Serial1.read();
inputString_1 += inChar_1;
if ((inChar_1 == '\r') || (inputString_1.length() == buffer_1)) {
Serial.flush();
stringComplete_1 = true;
}
if (stringComplete_1 == true) { //Solo se realiza si hay algun dato disponible del puerto serie0
for (P_R1 = 0; P_R1 < buffer_1; P_R1++) {
if (inputString_1[P_R1] == '
) { //se comprueba si hay algun dato dentro del string del puerto serie0 que contenga "$" al inicio
inicio1 = P_R1; //copiamos ubiación dentro del string INICIO
for (P_R1 = inicio1; P_R1 < buffer_1; P_R1++) {
if (inputString_1[P_R1] == '\r') { //se comprueba si hay algun dato dentro del string del puerto serie0 que contenga "\r" al final
final1 = P_R1; //copiamos ubicación FIN
stringComplete_1 = false;
writeComplete_1 = true;
break; //rompemos ya el bule
}
}
break;//rompemos ya el bucle
}
}
}
if (writeComplete_1 == true) {
for (o = inicio1; o <= final1; o++) { //se indica el INICIO y FIN donde se encuentra la trama dentro del string del puerto serie0
dato_labview[p] = inputString_1[o]; //se van copiando los datos del string en otra avriable
p++;
}
writeComplete_1 = false;
// flag_RecepcionPuertoSerie1 = true;
delay(2000);
Serial.print(dato_labview);
inputString_1 = ""; //se reinicia el string que guarda datos en el puerto serie0
o = 0;
p = 0;
}
}
yield();
}