Modbus

Print debug info to stderr to help catch the bug!
debug_read80holdreg
2022-10-05, Mario de Sousa
9a9096db9e16
Parents e319814f1c17
Children
Print debug info to stderr to help catch the bug!
  • +15 -2
    mb_rtu.c
  • --- a/mb_rtu.c Mon Jun 07 11:21:26 2021 +0200
    +++ b/mb_rtu.c Wed Oct 05 19:40:55 2022 +0100
    @@ -43,7 +43,8 @@
    #define ERRMSG
    #define ERRMSG_HEAD "ModbusRTU: "
    -// #define DEBUG /* uncomment to see the data sent and received */
    +/* help in DEBUGing read80holdreg bug... */
    +#define DEBUG /* uncomment to see the data sent and received */
    #ifdef DEBUG
    #ifndef ERRMSG
    @@ -1303,6 +1304,11 @@
    #define SFF_HIST_NO_RESPONSE_FRAME 0x02
    #define SFF_HIST_NO_FRAME (SFF_HIST_NO_RESPONSE_FRAME + SFF_HIST_NO_QUERY_FRAME)
    + /* help in DEBUGing read80holdreg bug... */
    + fprintf(stderr, "mb_rtu::search_for_frame() called\n");
    + fprintf(stderr, "mb_rtu::search_for_frame() frame_data_ptr=%p frame_data_length=%d\n", frame_data, frame_data_length);
    +
    +
    if ((*search_history == SFF_HIST_NO_FRAME) ||
    (frame_data_length < MIN_FRAME_LENGTH) ||
    (frame_data_length > MAX_RTU_FRAME_LENGTH))
    @@ -1534,6 +1540,10 @@
    /* assume error... */
    *recv_data_ptr = NULL;
    + /* help in DEBUGing read80holdreg bug... */
    + fprintf(stderr, "mb_rtu::read_frame() called\n");
    + fprintf(stderr, "mb_rtu::read_frame() data_count in recv_buf=%d\n", lb_data_count(&recv_buf->data_buf));
    +
    /*===================================*
    * Delete any previously received data that has already been returned as a valid frame in *
    *===================================*/
    @@ -1554,6 +1564,7 @@
    frame_length = search_for_frame(lb_data(&recv_buf->data_buf),
    lb_data_count(&recv_buf->data_buf),
    &recv_buf->frame_search_history);
    + fprintf(stderr, "mb_rtu::search_for_frame(1) returned %d\n", frame_length);
    if (frame_length > 0)
    /* We found a valid frame! */
    return return_frame(recv_buf, frame_length, recv_data_ptr);
    @@ -1613,6 +1624,7 @@
    {/* display the hex code of each character received */
    int i;
    fprintf(stderr, "-");
    + fprintf(stderr, "-%p--", lb_free(&recv_buf->data_buf));
    for (i=0; i < read_stat; i++)
    fprintf(stderr, "<0x%2X>", *(lb_free(&recv_buf->data_buf) + i));
    }
    @@ -1626,6 +1638,7 @@
    frame_length = search_for_frame(lb_data(&recv_buf->data_buf),
    lb_data_count(&recv_buf->data_buf),
    &recv_buf->frame_search_history);
    + fprintf(stderr, "mb_rtu::search_for_frame(2) returned %d\n", frame_length);
    if (frame_length > 0)
    /* We found a valid frame! */
    return return_frame(recv_buf, frame_length, recv_data_ptr);
    @@ -1698,7 +1711,7 @@
    /* We have found a valid frame. Let's get out of here! */
    return return_frame(recv_buf, frame_length, recv_data_ptr);
    }
    - }
    + }
    /* We have detected a frame boundary, but the frame we read
    * is not valid...