API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
rs485_receiver.c

This is an example about how to received a simple message using RS-485.

/* @file main.c
* @author TST
* @version 0.1
* @date 20/06/2012
* @brief This is an example about how to receive a message using a RS-485 bus.
*
* This example receive a message from a remote sender using a RS-485 bus.
*
* @note to use the debug UART, user must set the next configuration in the serial communication program:
* - Baud rate: 19200
* - Word length: 8 bits
* - Parity: none
* - Stop bit: 1
* - Flow control: none
*
*/
/* Platform headers */
#include "tsmart.h"
/* Ring buffer parameters for RS-485 bus */
#define TS_RS485_RX_BUFFER_SZ 28 /* Size frame = 14("Hello World!\r\n") then buffer size = 28 (the double of the frame)*/
/* Ring buffer definition */
static uint8_t TS_RS485_RxBuf[TS_RS485_RX_BUFFER_SZ];
/* @brief vRS485_receiver task
*
* This task waits a new message form a remote sender using RS-485 bus.
*
* @param pvParameters
*/
void vRS485_receiver(void *pvParameters) {
/* Variables */
uint8_t data_received[20];
uint8_t debug_buffer[50];
/* Enable reception */
/* Main loop */
for(;;) {
/* Reset buffer */
memset(data_received, 0x00, sizeof(data_received));
/* wait for new packet */
TSMART_RS485_Read(&tsmart_rs485, data_received, 14, portMAX_DELAY);
/* Debug message */
sprintf(debug_buffer, "Message received: %s", data_received);
TSMART_UART_Send(&tsmart_uart1, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
}
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
tsmart_uart_config_t tsmart_uart_config;
static uint8_t uart_buf[50];
portBASE_TYPE xReturn;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: RS485 and UART.
* To do the initialization of this bus it's necessary set several parameters
* like baud rate, flow control, parity, stop bit, word length and ring buffer.
* This values have been set to the following way:
*/
rs485_cfg.baud_rate = 9600;
rs485_cfg.flow_control = USART_HardwareFlowControl_None;
rs485_cfg.parity_bit = USART_Parity_No;
rs485_cfg.stop_bit = USART_StopBits_1;
rs485_cfg.word_length = USART_WordLength_8b;
rs485_cfg.ring_buffer_size = TS_RS485_RX_BUFFER_SZ;
rs485_cfg.ring_buffer = TS_RS485_RxBuf;
/* UART configuration. */
/* Baud rate */
tsmart_uart_config.baud_rate = 19200;
/* Flow control */
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
/* Parity bit */
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
/* Word length */
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
/* Stop bit */
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
/* Buffer size for our UART */
tsmart_uart_config.rx_buf_sz = 50;
/* Buffer for our UART */
tsmart_uart_config.rx_buf = uart_buf;
/* UART DMA */
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
/* UART time out */
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
TSMART_UART_Init(&tsmart_uart1, &tsmart_uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need one task:
*
* - RS485_receiver task: This task waits for a new message using RS-485 bus.
*/
xReturn = xTaskCreate(vRS485_receiver, "RS485_receiver", 256, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}