c - libmodbus 函数输出的部分输出打印

标签 c embedded modbus

我正在使用libmodbus 3.1.2编写应用程序。 我无法打印 modbus 函数的完整输出。另外,我在输出中得到了一些垃圾值。请建议。

 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�

最佳答案

更正 printf 行后问题得到解决。

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

关于c - libmodbus 函数输出的部分输出打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54511859/

相关文章:

python - Python 的性能是否物有所值?

c - 是否可以在运行时在 M0+ 上设置 ISR 处理程序

modbus - 如何使用 Modbus ping 仪表

c# - 与多个从站通信(基于 Modbus 协议(protocol))

visual-studio-2010 - MFC 中的 libmodbus

c - 使用堆栈进行迭代合并排序

c - 我不明白大小如何等于 float 而不是短整型,因为 i 的大小是短整型

c - 丢弃 C 中的字符?

c - 使用各种格式说明符打印 short int

c - 为什么这个条件总是成立?