serial port data read

hello, with arduino, I want to run the relay according to the hex data on the serial port from another device. Could you help

“Hex data” doesn’t adequately describe the incoming data. What values are being received, how are the delimited, which ones do you expect to turn the relay on and which turn it off?

With such a well-defined set of requirements, I don't see how this project will be anything but a resounding success.

ykilicaslan:
hello, with arduino, I want to run the relay according to the hex data on the serial port from another device. Could you help

You need to provide a lot more information.

  • What is the project you are trying to create? It is much easier to help when we can see the big picture.

  • What hex data?

  • Is the data being received as hex characters or is it being received as binary data? For example is the data the characters "4D" or "0x4D" or is it binary 01001101?

  • What program is sending the data?

  • Have you written that program?

  • In what circumstances should the relay be turned on or off?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

I would like to assume that UNO is the receiver and NANO is the sender. Nano will send 0x12 to UNO over soft UART Port (SUART); once UNO receives the data byte, it will activate a relay connected at DPin-5.

Sketch for UNO

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);   //SRX = DPin-2 of UNO; STX = DPin-3 of UNO
void setup()
{
    SUART.begin(9600);      //SUART Port is created and enabled at 9600 bits/sec    
    pinMode(5, OUTPUT);    //relay pin
    digitalWrite(5, LOW);     //relay is OFF
}

void loop()
{
    byte n = SUART.available();
    if(n !=0 )
    {
          byte x = SUART.read();
          if(x == 0x12)   //0x12 has come from NANO
          {
               digitalWrite(5, HIGH);    //relay is ON
          }
    }
}