UART (A Universal Asynchronous Receiver/Transmitter) interface is supported only by DLN-4M and DLN-4S adapters.UART is a interface which is included in micro-controller, that translates data between parallel and serial forms. UART is commonly used in conjunction with communication standards such as EIA, RS-232, RS-422 or RS-485, so that it can “talk” to and exchange data with modems and other serial devices.
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; DlnOpenUsbDevice(&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 DlnCloseHandle(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.
This section describes the UART functions. They are used to control UART interface and modify its settings.Actual control of the device is performed by use of commands and responses. Each function utilizes respective commands and responses. You can send such commands directly if necessary.
The DlnUartGetPortCount()
function retrieves the total number of the UART ports available in your DLN-series adapter.
This function is defined in the dln_uart.h
file.
The DlnUartEnable()
function enables UART port.This function is defined in the dln_uart.h
file.
The DlnUartDisable()
function deactivates corresponding UART port on your DLN-Series adapter.
This function is defined in the dln_uart.h
file.
The DlnUartIsEnabled()
function retrieves information whether the specified SPI master port is activated. This function is defined in the dln_uart.h
file.
The DlnUartSetBaudrate()
function sets the baud rate, which represents the number of bits that are actually being sent.This function is defined in the dln_uart.h
file.
The DlnUartGetBaudrate()
function retrieves the baud rate value, which represents the number of bits that are actually being sent.This function is defined in the dln_uart.h
file.
The DlnUartSetCharacterLength()
function selects the data character length of 5, 6, 7, 8 and 9 bits per character. This function is defined in the dln_uart.h
file.
The DlnUartGetCharacterLength()
function retrieves the current data character length.This function is defined in the dln_uart.h
file.
The DlnUartSetParity()
function selects even, odd, mark (when the parity bit is always 1), space (the bit is always 0) or none parity.This function is defined in the dln_uart.h
file.
The DlnUartGetParity()
function retrieves the current parity bit value.This function is defined in the dln_uart.h
file.
The DlnUartSetStopbits()
function selects the number of stop bits to be sent with each character.This function is defined in the dln_uart.h
file.
The DlnUartGetStopbits()
function retrieves the current number of stop bits to be sent with each character.This function is defined in the dln_uart.h
file.
The DlnUartWrite()
function sends data via UART interface. This function is defined in the dln_uart.h
file.
The DlnUartRead()
function receives data via UART interface.This function is defined in the dln_uart.h
file.
The DlnUartEnableEvent()
function enables UART event for specified UART port.This function is defined in the dln_uart.h
file.
The DlnUartDisableEvent()
function disables UART events for specified UART port.This function is defined in the dln_uart.h
file.
The DlnUartIsEventEnabled()
function informs whether the UART port is currently configured for monitoring events.This function is defined in the dln_uart.h
file.
The DlnUartSetEventSize()
function configures the event size.This function is defined in the dln_uart.h
file.
The DlnUartGetEventSize()
function retrieves the current event size value.This function is defined in the dln_uart.h
file.
The DlnUartSetEventPeriod() function configures the event period for specified UART port.This function is defined in the dln_uart.h file.
The DlnUartGetEventPeriod() function retrieves the current event period for specified UART port.This function is defined in the dln_uart.h file.
The DlnUartGetMinBaudrate()
function retrieves the minimum possible baud rate value.This function is defined in the dln_uart.h
file.
The DlnUartGetMaxBaudrate()
function retrieves the maximum possible baud rate value.This function is defined in the dln_uart.h
file.
The DlnUartGetSupportedCharacterLengths()
function returns all supported character length types. This function is defined in the dln_uart.h
file.
The DlnUartGetSupportedParities()
function returns all supported parities values. This function is defined in the dln_uart.h
file.
The DlnUartGetSupportedStopbits()
function returns all supported stop bits values. This function is defined in the dln_uart.h
file.