Search code examples
cembeddedmodbus

Partial output printing for libmodbus functions output


I'm writing application using libmodbus 3.1.2. I'm unable to print complete output of modbus functions. Also I'm getting some garbage values in output. Please suggest.

 printf("================Sending  Sample delay command==============\n");
  i32_read_ret = modbus_read_registers(ctx, 164, 1, sample_delay);
  printf("\nSample delay result:%d\n", i32_read_ret);
  printf("\n Sample delay : \n");
  for (i = 0; i < 40; i++) //printing only the received characters
  {
    printf(" : %.2x \t", (int)( * (unsigned char * )( & sample_delay[i])));

  }
  printf("\n\n\n");

  tcflush(fd, TCIOFLUSH);
  printf("================Sending  measurement command==============\n");

  i32_read_ret = modbus_write_registers(ctx, 1, 5, measurement);
  printf("\nMeasurement result : %d \n", i32_read_ret);
  for (i = 0; i < 40; i++) {
    printf(": %.2x \t", (int)( * (unsigned char * )( & measurement[i])));

  }
  printf("\n");
  tcflush(fd, TCIOFLUSH);
  printf("================Sending  actual measurement command==============\n");
  i32_read_ret = modbus_read_registers(ctx, 83, 10, actual_measurement);
  printf("\nRead Return actual_measurement 3:%d\n\n\n", i32_read_ret);

  for (i = 0; i < 40; i++) //printing only the received characters
  {
    printf(" : %.2x \t", (int)( * (unsigned char * )( & actual_measurement[i])));

  }
  printf("\n");
  printf("%s\n", actual_measurement);
  tcflush(fd, TCIOFLUSH);

o/p

================Sending  Sample delay command==============
[28][03][00][A4][00][01][C2][10]
Waiting for a confirmation...
<28><03><02><01><2C><E5><CF>

Sample delay result:1

 Sample delay : 
 : 2c    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 08    : 08    : 80    : 00    : 38    : 00    : 29    : 00    : 00    : 00    : c8    : 7d    : 01    : 00    : 00    : 00    : 30    : 00    : 00    : 00   


================Sending  measurement command==============
[28][10][00][01][00][05][0A][B1][D8][77][1E][B1][D8][77][1E][0D][F8][9F][E7]
Waiting for a confirmation...
<28><10><00><01><00><05><56><33>

Measurement result : 5 
: d8    : 1e    : d8    : 1e    : f8    : 00    : 65    : 57    : 70    : 00    : 10    : 02    : 65    : 57    : 7c    : 00    : 00 : 04   : 01    : 00    : 49    : 01    : 31    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00 : 00   : 00    : 00    : 00    : 00    : 00    
================Sending  actual measurement command==============
[28][03][00][53][00][0A][32][25]
Waiting for a confirmation...
<28><03><14><41><DD><8E><59><00><00><00><00><00><00><00><00><46><1C><34><00><46><1C><34><00><48><84>

Read Return actual_measurement 3:10


 : dd    : 59    : 00    : 00    : 00    : 00    : 1c    : 00    : 1c    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 08    : 1b    : 31    : 00    : 28    : 00    : 08    : 00    : 01    : 00    : 00    : 00    : 00    : 00    : 20    : 07    : 00    : 00    : 20    : 07   
�AY�

Solution

  • After correcting line of printf issue got resolved.

    for(i=0;i<i32_read_ret;i++)
    {
       printf("%X",sample_delay[i]);
    }