// change address 01 to 09
#define DE_RE_PIN 18      // DE and RE pins linked to GPIO 4
#define MODE_SEND HIGH   // HIGH for transmitting
#define MODE_RECV LOW    // LOW for receiving
byte ByteArray[250];int ByteData[20];

unsigned long rawValue = 0;
float finalValue;
float voltage;

void set_address_dds6619() {
  
  uint8_t buff[] = {

    0x01, // Original  address device
    0x10, // Write Multiple holding register
    //---------address of device id------------2 byte----
	0x00,  
    0x08,
    //----------- 	Quantity---- 2	
    0x00,  
    0x02, 
	//------------ byte count --- 4
    0x04,
	//------------- data 0x41100000 = address 09
    0x41,
    0x10,
    0x00,
    0x00,
	//---------------- crc  byte high(0xe7)  crc byte low(0xf0) 
    0xe7,
    0xf0
     
  };

  digitalWrite(DE_RE_PIN, MODE_SEND); // Enable transmission
  Serial2.write(buff, sizeof(buff));   // Send the command via Serial2
  delay(5);
  Serial2.flush();                     // Wait for send to complete
  digitalWrite(DE_RE_PIN, MODE_RECV);  // Enable reception

  int a = 0;

  while(Serial2.available()){
    ByteArray[a] = Serial2.read();
    a++;
  }

  int b=0;
  String Register;
  Serial.println("Receiving Data...");
  for(b=0;b<a;b++){
    Serial.print("[");
    Serial.print(b);
    Serial.print("]");
    Serial.print("=");

    Register = String(ByteArray[b],HEX);
    Serial.print(Register);
    Serial.print(" ");
    
   }
  //}


  Serial.println();


}

void setup() {
  pinMode(DE_RE_PIN, OUTPUT);
  digitalWrite(DE_RE_PIN, MODE_RECV); // Start in receive mode
  Serial.begin(115200);               // Serial monitor output
  Serial2.begin(9600, SERIAL_8N1, 19, 21); // Initialize Serial2 with standard Modbus settings
  Serial2.setTimeout(200);            // Set a timeout for response
 
  set_address_dds6619();

  while(1);
}

void loop() {
 

}
