Writing and reading floats
Hi,
I tried to adapt the code I found in the thread "Reading floats" to write floats but it doesn't work. Here is a part of my code:
//-----
// Calling part
//-----
int addr = 0x98;
int nbWords = 2;
float floatValue = 27.11;
int ret = writeFloatValue
//-----
// writeFloatValue method
//-----
int writeFloatValue(int startingAddress, int nbWordsToWrite, float value) {
int *source = convertFloatToT
return writeValue(
}
// -------
// Converts a float into two 16bit words.
// -------
int* convertFloatToT
int IEEE; // IEEE 754 32bit float data
int LSW; // Least Significant Word 16bit of IEEE
int MSW; // Most Significant Word 16bit of IEEE
IEEE = *((int *)&real);
LSW = IEEE << 16;
LSW = LSW >> 16;
MSW = IEEE >> 16;
printf("IEEE = %d\n", IEEE);
printf("LSW = %d\n", LSW);
printf("MSW = %d\n", MSW);
int *result = (int *)malloc(2 * sizeof(int));
memset(result, 0, 2 * sizeof(int));
result[0] = MSW;
result[1] = LSW;
return result;
}
// -------
// Writes the values in the slave from the array given in argument
// -------
int writeValue(int startingAddress, int nbWordsToWrite, uint16_t *source) {
printf("Writing values: ");
int i;
for (i=0; i < nbWordsToWrite; i++) {
printf("0x%X ", source[i]);
}
printf("at address 0x%X\n", startingAddress);
// Trigger the write
triggerWriteMo
// Write
int ret = writeModbus(
if (ret != nbWordsToWrite) {
printf("\tError while writing %d words at address 0x%X (%d)\n", nbWordsToWrite, startingAddress, ret);
return -1;
}
printf("\tWrites %d words at address 0x%X\n", nbWordsToWrite, startingAddress);
waitAWhile();
return 0;
}
Here is the output I got:
Writing float value: 27.110001
at address 0x98
IEEE = 1104732488
LSW = -7864
MSW = 16856
Any help ?
Hélène
Question information
- Language:
- English Edit question
- Status:
- Answered
- For:
- libmodbus Edit question
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Can you help with this problem?
Provide an answer of your own, or ask hchagnot for more information if necessary.