Pages: [1] 2   Go Down
Author Topic: Serial Programming question  (Read 988 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
[code][code]
void setup()
{
  Serial.begin(9600);
  Serial1.begin(300);
}

void loop()
{
  int sync;
  char data,fcs;
  if(Serial1.available())
  {
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0xaa)
    {
      if(Serial1.available())
      {
      data=Serial1.read();
      //fcs=Serial1.read();
      Serial.print(data);
      }
    //if((data+fcs)==0)
    //{
     // Serial.print(data);
    //}
    }
   
  }
}

I am trying to establish communication between my ATMega32 and Arduino Mega. My atmega is outputting characters of this format "aAaBaCaD" and repeats it at BAUD 300. What I want my arduino to do, is read the incoming data on Serial1, if it is "a" than read the next byte ie."A" and then only it is printed through Serial port to the computer.
And I am using this code to achieve that


But the out put  "some 2 dots on y character" that is repeating on the serial monitor(gibberish).

So I used this code

Code:
void setup()
{
  Serial.begin(9600);
  Serial1.begin(300);
}
void loop()
{
  char i;
  if(Serial1.available())
  {
    i=Serial1.read();
    Serial.print(i);
    Serial.print("\n");
  }
}


 to test what is being output from the Atmega and I got the format
"a
A
a
B
a
C
a
D
a
A
.
.

What could be wrong with the first code?

P.S: I need the sync byte and fcs(not used, but intend to use ) as I plan to do use 315MHZ RF communication.[/code][/code]
« Last Edit: December 07, 2012, 03:23:32 pm by cyberhawk9 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
A y-umlaut is in my experience a classic sign
..of reading a character that isn't there and trying to print 0xff.
Mismatched rates usually result in varied junk.
OP, are you certain that is your code?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

I corrected the format.I got the \ / confused. And that code is working(second part).
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I corrected the format.I got the \ / confused
What does that mean?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

I used "\code" instead of "/code".
« Last Edit: December 07, 2012, 03:31:06 pm by cyberhawk9 » Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if(Serial1.available())
  {
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0xaa)
    {
      if(Serial1.available())
      {
        data=Serial1.read();

I can't see how that code can ever work. As soon as a character is available, you check to see if it is the sync byte. If it is, you check immediately whether another character is available. It won't be, you are sending data at 300 baud so it will take around 33ms for the next character to arrive. You need to wait until each character is available. You'll also need some way of knowing when you have reached the end of the message, so you can go back to waiting for the sync byte.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if(sync==0xaa)

Code:
a
A
a
B
a
C
a
D
a
A

Are you aware that 'a' is not the same as 0xAA?
Logged

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

yes I am aware. 0xaa is not ASCII 'a'. I tried all the possibilities of using 'a', 0xaa, other formats, on both the ends(atmega and arduino as sync bit).

Code:
  if(Serial1.available())
  {
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0xaa)
    {
      if(Serial1.available())
      {
        data=Serial1.read();

I can't see how that code can ever work. As soon as a character is available, you check to see if it is the sync byte. If it is, you check immediately whether another character is available. It won't be, you are sending data at 300 baud so it will take around 33ms for the next character to arrive. You need to wait until each character is available. You'll also need some way of knowing when you have reached the end of the message, so you can go back to waiting for the sync byte.

I thought inserting 
if(Serial1.available()) before data=Serial1.read() would tell arduino to wait until the next byte is available.
« Last Edit: December 07, 2012, 03:53:32 pm by cyberhawk9 » Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought inserting 
if(Serial1.available()) before data=Serial1.read() would tell arduino to wait until the next byte is available.


You seem to be under the impression that Serial communication is fast; It's not. The second

if(Serial1.available())

will happen eons before the next serial byte is received. You either need to make it blocking, implement a state machine with some sort of end of packet marker and a buffer to store all of the data, or check for more than one byte is available the first time.
Logged

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



You seem to be under the impression that Serial communication is fast; It's not. The second

if(Serial1.available())

will happen eons before the next serial byte is received. You either need to make it blocking, implement a state machine with some sort of end of packet marker and a buffer to store all of the data, or check for more than one byte is available the first time.
So how do I make it wait till the byte after the sync byte arrives?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

By looping until data is available.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

By looping until data is available.
Code:
  if(Serial1.available())
  {
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0x02)
    {
      while(~Serial1.available());
      data=Serial1.read();
      //fcs=Serial1.read();
      Serial.print(data);
     
    //if((data+fcs)==0)
    //{
     // Serial.print(data);
    //}
    }
   
  }
doesn't solve the problem
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

BITNOT Serial.available()
Is going to be true a lot of the time.
Try logical NOT.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

doesn't solve the problem

How about some debug prints?

Code:
if(Serial1.available())
  {
    sync=Serial1.read();
    if(sync==0x02)
    {
      ...
    }
    else
    {
      Serial.print("The byte I received was not a sync byte: ");
      Serial.println(sync);
    }
  }

It's also worth noting that Serial.available() doesn't return a boolean, it returns a number of bytes in the buffer. The proper way to write it is:

Code:
if (Serial.available() > 0)
Logged

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


How about some debug prints?

Code:
if(Serial1.available())
  {
    sync=Serial1.read();
    if(sync==0x02)
    {
      ...
    }
    else
    {
      Serial.print("The byte I received was not a sync byte: ");
      Serial.println(sync);
    }
  }

It's also worth noting that Serial.available() doesn't return a boolean, it returns a number of bytes in the buffer. The proper way to write it is:

Code:
if (Serial.available() > 0)
I will remember those pointers next time. I was using LED on 13 for debugging. I will use debug prints next time.

For now this solved the problem
Code:
void setup()
{
  Serial.begin(9600);
  Serial1.begin(300);
}

void loop()
{
  int sync;
  char data,fcs;
  if(Serial1.available())
  {
    sync=Serial1.read();
    //Serial.print(sync);
    if(sync==0xAA)
    {
      while(!Serial1.available());
      data=Serial1.read();
      //fcs=Serial1.read();
      Serial.print(data);
     
    //if((data+fcs)==0)
    //{
     // Serial.print(data);
    //}
    }
   
  }
}

Thank you. I have been struggling with this problem for many hours now. Seeking help here got it solved in minutes

Now I will go on to test the RF receiver and transmitter module
« Last Edit: December 07, 2012, 04:24:20 pm by cyberhawk9 » Logged

Pages: [1] 2   Go Up
Jump to: