Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

uart bridge reloaded

hosihosi Österreich

Hi there!

I spent some time playing with the uart bridge and while doing so, I found some behaviour, that seems odd to me:

When using the bridge with websockets it happens that the data I send to the uart, is received twice by the uart. (when sending e.g.: 123 the uart receives 123123) Did anybody experience the same?

Here's some debug output (the first line is some output I added to uart_bridge.c)

tu_dispatch_to_uart 4857103 UART2 wrote: 3
TCP 0x3ffdaa5c 10.20.30.42:52571 f 256 rb 0 sb 0
UART2 ints 1/0/1; rx en 1 bytes 0 buf 0 fifo 0, ovf 0, lcs 0; tx 6 2048 0, thr 0; hf 90012 i 0x7822 ie 0x151 cts 1

the thing is, last line says "tx 6" even thoug I sent only three bytes. Am I missing something here?

best regards, Claus

Comments

  • hosihosi Österreich

    Hi!

    I think I found a little imperfection in tu_ws_conn_handler( struct mg_connection *nc, int ev, void *ev_data, void *user_data ) (in uart_bridge.c)

    case MG_EV_WEBSOCKET_FRAME: {
      struct websocket_message *wm = (struct websocket_message *) ev_data;
      size_t len = 0;
      LOG(LL_DEBUG, ("ws frame %d", (int) wm->size));
      if (uart_no >= 0) {
        /* Note: this write is blocking if wm->size exceeds write_avail. */
        mgos_uart_write(uart_no, wm->data, wm->size); 
        s_last_activity = mg_time();
      } else {
        /* UART is disabled, throw away the frame. */
        len = wm->size;
      }
      if (len > 0) {
        LOG(LL_DEBUG, ("UART <- %d <- WS", (int) len));
      }
      if (len < wm->size) {
        /* UART buffer is full. Save the rest of the frame and throttle RX. */
        size_t tail_len = (wm->size - len);
        LOG(LL_DEBUG, ("%d bytes added to tail", (int) tail_len));
        mbuf_append(&s_tcp_rx_tail, wm->data + len, tail_len);
        nc->recv_mbuf_limit = 0;
      }
      break;
    }
    

    I think the bold line above (line 462) should look like this:

    len = mgos_uart_write(uart_no, wm->data, wm->size); 
    

    otherwise the program would have no clue if the write process succeeded. Is this correct? (Btw this solved my issues described above!)

    best regards,
    Claus

Sign In or Register to comment.