I was done all the correction in my code and the output is given below for your reference, and if i change register address 74 instead of 75 i am getting nothing so i don't change register address, here i have two problems why the "j"(unsigned int) value does not increase to greater than 9. then output line print ovf why?oh sorry i forget to the device address, register address bytes, and length of the bytes, then also forget to write the j++ lines after the serial print function if i upload my above program to my Arduino definitely that will confusing me for some minutes so thanks for point out my mistakes, i will share my happiness after the successful conversion.
#include <SoftwareSerial.h>
#define Pin13LED 13
byte byteSend[10];
unsigned short reg_data[4];
long temp_int64;
double final_value;
unsigned int j = 0;
byte request[] = {0x01, 0x03, 0x00, 0x75, 0x00, 0x04, 0x55, 0xD3};
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("MODBUS Test...");
}
void loop()
{
//Serial1.write(request, sizeof(request));
/*int i = 0;
for (i = 0; i < sizeof(request); i++)
{
Serial1.write(request);
//Serial1.print(request,BIN);
}*/
while (Serial1.available())
{
byteSend[j] = Serial1.read(); // Read the byte
Serial.print("DATA[");
Serial.print(j,DEC);
Serial.print("]:");
Serial.println(byteSend[j], HEX);
j++;
}
reg_data[0] = (((unsigned short)byteSend[3]) << 8) | ((unsigned short)byteSend[4]);
reg_data[1] = (((unsigned short)byteSend[5]) << 8) | ((unsigned short)byteSend[6]);
reg_data[2] = (((unsigned short)byteSend[7]) << 8) | ((unsigned short)byteSend[8]);
reg_data[3] = (((unsigned short)byteSend[9]) << 8) | ((unsigned short)byteSend[10]);
// Copy the least significant 16-bit register
temp_int64 = (long)reg_data[0];
// Copy the next 16-bit register
temp_int64 = temp_int64 | (((long)reg_data[1]) << 16);
// Copy the next 16-bit register
temp_int64 = temp_int64 | (((long)reg_data[2]) << 32);
// Copy the most significant 16-bit register
temp_int64 = temp_int64 | (((long)reg_data[3]) << 48);
// Convert the 64-bit integer to a double
final_value = *((double *)&temp_int64);
Serial.print("Double Data:");
Serial.println(final_value);
j = 0;
Serial1.write(request, sizeof(request));
delay(1000);
}
output:
i have just change the code to check the conversion method so i add the values for byte read variable at default as below
byteSend[0] =0x01;
byteSend[1] =0x03;
byteSend[2] =0x08;
byteSend[3] =0xFC;
byteSend[4] =0xF4;
byteSend[5] =0xD4;
byteSend[6] =0x82;
byteSend[7] =0x3F;
byteSend[8] =0xF7;
byteSend[9] =0x00;
byteSend[10] =0x03;
byteSend[11] =0x39;
byteSend[12] =0x25;
reg_data[0] = (((unsigned short)byteSend[3]) << 8) | ((unsigned short)byteSend[4]);
reg_data[1] = (((unsigned short)byteSend[5]) << 8) | ((unsigned short)byteSend[6]);
reg_data[2] = (((unsigned short)byteSend[7]) << 8) | ((unsigned short)byteSend[8]);
reg_data[3] = (((unsigned short)byteSend[9]) << 8) | ((unsigned short)byteSend[10]);
output
i can't understand the problem kindly help to understand what is going on here
Attachments
-
171.4 KB Views: 0
-
171.4 KB Views: 0