Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 24, 2012, 01:44:20 pm
OK. I trying to make multhops. I have three arduinos UNO.
I calling them ard_5 (id = 2), ard_8 (id = 1) and ard_3 (id = 3).
So, if you look on my first post you'll se that I put my beacon.ccp code.
Now everything on every code that had "int " or "short" I changed for "byte".

Good news!!! I am getting 2#1#0 at my first hop from ard_5 arduino to ard_8!!!
Like this ard_8 (2#1#0) -> ard_5(2#1#0) -> ard_5(3#1#0)-> ard_x(3#1#0), that is:

ard_8 just send, ard_5 receives from ard_8 and send for ard_x changing the destination, but keeping the source.

Howerver, with ard_x, I am getting like this ( 13448#88787#7878)

ard_8
Code:
#include <Service.h>
#include <Beacon.h>

//Arduino 08 -> ard_8 -> Somente envia!!!

Beacon beacon;
Service service;

// Identidade dos Arduinos
byte ard_8 = 1; //Arduino 08
byte ard_5 = 2; //Arduino 05
byte ard_x = 3; //arduino X


void setup() {     
  // comunicação XBEE a taxa de 9600 bps
   Serial.begin(9600); 
}


void loop() {
         
           
           beacon = Beacon(ard_5, ard_8, 0);
           service.enviaBeacon(beacon);
           delay(2000);
               
}

ard_5
Code:

#include <Beacon.h>
#include <Service.h>

Beacon beacon;
Beacon recebe;
Service service;

byte ard_8 = 1; //Arduino 08
byte ard_5 = 2; //Arduino 05
byte ard_x = 3; //arduino X


void setup() {     
   Serial.begin(9600); 
}


void loop() {
 
             recebe = service.recebeBeacon(ard_5);
           
             if (recebe.getDhost() == ard_5){
             delay(2000);
             //byte i = recebe.getSALTO()+1;
             beacon = Beacon(recebe.getDhost(), recebe.getShost(), recebe.getSALTO());         
             service.converteBeaconParaArrayChar(beacon);
               
              if (recebe.getDhost() == ard_5){
                  Serial.println("Ponto N");
                  beacon = Beacon(ard_x, ard_8,recebe.getSALTO() );
                  service.enviaBeacon(beacon);                 
              } 
    }     
}

ard_x
Code:
#include <Service.h>
#include <Beacon.h>

Service service;
Beacon beacon;
Beacon recebe;

byte ard_8 = 1; //Arduino 08
byte ard_5 = 2; //Arduino 05
byte ard_x = 3; //arduino X

//int led = 13;

void setup() {     
   Serial.begin(9600); 
 //  pinMode(led, OUTPUT);
}

void loop() {
 
         
              recebe = service.recebeBeacon(ard_x);
           
              if (recebe.getDhost() == ard_x){
              delay(2000);
         
              beacon = Beacon(recebe.getDhost(), recebe.getShost(), recebe.getSALTO());         
              service.converteBeaconParaArrayChar(beacon);
          }

One more thing, I changed my while from method converteArrayCharParaBeacon to:

Code:

byte  i=0;
byte vet[3];
Beacon beacon;


for (i=0; i <= 2; i++){
    vet[i] = msg[2 * i] - '0';
    i++;
}

Do you see any mistake?
2  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 24, 2012, 11:04:19 am
OK guys, I followed your advice and now I'm using C string. However, I am getting some problen, at least less than the others!
I put some debugs in tge code to identify where are them. So, I identified that the problem is between the points
"Ponto 3------------" and "Ponto 3.1------------", that is, on the method
Code:
void Service::converteBeaconParaArrayChar(Beacon beacon)

I won't put my Beacon.cpp e .h again. I just have to say that I changed all my "short's" to "byte's"

Here is my Service.cpp
Code:

#include "Service.h"
#include "Beacon.h"
#include <stdio.h>

byte meu_id; //identificação do no raiz

//Estrutura do pacote de controle
Service::Service() {
}

Service::~Service() {/*nothing to destruct*/
}

void Service::enviaBeacon(Beacon beacon) {
if(beacon.getDhost() != 0){
  converteBeaconParaArrayChar(beacon);
}

}

Beacon Service::recebeBeacon(short id) {

Beacon beacon;
int i = 0;
char msg[20];

while(Serial.available() > 0) {
byte temp = Serial.read();
if (temp != 255){
msg[i] = (char) temp;
i = i + 1;
}
}
Serial.println("Ponto_1 -----------------");
Serial.println(msg);
/* Aqui to recebendo 2#1#0 redondinho*/

msg[i] = '\0';
if (i > 0) {
  beacon = converteArrayCharParaBeacon(msg);

Serial.println("Ponto_2-----------------");
  Serial.println(msg);
 
  delay(1000);
  Serial.flush();

if (verificaMensagemBeacon(beacon,id)) {
return beacon;
}
}
}
void Service::converteBeaconParaArrayChar(Beacon beacon) {
Serial.println("Ponto_3-----------------");

Serial.print(beacon.getDhost());
Serial.print("#");
Serial.print(beacon.getShost());
Serial.print("#");
Serial.println(beacon.getSALTO());

/* Aqui to recebendo 2#1#0 e num aleat.*/
Serial.println("Ponto_3.1-----------------");
}


Beacon Service::converteArrayCharParaBeacon(char msg[]) {
int c =0;
int  i=0;
short vet[3];
Beacon beacon;

while (msg[i] != '\0') {
if (msg[i] != '#') {
vet[c] = vet[c] * 10 + (msg[i] - '0');
}
else {
c = c + 1;
}
i = i + 1;
}
Serial.println("Ponto_4-----------------");
Serial.println(msg);

beacon.setDhost(vet[0]);
beacon.setShost(vet[1]);
beacon.setSALTO(vet[2]);

return beacon;
}

bool Service::verificaMensagemBeacon(Beacon beacon, short id) {

      //Verifica se a mensagem é para ele.
if (beacon.getDhost() == id) {
  return true;
}

return false;
}



I dont have any ideia why in the "void Service::converteBeaconParaArrayChar(Beacon beacon)" is doing what I attached in the picture?

Does anyone knows?

thanks
3  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 21, 2012, 03:55:12 pm
Hey buddy!! Calm down, I'm not ignoring your suggestion. You can note that I am having difficult to solve it!! Could you be a little bit humble and pacient??? I am new here, ok?
I'm being humble, I am always being grateful four your help!!! So, calm down!! If you do not want to help, ok? I think it would have more people wanting to help.
However, I am grateful for your attention to solve my problem.
4  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 21, 2012, 03:06:18 pm
Hi guys, I've been really diligent to solve this problem and I have to appreciate your help. A good news is that 50% of the problem was solved. In the code I changed what you suggested below on methods "converteBeaconParaString", "enviaBeacon" and "converteStringParaBeacon". Look that in the last now I looking for the "#" ah hand an
Code:
#include "Service.h"

#include "Beacon.h"
#include <stdio.h>
#include <string.h>


short meu_id; //identificação do no raiz

//Estrutura do pacote de controle
Service::Service() {
}

Service::~Service() {/*nothing to destruct*/
}

void Service::enviaBeacon(Beacon beacon) {
if(beacon.getDhost() != 0){
  converteBeaconParaString(beacon);
}
}

Beacon Service::recebeBeacon(short id) {

Beacon beacon;
String msg;

while(Serial.available() > 0) {
byte temp = Serial.read();
if (temp != 255){
msg = msg + (char) temp;
}

}
if (msg.length() > 0) {
  beacon = converteStringParaBeacon(msg);
  delay(1000);
  Serial.flush();

if (verificaMensagemBeacon(beacon,id)) {
return beacon;
}
}
return beacon;
}


void Service::converteBeaconParaString(Beacon beacon){

Serial.print(beacon.getDhost());
Serial.print("#");
Serial.print(beacon.getShost());
Serial.print("#");
Serial.println(beacon.getSALTO());
}


Beacon Service::converteStringParaBeacon(String msg) {
int c=0;
int i=0;
short vet[3];
Beacon beacon;

while (msg[i] == '\0') {
if (msg[i] != '#') {
vet[c] = vet[c] * 10 + (msg[i] - '0');
}
else {
c = c + 1;
}
i = i + 1;
}

beacon.setDhost(vet[0]);
beacon.setShost(vet[1]);
beacon.setSALTO(vet[2]);

return beacon;
}


bool Service::verificaMensagemBeacon(Beacon beacon, short id) {

if (beacon.getDhost() == id) {
  return true;
}

return false;
}

Now, when I send the information from ard_8 -> ard_5, I get what I wanted (2#1#0+1). 50% of the problem solved!!!!
HOWEVER, when I get this information (2#1#0+1) and change de destination for ard_x, the thrird arduino, I get like this (76656#56663#88877)
Really, annoying!!!

Any suggestion?

Thanks a lot folks.
5  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 07:49:47 pm
Ok guys, I'll work on it!!!
I would search for C String and choose one wich fits better.

thanks a lot
6  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 07:27:54 pm
And the reason to not use it.
Can you explain me and give me a tip about how to use what do you think is better in my code?
7  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 07:25:44 pm
And use what?? You didnt tell me the suggestion!!! smiley
Array of bytes, char... ???
8  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 07:16:18 pm
Ok, here is my whole code:
What suggestion you give me with String class??
thanks

Code:
#include "Beacon.h"

#include <stdio.h>
#include <string.h>


Beacon::Beacon() {
 
}

Beacon::Beacon(short meu_id) {
this->dhost = 0; // host de destino
this->shost = 0; // host de origem
this->salto = 0;

}

Beacon::Beacon(short dhost, short shost, short salto) {
this->dhost = dhost;
this->shost = shost;
this->salto = salto;

}

Beacon::~Beacon() {/*nothing to destruct*/
}


short Beacon::getDhost() const {
return dhost;
}

short Beacon::getShost() const {
return shost;
}

short Beacon::getSALTO() const {
return salto;
}

void Beacon::setDhost(short dhost) {
this->dhost = dhost;
}

void Beacon::setShost(short shost) {
this->shost = shost;
}

void Beacon::setSALTO(short salto) {
this->salto = salto;
}

Code:
#ifndef BEACON_H_
#define BEACON_H_

#include <Arduino.h>
#include <string.h>

class Beacon {
public:
Beacon();
Beacon(short meu_id);
Beacon(short dhost,short shost, short salto);
~Beacon();
    short getDhost() const;
    short getShost() const;
    short getSALTO() const;
    void setDhost(short dhost);
    void setShost(short shost);
    void setSALTO(short salto);
   
private:
short dhost;
short shost;
short salto;
};

#endif

Code:
#include "Service.h"

#include "Beacon.h"
#include <stdio.h>
#include <string.h>


short meu_id; //identificação do no raiz

//Estrutura do pacote de controle
Service::Service() {
}

Service::~Service() {/*nothing to destruct*/
}

void Service::enviaBeacon(Beacon beacon) {
if(beacon.getDhost() != 0){
  String msg = converteBeaconParaString(beacon);
  Serial.println(msg);
}

}

Beacon Service::recebeBeacon(short id) {

Beacon beacon;
String msg;

while(Serial.available() > 0) {
byte temp = Serial.read();
if (temp != 255){
msg = msg + (char) temp;
}

}
if (msg.length() > 0) {
  beacon = converteStringParaBeacon(msg);
  delay(1000);
  Serial.flush();

if (verificaMensagemBeacon(beacon,id)) {
return beacon;
}
}
return beacon;
}

// Funcao que converte inteiro para String. Arduino trata somente string
String Service::converteBeaconParaString(Beacon beacon) {
char buf[10];
sprintf(buf, "%d#%d#%d#", beacon.getDhost(), beacon.getShost(), beacon.getSALTO());
return buf;
}


Beacon Service::converteStringParaBeacon(String msg) {
int c, i;
short vet[3];
Beacon beacon;
for (c = i = 0; c < 3; c++) {
vet[c] = (short) msg.substring(i, msg.indexOf('#', i)).toInt();
i = msg.indexOf("#", i) + 1;
}

beacon.setDhost(vet[0]);
beacon.setShost(vet[1]);
beacon.setSALTO(vet[2]);

return beacon;
}


bool Service::verificaMensagemBeacon(Beacon beacon, short id) {

      //Verifica se a mensagem é para ele.
if (beacon.getDhost() == id) {
  return true;
}

return false;
}


Code:
#ifndef SERVICE_H_
#define SERVICE_H_

#include <Arduino.h>
#include <string.h>
#include <Beacon.h>


class Service {
public:
Service();
~Service();
    String converteBeaconParaString(Beacon beacon);
    Beacon converteStringParaBeacon(String msg);
    void enviaBeacon(Beacon beacon);
    Beacon recebeBeacon(short id);
    bool verificaMensagemBeacon(Beacon beacon,short id);

};

#endif

//Arduino that sends the packet
Code:
#include <Service.h>
#include <Beacon.h>

Beacon beacon;
Service service;

short ard_8 = 1; //Arduino 08
short ard_5 = 2; //Arduino 05



void setup() {     
   Serial.begin(9600); 
}


void loop() {
         
           //Enviar beacon com D(ard_5), S(ard_8), 0
           beacon = Beacon(ard_5, ard_8, 0);
           service.enviaBeacon(beacon);
           delay(2000);
               
}

//Arduino that receives packet (The problem is here)
Code:
#include <Beacon.h>
#include <Service.h>

Beacon beacon;
Beacon recebe;
Service service;

short ard_8 = 1; //Arduino 08
short ard_5 = 2; //Arduino 05



void setup() {     
   Serial.begin(9600); 
}


void loop() {
 
           recebe = service.recebeBeacon(ard_8);
           
           delay(2000);
           
           //recebe.getSALTO() ta dando problema no metodo recebe que nao sei qual e. Isso ta afetando o getSALTO()
            beacon = Beacon(recebe.getDhost(), recebe.getShost(), recebe.getSALTO());         
           Serial.println(service.converteBeaconParaString(beacon));
9  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 07:02:35 pm
Ok Ok, I agree with your comment!!!
I really need that when the arduino get the packet, it doen not come with so much garbage. I think the problem is with the "recebe" method.
But I really do not know what is happening. One told me to handle XCTU for xbee, I'm looking for information about this.
What do you think??

thanks
10  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 06:43:03 pm
Hey, about the 255 code I've  put this, but it doesnt function!!!

Code:
Beacon beacon;
String msg;

while(Serial.available() > 0) {
byte temp = Serial.read();
if (temp != 255){
msg = msg + (char) temp;
}

}

Do you have more suggestion?

Thanks folks,
11  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 02:47:03 pm
Code:
Beacon Service::converteStringParaBeacon(String msg) {
int c, i;
short vet[3];
Beacon beacon;
for (c = i = 0; c < 3; c++) {         [b]  // If I put c<3 and not c<2 it get worst [/b]
vet[c] = (short) msg.substring(i, msg.indexOf('#', i)).toInt();
i = msg.indexOf("#", i) + 1;
}

beacon.setDhost(vet[0]);
beacon.setShost(vet[1]);
beacon.setSALTO(vet[2]);

return beacon;
}

Look at the picture!!! At leat with C<2 I still get in the midle of garbage 2#1#0# ahuahuahah

But, I thank you so much for help. Please, stay with me! smiley

12  International / Portugues / Re: Problemas com serial.read() on: November 16, 2012, 02:39:57 pm
Olá amigão,

Então não é problema de bauds, já usei a 19200 e 9600 e a 19.200 acontece a mesma coisa.
Nunca mexi nessa parte de configuração. Vou tentar ler estes tutoriais que você me passou? Não lembra mesmo o que você fez?? :/

muito obrigado.

13  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 02:23:43 pm
@ Sergio

I appreciate your attention. The bug is already corrected I've put
Code:
short vet [3];

@smarcelo
Thanks too. I didnt understand, but can you help me solve it? Do you know how to solve it?
I'd appreciate.
14  International / Portugues / Re: Problemas com serial.read() on: November 16, 2012, 10:28:38 am
Olá amigo, obrigado pela atenção.

Então, o que eu estou fazendo é usar tres cabos seriais, do tipo de cabo de impressora, um em cada arduino.
Os tres arduinos tem um skield Xbee, mas eles estao conectados no computador. Faço a comunicação assim, para depois poder usar eles com pilha!!
Não configurei Xbee, você teve problema parecido? Pode me explicar como fez?

abs e muito obrigado.
15  Using Arduino / Programming Questions / Re: Problems on serial.read() on: November 16, 2012, 10:17:54 am
1) char array´s.
How can I do it, please? Can you give a tip?

I think if you see the whole code it´ll get clear;
This is my Beacon.cpp
Code:

#include "Beacon.h"
#include <stdio.h>
#include <string.h>

Beacon::Beacon() {
}

Beacon::Beacon(short meu_id) {
this->dhost = 0; // host de destino
this->shost = 0; // host de origem
this->salto = 0; // (metrica SALTO, numero de saltos para cada no)
//this->meu_id = meu_id;
}

Beacon::Beacon(short dhost, short shost, short salto) {
this->dhost = dhost; // destination host
this->shost = shost; // source host
this->salto = salto; // hops

}

This is my Service.cpp
Code:
//send beacon
void Service::enviaBeacon(Beacon beacon) {
if(beacon.getDhost() != 0){
  String msg = converteBeaconParaString(beacon);
  Serial.println(msg);
}

}
//receive beacon
Beacon Service::recebeBeacon(short id) {

Beacon beacon;
String msg;

while(Serial.available() > 0) {
byte temp = Serial.read();
msg = msg + (char) temp;

}
Serial.flush();


if (msg.length() > 0) {
beacon = converteStringParaBeacon(msg);
delay(1000);
Serial.flush();


if (verificaMensagemBeacon(beacon,id)) {
return beacon;
}
}
return beacon;
}
//convert Beacon to String
String Service::converteBeaconParaString(Beacon beacon) {
char buf[10];
sprintf(buf, "%d#%d#%d#", beacon.getDhost(), beacon.getShost(), beacon.getSALTO());
return buf;
}

//convert  String to Beacon
Beacon Service::converteStringParaBeacon(String msg) {
int c, i;
short vet[2];
Beacon beacon;
for (c = i = 0; c < 2; c++) {
vet[c] = (short) msg.substring(i, msg.indexOf('#', i)).toInt();
i = msg.indexOf("#", i) + 1;
}

beacon.setDhost(vet[0]);
beacon.setShost(vet[1]);
beacon.setSALTO(vet[2]);

return beacon;
}

Clear now? Any suggestion?

Thank a lot.
Pages: [1] 2