UART receives junk data

Prasad
Posts: 48
Joined: Sun Jul 23, 2017 5:26 pm

UART receives junk data

Postby Prasad » Fri Jul 28, 2017 5:14 pm

I'm trying to read UART data from my SIM900 module using slightly modified version of uart_echo example. but i'm receiving some junk data in between the useful data. Here is my code.

Code: Select all

/* Uart Events Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "driver/uart.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "soc/uart_struct.h"

/**
 * This is a example exaple which echos any data it receives on UART1 back to the sender, with hardware flow control
 * turned on. It does not use UART driver event queue.
 *
 * - port: UART1
 * - rx buffer: on
 * - tx buffer: off
 * - flow control: on
 * - event queue: off
 * - pin assignment: txd(io4), rxd(io5), rts(18), cts(19)
 */

#define ECHO_TEST_TXD  (4)
#define ECHO_TEST_RXD  (5)
#define ECHO_TEST_RTS  (18)
#define ECHO_TEST_CTS  (19)

#define BUF_SIZE (1024)

int A(const char* name);

//an example of echo test with hardware flow control on UART1
static void echo_task()
{
    const int uart_num = UART_NUM_1;
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
        .rx_flow_ctrl_thresh = 122,
    };
    //Configure UART1 parameters
    uart_param_config(uart_num, &uart_config);
    //Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
    uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    //Install UART driver (we don't need an event queue here)
    //In this example we don't even use a buffer for sending data.
    uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
    
    uint8_t* data = (uint8_t*) malloc(BUF_SIZE);

    
    while(1) {
        //Read data from UART
        int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        //Write data back to UART
        //uart_write_bytes(uart_num, (const char*) data, len);
        
        if(len > 0){
            char* value = malloc(strlen((const char*)data)+1);
            if (value) {
                strcpy(value,(const char*)data);
                printf("%s\n", value);
            }
        }
    }
}

void app_main()
{
    //A uart read/write example without event queue;
    xTaskCreate(echo_task, "uart_echo_task", 2048, NULL, 10, NULL);
}

And this is the output i'm getting,

Code: Select all

I
I
I
I
?
?
?
?

RDY
?6e??S????E

+CFUN: 1

+CPIN: READY

%?o?m$?
       *vH~??s|?•`M޶??ꂝ??f?@Rw?q?f?Z?
q?	???Oo?q???_?v?)jwl?e???-?l?3Zn#????w -???k?j??1???^

*PSSTK: "SETUP MENU",1,4,"Dialog Services",0,0,1,0,0,1
??f?@Rw?q?f?Z?
q?	???Oo?q???_?v?)jwl?e???-?l?3Zn#????w -???k?j??1???^

Call Ready
UP MENU",1,4,"Dialog Services",0,0,1,0,0,1
??f?@Rw?q?f?Z?
q?	???Oo?q???_?v?)jwl?e???-?l?3Zn#????w -???k?j??1???^
I understand the first 8 lines of characters are supposed to be like that since module is sending when power up. But i don't understand why i'm getting rest of the junk data in between. Also i can confirm that the baud rate is set to 115200 in the module and i'm not getting any junk output when i'm using Tera Term.

Who is online

Users browsing this forum: Baidu [Spider] and 97 guests