UART: Unable to transmit data

frosticles
Posts: 3
Joined: Fri Jun 30, 2017 4:39 pm

UART: Unable to transmit data

Postby frosticles » Fri Jun 30, 2017 4:56 pm

I'm using an MTK3339 GPS module with UART1 in pins 16 (RX) & 17 (TX). I can read data coming from the GPS module with uart_read_bytes() on UART1, but I can't get uart_write_bytes() working to send commands to the module (see: https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf). I've tried sending data as a char array, and also sending the <CR><LF> as litterally "<CR><LF>" or \r\n which I believe mean the same thing. Neither have worked so far, so hopefully someone smarter than me can tell me what I'm doing wrong :D.

Code: Select all

    
    uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .rx_flow_ctrl_thresh = 122,
    };
    
    uart_param_config(UART_NUM_1, &uart_config);
    uart_set_pin(UART_NUM_1, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
    
    uint8_t* writeBuf = (uint8_t*) "$PMTK220,10000*2F\r\n";
    uart_write_bytes(UART_NUM_1, (const char*)writeBuf, 0);

    unsigned char buf[BUF_SIZE];
    while(1) {
        int size = uart_read_bytes(UART_NUM_1, buf, BUF_SIZE, 1000 / portTICK_RATE_MS);
        if (size >0) {
          printf("%s", buf);
        }
    }

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: UART: Unable to transmit data

Postby Ritesh » Sat Jul 01, 2017 10:38 am

frosticles wrote:I'm using an MTK3339 GPS module with UART1 in pins 16 (RX) & 17 (TX). I can read data coming from the GPS module with uart_read_bytes() on UART1, but I can't get uart_write_bytes() working to send commands to the module (see: https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf). I've tried sending data as a char array, and also sending the <CR><LF> as litterally "<CR><LF>" or \r\n which I believe mean the same thing. Neither have worked so far, so hopefully someone smarter than me can tell me what I'm doing wrong :D.

Code: Select all

    
    uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .rx_flow_ctrl_thresh = 122,
    };
    
    uart_param_config(UART_NUM_1, &uart_config);
    uart_set_pin(UART_NUM_1, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
    
    uint8_t* writeBuf = (uint8_t*) "$PMTK220,10000*2F\r\n";
    uart_write_bytes(UART_NUM_1, (const char*)writeBuf, 0);

    unsigned char buf[BUF_SIZE];
    while(1) {
        int size = uart_read_bytes(UART_NUM_1, buf, BUF_SIZE, 1000 / portTICK_RATE_MS);
        if (size >0) {
          printf("%s", buf);
        }
    }
Hi,

I have looked your code snippet and found issue in your uart_write_bytes call which you have called in your example.

The last argument for uart_write_bytes call is how many data you want to send over UART on which you are transmitting your data.

You have passed it 0 means nothing will be sent over UART as length of data you want send will be passed it as 0 in last argument.

So, Please pass correct bytes of data length whatever you want to send then check both write and receive data communication.

Let me know if you have any doubt or query for that.
Regards,
Ritesh Prajapati

frosticles
Posts: 3
Joined: Fri Jun 30, 2017 4:39 pm

Re: UART: Unable to transmit data

Postby frosticles » Sat Jul 01, 2017 12:36 pm

Thanks for the reply, I misread the documents so I've changed it to:

Code: Select all

  uint8_t *writeBuf = (uint8_t *)"$PMTK220,10000*2F\r\n";
  uart_write_bytes(UART_NUM_1, (const char *)writeBuf, 128);
This didn't work at first, but when I moved the uart_write_bytes() call further down the program after I'd read a few bytes, it did work. So it turns out you need a delay after uart_driver_install() before you call uart_write_bytes(). So this does work:

Code: Select all

  uart_param_config(UART_NUM_1, &uart_config);
  uart_set_pin(UART_NUM_1, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
  
  vTaskDelay(100 / portTICK_RATE_MS);
  uint8_t *writeBuf = (uint8_t *)"$PMTK220,100*2F\r\n";
  uart_write_bytes(UART_NUM_1, (const char *)writeBuf, 128);

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: UART: Unable to transmit data

Postby ESP_Sprite » Sun Jul 02, 2017 3:42 am

You're now actually sending your command, an enter, plus whatever crap is in memory after the command because you're sending 128 bytes and your writeBuf is only 19 bytes. Suggest replacing the 128 with strlen(writeBuf).

Who is online

Users browsing this forum: No registered users and 79 guests