You are here

Simple UART Module Example

This example shows how to setup and send data with UART module. You can find the complete example in the “..\Program Files\Diolan\DLN\examples\c_cpp\examples\simple” folder after DLN setup package installation.

#include "..\..\..\common\dln_generic.h"
#include "..\..\..\common\dln_uart.h"
#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

int _tmain(int argc, _TCHAR* argv[])
	// Open device
	HDLN device;

	// Configure UART to 19200 8N1
	uint32_t baud;
	DlnUartSetBaudrate(device, 0, 19200, &baud);
	DlnUartSetCharacterLength(device, 0, DLN_UART_CHARACTER_LENGTH_8);
	DlnUartSetParity(device, 0, DLN_UART_PARITY_NONE);
	DlnUartSetStopbits(device, 0, DLN_UART_STOPBITS_1);
	// Enable UART
	uint16_t conflict;
	DlnUartEnable(device, 0, &conflict);

	// Write data
	uint8_t output[10] = "123456789";
	DlnUartWrite(device, 0, 10, output);
	// Read data
	uint8_t input[10];
	uint16_t total;
	DlnUartRead(device, 0, 10, &input, &total);
	// Print it
	for (int i = 0; i < total; i++) putchar(input[i]);

	// Disable UART
	DlnUartDisable(device, 0);

	// Close device
	return 0;

  • Line 1:#include "..\..\..\common\dln_generic.h"

    The dln_generic..h header file declares functions and data structures for the generic interface. In current example this header is used to call DlnOpenUsbDevice() and DlnCloseHandle() functions.

  • Line 2:#include "..\..\..\common\dln_uart.h"

    The dln_uart.h header file declares functions and data structure specific for UART interface. By including this header file you are able to call DlnUartSetBaudrate(), DlnUartSetCharacterLength(), DlnUartSetParity(), DlnUartSetStopbits(), DlnUartEnable(), DlnUartWrite(), DlnUartRead(), DlnUartDisable() and other UART interface functions.

  • Line 3:#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

    Use dln.lib library while project linking.

  • Line 9:DlnOpenUsbDevice(&device);

    The function establishes the connection with the DLN adapter. This application uses the USB connectivity of the adapter. For additional options, refer to the Device Opening & Identification section.

  • Line 13:DlnUartSetBaudrate(device, 0, 19200, &baud);

    The function sets the baud rate value equal to 10200 bits.

  • Line 14:DlnUartSetCharacterLength(device, 0, DLN_UART_CHARACTER_LENGTH_8);

    The function sets the character length equal to 8 bits.

  • Line 15:DlnUartSetParity(device, 0, DLN_UART_PARITY_NONE);

    The function sets parity to none.

  • Line 16:DlnUartSetStopbits(device, 0, DLN_UART_STOPBITS_1);

    The function sets the number of stop bits to be sent with each character.

  • line 19:DlnUartEnable(device, 0, &conflict);

    The function enables the UART interface.

  • Line 22:uint8_t output[10] = "123456789";

    Define the array of 10 8-bits unsigned integer values (10 bytes) and set it to “123456789”.

  • Line 23:DlnUartWrite(device, 0, 10, output);

    The function sends 10 bytes data buffer via UART interface.

  • Line 28:DlnUartRead(device, 0, 10, &input, &total);

    The function reads 10 bytes data buffer to input variable and returns the value of real read bytes to total variable.

  • Line 30: for (int i = 0; i < total; i++) putchar(input[i]);

    Output input array values in loop.

  • Line 33:DlnUartDisable(device, 0);

    Disable UART interface port 0.

  • Line 36:DlnCloseHandle(device);

    The application closes handle to the DLN adapter.

Average: 1 (1 vote)

User login