[SOLVED] Serial Communication - send and a receive a byte?

In the simplified example below, my Master sends out the address of a Slave, when the Slave receives its address it should turn on its LED, and turn it off if a different address is received. However this is not the case... I don't seem to be correctly sending and receiving the byte. I know there is Serial activity as I can flash the LED when serial is available. This is an RS485 network with a single Master and currently a single Slave.

MASTER

byte address1 = 0x31; //1
byte address2 = 0x32; //2

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

void loop()
{
   sendPacket(address1);
   delay(1000);
   sendPacket(address2);
   delay(1000);
}

void sendPacket(byte address)
{
  Serial.write(address);
}

SLAVE

byte address = 0x31; //1

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

void loop()
{
   if (Serial.available())
   {
     byte inByte = Serial.read();
     
     if (inByte == address)
     {
       digitalWrite(13, HIGH);
     }
     else
     {
       digitalWrite(13, LOW);
     }
   }
}

Blowed if I can see anything wrong with that, but

In the simplified example below

This isn't the real code is it?

Does that actual code work?


Rob

Change SLAVE to this and retest...

byte address = 0x31; //1

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

void loop()
{
   if (Serial.available())
   {
     byte inByte = Serial.read();
     
     if (inByte == address)
     {
       digitalWrite(13, HIGH);
     }
   }
}

Does the pin 13 LED light?

This isn't the real code is it?

Does that actual code work?

Yes - I'm really troubleshooting at the most basic level.

Change SLAVE to this and retest...

Still nothing.

I have now got rid of all circuits boards, and instead have two arduino's connected: RX->TX, TX->RX, sharing ground. Two LEDs on Pins 13 & 12. The code below, still fails to acknowledge the address byte on Pin 13, but pin 12 flash with each byte received....I really don't understand!

byte address = 0x31; //1
boolean serialActivity;

void setup()
{
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
   if (Serial.available())
   {
     if (serialActivity)
     {
       serialActivity = false;
       digitalWrite(12, LOW);
     }
     else
     {
       serialActivity = true;
       digitalWrite(12, HIGH);
     }
     
     byte inByte = Serial.read();
     
     if (inByte == address)
     {
       digitalWrite(13, HIGH);
     }
     else
     {
       digitalWrite(13, LOW);
     }
   }
}

To try & eliminate a hardware issue, set inByte to address after you read it from serial. Now does the LED come on?

void loop()
byte address = 0x31; //1
//.....

{
if (Serial.available())
{
byte inByte = Serial.read();

if (inByte == address)
{
digitalWrite(13, HIGH);
}

Well this wont work because the variable adress have inside a value 0x31 that in decimal is 49 (see ascii table)
inbyte variable will have a value 49 (serial.read returns a decimal)
So when you do the compare on the if (if (inByte == address) you are comparing 0x31 with 49 !!!! and so return false

0x31 does == 49 DEC, they are the same at the binary level we just chose to represent them differently on occasion to make code more human readable, but the processor doesn't care.


Rob

To try & eliminate a hardware issue, set inByte to address after you read it from serial. Now does the LED come on?

Sorry what do you mean by this?

He means add the line

inByte = address;

after you read the byte from the serial port. If it still doesn't work then the problem is local, if it now works you aren't getting the value you think you are.

This is how you divide and conquer to narrow down where the problem is.


Rob

OK so that lights the LED…
What could be causing me to receive the wrong value?

Don't know, how about you print out inByte (in HEX), often knowing what you do get points to the problem.


Rob

Alternatively, connect the master to your PC and use the serial monitor to see what it’s sending.

I was just about to ask how I could view that.

When the master is connected to the PC, the serial monitor outputs:
49504950495049504950495049504950.....

davivid:
49504950495049504950495049504950.....

In which case you are not running the code you posted.

I will still confirm your problem is because the diference between 0x31 and 49!!
This output is just another clue

49504950495049504950495049504950.....

Why not just try this on the slave:

int address = 1; //1
void loop()

//.....

{
   if (Serial.available())
   {
     int inByte = Serial.read();
    inByte = inByte - 48;
     if (inByte == address)
     {
       digitalWrite(13, HIGH);
     }

The led lights right?

:blush: yep.

When I changed circuits, the code some how got out of sync...

Thanks for all the help getting me there.

Serial.println() would stop that. At least then you'd get

49
50
49
50

Which might make sense if you looked at an ASCII table.