ESP32/UART : Buffer is null
ESP32/UART : Buffer is null
Hello guys,
I'm testing the UART connection between ESP32 (as receiver) and a PIC microcontroller (transmitting an integer). The code below is for the ESP32 side in order to configure it for the UART reception.
[Codebox]
#include <stdio.h>
#include <esp_vfs_fat.h>
#include <esp_modbus_slave.h>
#include <esp_err.h>
#include "UART.h"
void app_main() {
int len = 0;
int data = 0;
const int uart_buffer_size = (1024*2);
const int uart_num = UART_NUM_2;
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,
.source_clk = UART_SCLK_APB,
};
// Configure UART parameters
uart_param_config(uart_num, &uart_config);
uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart_num, uart_buffer_size, 0, 0, NULL, 0);
while (1) {
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&len));
// Read data from the UART
len = uart_read_bytes(uart_num, data, uart_buffer_size, 20 / portTICK_RATE_MS);
printf("data is %d", data);
// Write data back to the UART
uart_write_bytes(uart_num, (const char *) data, len);
}
}[/Codebox]
After building and flushing, the buffer always reads null :
E (1875) uart: uart_read_bytes(1155): uart data null
E (1885) uart: uart_write_bytes(1122): buffer null
Could you please highlight where the issue is ?
Thanks in advance
I'm testing the UART connection between ESP32 (as receiver) and a PIC microcontroller (transmitting an integer). The code below is for the ESP32 side in order to configure it for the UART reception.
[Codebox]
#include <stdio.h>
#include <esp_vfs_fat.h>
#include <esp_modbus_slave.h>
#include <esp_err.h>
#include "UART.h"
void app_main() {
int len = 0;
int data = 0;
const int uart_buffer_size = (1024*2);
const int uart_num = UART_NUM_2;
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,
.source_clk = UART_SCLK_APB,
};
// Configure UART parameters
uart_param_config(uart_num, &uart_config);
uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart_num, uart_buffer_size, 0, 0, NULL, 0);
while (1) {
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&len));
// Read data from the UART
len = uart_read_bytes(uart_num, data, uart_buffer_size, 20 / portTICK_RATE_MS);
printf("data is %d", data);
// Write data back to the UART
uart_write_bytes(uart_num, (const char *) data, len);
}
}[/Codebox]
After building and flushing, the buffer always reads null :
E (1875) uart: uart_read_bytes(1155): uart data null
E (1885) uart: uart_write_bytes(1122): buffer null
Could you please highlight where the issue is ?
Thanks in advance
Re: ESP32/UART : Buffer is null
Code: Select all
int data = 0;
Re: ESP32/UART : Buffer is null
Thanks @WiFive for your reply..
So i initialized (data) as follows : int *data = (int *) malloc(uart_buffer_size);
but the output is now nothing :-
data is
data is
data is
data is
data is
data is
How is that possible ?
So i initialized (data) as follows : int *data = (int *) malloc(uart_buffer_size);
but the output is now nothing :-
data is
data is
data is
data is
data is
data is
How is that possible ?
-
- Posts: 9761
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP32/UART : Buffer is null
What does your code look like now?
Re: ESP32/UART : Buffer is null
Hi @ESP_Sprite,
It's the same code, i just replaced : (Int data = 0) By (int *data = (int *) malloc(uart_buffer_size);)
It's the same code, i just replaced : (Int data = 0) By (int *data = (int *) malloc(uart_buffer_size);)
-
- Posts: 9761
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP32/UART : Buffer is null
That doesn't make sense. That printf line should still print out "data is [a number]". The number will be bullshit as you're trying to print out the pointer to the data instead of the data itself, but it should give you a number.
Re: ESP32/UART : Buffer is null
@ESP_Sprite.. So, i let the pic transmits a letter (uint8_t) instead of an integer and the final code is as below:
But the output now is : data is ����������������
Could you please just show me how to print the letter correctly instead of this ?
Code: Select all
void app_main() {
int len;
int uart_buffer_size = (1024*2);
const int uart_num = UART_NUM_2;
uint8_t data[128];
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,
.source_clk = UART_SCLK_APB,
};
// Configure UART parameters
uart_param_config(uart_num, &uart_config);
//QueueHandle_t uart_queue;
uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart_num, uart_buffer_size, 0, 0, NULL, 0);
while (1) {
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&len));
// Read data from the UART
len = uart_read_bytes(uart_num, data, uart_buffer_size, 20 / portTICK_RATE_MS);
printf("data is %s\n", data);
fflush(stdout);
}
}
Could you please just show me how to print the letter correctly instead of this ?
-
- Posts: 9761
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP32/UART : Buffer is null
Now you're trying to print a integer array as a string... and I can't tell you how to print your data as you never told us what information you're receiving from the UART in the first place. It may help if you also post the PIC code that sends the data.
Re: ESP32/UART : Buffer is null
Code: Select all
#define _XTAL_FREQ 16000000
/*
* Includes
*/
#include <xc.h>
#include <stdint.h>
#include<stdlib.h>
/*
* Prototypes
*/
void initChip(void);
/*
* Global Variables
*/
/*
* Functions
*/
void initChip(void)
{
//CLK settings
OSCTUNE = 0x80; //3X PLL ratio mode selected
OSCCON = 0x70; //Switch to 16MHz HFINTOSC
OSCCON2 = 0x10; //Enable PLL, SOSC, PRI OSC drivers turned off
while(OSCCON2bits.PLLRDY != 1); //Wait for PLL lock
ACTCON = 0x90; //Enable active clock tuning for USB operation
ANSELA = 0x00;
PORTA = 0x00; //Initial PORTA
TRISA = 0b00000000; //Define PORTA as output
CM1CON0 = 0x00; //Turn off Comparator
PORTB = 0x00; //Initial PORTB
TRISB = 0x00; //Define PORTB as output
PORTC = 0x00; //Initial PORTC
TRISC = 0x00; //Define PORTC as output
}
//--------------------------------
// Function Declarations
void UART_TX_Init(void);
void UART_Write(uint8_t);
// Main Routine
void main(void)
{
//--[ Peripherals & IO Configurations ]--
initChip();
UART_TX_Init(); // Initialize The UART in Master Mode @ 9600bps
//---------------------------
while(1)
{
_delay(100);
UART_Write('p');
}
return;
}
//--------------------------------
// Functions Definitions
void UART_TX_Init(void)
{
TXSTA1bits.SYNC = 0;
RCSTA1bits.SPEN = 1;
TXSTA1bits.BRGH = 0; // Set For High-Speed Baud Rate
BAUDCON1bits.BRG16 = 0;
SPBRG1 = 25; // Baud rate will be set to 115.200 bps (9600bps for now)
// Set The RX-TX Pins to be in UART mode
TRISCbits.RC6 = 1; //Tx
TRISCbits.RC7 = 1; //Rx
TXSTA1bits.TXEN = 1; // Enable UART Transmission
}
void UART_Write(uint8_t data)
{
while(!TXSTA1bits.TRMT); // While TSR register is not empty
TXREG1 = data; // Load the data to the transmit buffer
}
-
- Posts: 9761
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP32/UART : Buffer is null
Okay, in that case you're *almost* doing it right. The issue is that printf expects a zero-terminated string, and your PIC isn't sending one of those, so you'll need to zero-terminate it. Solution is to do something like 'data[len]=0;' to zero-terminate the end of the string.
Note that you added a timeout of 20mS to uart_read_bytes, though. Depending on how long '_delay(100);' in your PIC code delays, you may get some lines with no response at all interspersed with P's.
Edit: Ah, and what is the line ' ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&len));' doing in your code? You're not using the result of that anyway, suggest you delete that.
Note that you added a timeout of 20mS to uart_read_bytes, though. Depending on how long '_delay(100);' in your PIC code delays, you may get some lines with no response at all interspersed with P's.
Edit: Ah, and what is the line ' ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&len));' doing in your code? You're not using the result of that anyway, suggest you delete that.
Who is online
Users browsing this forum: Google [Bot] and 101 guests