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

This is an example about how to send 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 send a message using a RS-485 bus.
*
* This example sends a message("Hello World!") using a RS-485 bus.
*
*/
/* Platform headers */
#include "tsmart.h"
/* Ring buffer parameters for RS-485 bus.
* This parameters are only for reception. This example only shows to the
* user how to send a message, but it's necessary to define them to
* initialize the 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_sender task
*
* This task send a message to a remote receiver using RS-485 bus.
*
* @param pvParameters
*/
void vRS485_sender(void *pvParameters) {
/* Enable Tx */
for(;;) {
/* Send the message "Hello World!" */
TSMART_RS485_Send(&tsmart_rs485, "Hello World!\r\n", 14);
/* Wait for a second to send again */
vTaskDelay(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 */
portBASE_TYPE xReturn;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: RS485.
* 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;
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need one task:
*
* - RS485_sender task: This task send a hello world!! message using a RS485 bus.
*/
xReturn = xTaskCreate(vRS485_sender, "RS485_sender", 128, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}