Pages: 1 2 [3]   Go Down
Author Topic: Problems on serial.read()  (Read 1339 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey buddy!! Calm down, I'm not ignoring your suggestion. ...
However, I am grateful for your attention to solve my problem.

Two Three pages already, <sigh>.

Did you read the sticky?  Read this before posting a programming question


If you had you would have put your initial code into code tags. And read about the problems with the String class.



Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).



The first thing in solving a problem is trying to help yourself. Read the FAQ and the "sticky". Then you don't have to ask so much and wonder why people are getting a bit snippy.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

If the op post the full code that has the described issues are *you* willing to rework the String parts of the code to the C-style strings? I'm most interested to see if this will fix the current issues, or if there is something else causing the problem.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


* Captura de tela de 2012-11-24 14:03:40.png (62.49 KB, 1280x800 - viewed 11 times.)
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4773
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

What for example should that print? Is that making the 45#45#9 in the png?

What do those gets return?

example of printing different results from the same variable:
short x = 65; // same way with int but not byte or char
Serial.println(x); // prints 65, x as byte or char prints A
Serial.println((char) x); // prints A

Could your problem be due to this type of thing?
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4773
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see no obvious mistake but then I've only posted about parts of the code anyway.
I am trying not to dig into the whole thing and don't know what's in those libraries.
It's a matter of time and energy, I am not so young.

I see the 13448#88787#7878. 88787 is more than 16 bits.
Should that be possible from the get? Get like beacon.getDhost() type get?
Does the get return certain values on error?

Perhaps some more simple debug:

   long dbg = beacon.getDhost()); // unless getDhost() should return an int or a byte?
   Serial.print( dbg, HEX );

Sometimes print the return from a function is a bit different than I expect.

Sometimes when breaking code down to simpler I finally see what I didn't spell or arrange quite right.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial.print(beacon.getDhost());

What type does beacon.getDhost() return? Byte? Char? Int?
Logged

Pages: 1 2 [3]   Go Up
Jump to: