Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

new UART APIs doesn't work.

aiferaifer China
edited April 4 in Mongoose OS

Just a minute ago, I use the new UART APIs, but they doesn't work.

 // init config parameters
  int uart_no = 1;
  struct mgos_uart_config cfg;
  mgos_uart_config_set_defaults(uart_no, &cfg);
  ESP_LOGI(TAG, " jswrap_uart_init, default baud: %d", cfg.baud_rate);  // => 115200
  ESP_LOGI(TAG, " jswrap_uart_init, rx_buf_size: %d", cfg.rx_buf_size); // => 256


  // init uart
  if(mgos_uart_configure(uart_no, &cfg)){
    mgos_uart_set_dispatcher(uart_no, uart_handler, NULL);
    mgos_uart_flush(uart_no);
    mgos_uart_set_rx_enabled(uart_no, true); 
    size_t written = mgos_uart_write(uart_no, "hello", 5);
    ESP_LOGI(TAG, " jswrap_uart_init, init: %s", "SUCCESS"); // => jswrap_uart_init, init: SUCCESS
    ESP_LOGI(TAG, " jswrap_uart_init, written: %d", written); // => jswrap_uart_init, written: 5
  }else{
    ESP_LOGI(TAG, " jswrap_uart_init, init: %s", "FAIL");
  }

The output says that uart 1 is initialized and "hello" is written, but in fact , nothing is sent on uart 1 port(TX:GPIO19, RX:GPIO18)。

And in uart_handler(),

static void uart_handler(int uart_no, void *arg) {
  ESP_LOGI(TAG, "uart_handler uart_no: %d", uart_no); // => uart_handler uart_no: 1

  int rx_avail = mgos_uart_read_avail(uart_no);
  uint8_t* buf_cb = (uint8_t*) malloc(rx_avail+1);
  size_t rx_len = mgos_uart_read(uart_no, buf_cb, rx_avail);
  buf_cb[rx_len] = '\0';
  ESP_LOGI(TAG, "uart_handler rx_len: %d", rx_len); // => uart_handler rx_len: 0
  if( rx_len > 0 ){
    ESP_LOGI(TAG, "uart_handler rx_data: %s", buf_cb);
  }
  free(buf_cb);
  mgos_uart_write(uart_no, "ahalo", 5); // => in fact , nothing is sent
  (void)arg;
}

rx_len is always zero, even I have sent somethien to uart 1 port.

What's wrong?
( => means output on screen)
(all the codes above compiled with no errors)
(ESP32, mongoose os)

Comments

Sign In or Register to comment.