How to Serial Communication

Forum : Compare Hex values to output sound
HEX : 02 Data Data Data CheckSum 03 , but Variable Length.
Fixed Value : value[0] = 0x02, value[last] = 0x03

I don’t know how to approach it.

#include <SoftwareSerial.h>

//=======정의=======
#define STX 0x02
#define ETX 0x03

byte buttonStatus = 0;
String rxData={};
char countInput = 0;
boolean rxStart = false;
boolean rxEnd = false;

// commands
#define Fire Alarm 0x46 //F
#define Power Off Alarm 0x50 //P
#define IoT Terminal Error 0x54 //T

void setup(){
Serial.begin(9600);
Serial.println(“Enter AT commands:”);
}

void loop(){
if(Serial.available()> 0) {
delay(2);
rxData[0] = Serial.read();
if(rxData[0] == STX){
int i = 1;
while(Serial.available()){
delay(1);
rxData[i] = Serial.read();
if(rxData[i]> 127 || i>7) break;
if((rxData[i])==ETX) && rxEnd == true)) break;
i++;
}

}

}
int input;
input = Serial.read();

if(input != -1 ){
Serial.write(input);
Serial.write("\n");
}
}

void serialEvent(){
while(Serial.available()){
rxData[countInput] = Serial.read();

Serial.print(rxData);

 if (rxData[countInput] == 0x03) {
  rxEnd = true;
}
countInput++;

}
}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

Did you look at the SoftwareSerialExample example?

there are too many parenthesises on this line – compiler generates errors

1. Is the data item of the frame is ASCII Coded binary? If so, provide an example.
2. Assume that the following message frame has come from the remote sender:
021356753503 //STX Data1 Data2 Data3 CKSUM ETX

3. The above frame follows the below tansmission pattern where the digits of the message are in ASCII Coded bits except STX and ETX which remian in natural binary:
02 31 33 35 36 37 35 33 35 03

4. Program codes for receiver (untested):

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);   //SRX = DPin-2, STX = DPin-3
bool flag = false;
char myData[20];

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

void loop()
{
   byte n = SUART.available();  //check that a data item has arrived from sender
   if(n !=0)
   {
         if(flag != true)
         {
              byte x = SUART.read();
              if(x == 0x02)
              {
                   flag = true;                 //STX has arrived
              }
          }
          else
          {
                 byte m = SUART.readBytesUntil(0x03, myData, 20); //0x03 is not saved
                 mData[m] = '\0'; //insert null-charcater;
                 Serial.println(myData):   //shows received string
                 //-- add codes to recompute CHKSUM and check validity of data
                 //-- add codes to process received string
                 memset(myData, 0x00, 20);  //array is reset to 0s.
          }
   }
}

Compiler doesn’t care how many, just that they are correctly matched… To be readable
that line needs more whitespace and put the “break;” on the next line. :slight_smile: