Hello,
I am working on a project where I need to read the Scalr, Unit, Value, and Logical name using C programming language. I've included my code below for reference. The issue I'm facing is that when I try to print the Scalar or Unit, they are not appearing as expected. I aim to convert them to strings and print them correctly. Any assistance on this matter would be greatly appreciated.
Thank you.
unsigned char logical_name[6];
char data_string[50] = "1.0.14.7.0.255,1.0.32.7.0.255";
char *data = NULL;
data_string[sizeof(data_string) - 1] = '\0';
char *token = strtok(data_string, ",");
int numberOfProcessedObisCode = 0;
while (token != NULL)
{
gxRegister register_object;
hlp_setLogicalName(logical_name, token);
cosem_init2(®ister_object.base, DLMS_OBJECT_TYPE_REGISTER, logical_name);
memcpy(obisCodeInstantaneousData[numberOfProcessedObisCode].logical_name, ®ister_object.base.logicalName, sizeof(®ister_object.base.logicalName));
int read_status = com_read(connection, ®ister_object.base, 3);
ret = obj_toString(®ister_object.base, &data);
if (ret != DLMS_ERROR_CODE_OK)
{
return ret;
}
if (data != NULL)
{
sscanf(data, "Value: Scaler: %lf Unit: %s",
&obisCodeInstantaneousData[numberOfProcessedObisCode].scalar,
obisCodeInstantaneousData[numberOfProcessedObisCode].unit);
free(data);
data = NULL;
}
read_status = com_read(connection, ®ister_object.base, 2);
obisCodeInstantaneousData[numberOfProcessedObisCode].value = var_toInteger(®ister_object.value);
numberOfProcessedObisCode++;
token = strtok(NULL, ",");
}
// Print the structured data from the array of measurements
for (int i = 0; i < numberOfProcessedObisCode; i++) {
printf("Value: %d\n", obisCodeInstantaneousData[i].value);
printf("Unit: %s\n", obisCodeInstantaneousData[i].unit);
printf("Scaler: %f\n", obisCodeInstantaneousData[i].scalar);
printf("Logical Name: ");
for (int j = 0; j < sizeof(token); j++) {
printf("%02X", obisCodeInstantaneousData[i].logical_name[j]);
}
printf("\n\n");
}
Hi, You can use obj_toString…
Hi,
You can use obj_toString method. Something like this:
https://github.com/Gurux/GuruxDLMS.c/blob/e58316be969d0e8fc06d38d6ec6ca…
BR,
Mikko