Go Down

Topic: Serial Programming question (Read 1 time) previous topic - next topic

Dec 07, 2012, 09:04 pm Last Edit: Dec 07, 2012, 09:23 pm by cyberhawk9 Reason: 1
Code: [Select]
[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: [Select]

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]

AWOL

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?
"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.

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

AWOL

Quote
I corrected the format.I got the \ / confused

What does that mean?
"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.

#4
Dec 07, 2012, 09:29 pm Last Edit: Dec 07, 2012, 09:31 pm by cyberhawk9 Reason: 1
I used "\code" instead of "/code".

dc42

Code: [Select]

  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.
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.

Arrch

Code: [Select]
    if(sync==0xaa)

Code: [Select]
a
A
a
B
a
C
a
D
a
A


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

#7
Dec 07, 2012, 09:48 pm Last Edit: Dec 07, 2012, 09:53 pm by cyberhawk9 Reason: 1
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: [Select]

  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.

Arrch


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.




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?

AWOL

By looping until data is available.
"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.


By looping until data is available.

Code: [Select]

  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

AWOL

BITNOT Serial.available()
Is going to be true a lot of the time.
Try logical NOT.
"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.

Arrch


doesn't solve the problem


How about some debug prints?

Code: [Select]
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: [Select]
if (Serial.available() > 0)

#14
Dec 07, 2012, 10:22 pm Last Edit: Dec 07, 2012, 10:24 pm by cyberhawk9 Reason: 1


How about some debug prints?

Code: [Select]
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: [Select]
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: [Select]

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

Go Up