This documentation is out of date.

The new version of the documentation is here: https://cannylogic.com/docs

CANNY 7, UART Driver

From CANNY Wiki
Revision as of 13:52, 11 September 2016 by MEGA (Talk | contribs) (Работа контроллера в режиме RS-232)


Jump to: navigation, search

General description

Two of eleven IO channels (#9 and #10) CANNY 7 supports UART, RS-232 serial protocols data transmission/reception. It may be used to connect the controllers with each other or with external equipment supporting this communications protocols. The channels may be activated independently of each other and have individual data speed setting, protocol type and configuration, pull up line.

UART implementation in CANNY 7 controllers allows to organize serial data reception and transmission over a single wire in half-duplex mode. Thus CANNY7 may have two independent connections using the UART protocol. Monitoring of the data link status should be done from the functional diagram. If the channel is free, the device may start data transmitting , otherwise the device should wait for the line release.

RS-232 implementation by using both UART data channels , allows to exchange data with other RS-232 device in a duplex mode, i.e. one channel to perform data sending and on the other to simultaneously receive data.

Modbus protocol in CANNY7 controllers implemented as over UART as over RS-232. As ADU (Application Data Unit) used compact binary option Modbus RTU. Checking the data integrity carried out by automatically calculated checksum (CRC). Package size is limited to 16 bytes including the CRC.

Note: For correct operation of all protocols based on the UART / RS-232 requires that all GND terminals of the devices committing communication, were given to a single potential ("common ground").
Note: In UART implementation-active line is the potential GND 100mA, passive - positive potential of a given internal or external channel pull-up. In implementing RS-232 - the potentials inverse.

Driver UART/RS-232/Modbus operates using controllers' channels resources, but has a higher priority than a discrete input-output driver. Thus, upon activation of UART/RS-232/Modbus driver, for channels involved in its operation, a value changes in registers associated with discrete input-output driver, will be ignored.

Driver registers

The following describes the acceptable and returnable values of the driver control operation registers.

UART / RS-232 / Modbus Driver Configuration Registers.

Address Expected values
UART1 Mode Setup Register
UART2 Mode Setup Register
1...65535 = set controller channel configuration, which determines current operation mode and parameters (given the special constant from the constants directory);
0 = disconnect the channel from UART driver, return channel control to IO channels driver and allow status changing from the functional diagram.
UART1 Receive Timeout Setup Register, bits
UART2 Receive Timeout Setup Register, bits
1...65535 = stop data reception, if during the time for which a specified number of data bits can be recd, line was not recorded any potential change and the line is in the passive state.;
0 = use the default value defined in channel (13) configuration.

UART driver configuration is determined by the constants that represent the combination of parameters that determine the speed, mode, additional date transmission parameters and line potential in the passive mode.

Parameter Acceptable values
The data rate, bps 110; 150; 300; 600; 1200; 1800; 2400; 4800; 9600; 19200; 38400; 57600
Mode UART; RS-232
Pull-up in UART mode plus; air
The transfer direction in RS-232 mode receive; transmit
Data bits 8; 9
parity N (no) ; O (odd) ; E (even)
Number of stop bits 1; 2

Named constants that represent UART configuration parameters combination, available to the user in the "UART / RS-232 modes" CannyLab constants directory, which can be accessed via function block entry context menu having "constant" type.

UART / RS-232 / Modbus Diagnostic Registers.

Address Return values
UART1 Overflow Register
UART2 Overflow Register
1 = UART buffer is full;
0 = No overflow detected.
UART1 Receive Error Register
UART2 Receive Error Register
1 = during UART data reception an error occurred;
0 = the driver is operating normally.
UART1 Ready To Send Register
UART2 Ready To Send Register
1 = UART driver data transmit buffer is free;
0 = UART driver data transmit buffer is busy, data transmission not possible.

UART / RS-232 / Modbus Receive Registers.

Address Expected values
UART1 Data Set Ready Register
UART2 Data Set Ready Register
1 = the message is received and placed into corresponding UART channel receive buffer;
0 = in the corresponding UART driver channel receive buffer no actual data present.
UART1 RTU Received Register
UART2 RTU Received Register
1 = received a message on the appropriate channel UART is correct Modbus RTU message, the checksum is correct;
0 = received a message on the appropriate channel UART is incorrect Modbus RTU message.
UART1 Idle Register
UART2 Idle Register
1 = no activity on the corresponding channel UART drivers, the line is in a passive mode;
0 = fixed activity on the line of corresponding channel UART driver.
UART1 Received Data Length Register
UART2 Received Data Length Register
0...16 = a value, equal to the number of data bytes in the packet received via respective UART channel..
UART1 Received Data Register b1:b0
...
UART1 Received Data Register b15:b14
UART2 Received Data Register b1:b0
...
UART2 Received Data Register b15:b14
0...65535 = respective data byte values, UART reception buffers of each channel by two bytes to the register.

UART / RS-232 / Modbus Transmit Registers.

Address Expected values
UART1 Request To Send Register
UART2 Request To Send Register
1 = to load data from the transfer registers in the respective channel UART transmit buffer;
0 = do not load data to the respective channel UART transmit buffer.
UART1 RTU Transmit Register
UART2 RTU Transmit Register
1 = a command to the driver, to add to the message in the appropriate channel of UART driver's transmit buffer, the checksum in the Modbus RTU format, to form a data packet, to send in accordance with Modbus RTU protocol;
0 = transmit data from the corresponding channel driver buffer "as is".
UART1 Transmit Data Length Register
UART2 Transmit Data Length Register
0...16 = the number of data bytes that will be transmitted to the line, when receiving a command to send on the corresponding channel of UART driver.
UART1 Transmit Data Register b1:b0
...
UART1 Transmit Data Register b15:b14
UART2 Transmit Data Register b1:b0
...
UART2 Transmit Data Register b15:b14
0...65535 = values of the respective data bytes for transmission via respective UART driver channel, two bytes to the register.


Controller operation in UART mode

Operating in UART mode, the controller can perform half-duplex data transmission/reception on a single wire.

An example of a functional diagram for receiving data by UART.

6 8 3 1.png

An example of a functional diagram for transmitting data by UART.

6 8 3 2.png


Note: Particular attention should be paid to the fact that in order to avoid collisions while sending data, it is necessary to strictly control UART channel data ready to send buffer: if the buffer is not ready, then at the moment the driver is receiving data, sending data should be postponed.

Controller operation in RS-232 mode

Operating on the RS-232 protocol controller can use each of its data transfer channels only in simplex (unidirectional) mode. When using both UART channels, which operates independently of each other, it is possible to organize a full-duplex mode of data transmission via two wires: one channel - only reception, the second - the only transmission.

An example of a functional diagram working with RS-232. Receiving data through UART2, provided that the value of bytes b1: b0 is "0hAAAA", the controller sends, via UART1, 2 bytes of data containing the value «0xBBBB».

6 8 4 1.png

Реализация Modbus RTU

Драйвер UART/RS-232 включает в себя функционал автоматического формирования и проверки контрольной суммы по стандарту Modbus RTU, что упрощает включение контроллера в сеть работающую по данному протоколу. Управлять формированием контрольной суммы передаваемых сообщений и её проверкой при приёме можно через соответствующие регистры драйвера.

Примечание: При работе в режиме Modbus регистр чтения длины UARTx содержит значение с учетом принятых байт контрольной суммы (CRC), т. е. на 2 байта больше чем длина полезной нагрузки сообщения. При отправке пакета Modbus значение регистра установки длины сообщения UARTx также должно быть увеличено на 2 байта для возможности размещения и пересылки CRC.

Пример функциональной диаграммы работы контроллера в режиме MASTER-узла Modbus поверх RS-232. Выполняя диаграмму, контроллер периодически отправляет SLAVE-узлу с адресом 0х20 запрос на получение от него значения из Modbus-регистра данных с адресом 0х0001. Получение ответа на свой запрос MASTER сопровождает коротким включением своего зеленого светодиода.

6 8 5 1.png

Строка запроса, при обмене данными между устройствами, будет выглядеть так: -> 20 03 00 01 00 01 D3 7B

Адрес опрашиваемого устройства: 0x20 (байт b0 регистра приема сообщения UART2 b1:b0).

Функция: 0x03 - чтение значений из нескольких регистров хранения (байт b1 регистра приема сообщения UART2 b1:b0).

Номер первого запрашиваемого регистра: 0x0001 (байты b2 и b3 регистра приема сообщения UART2 b3:b2).

Число запрашиваемых регистров: 0x0001 (байты b4 и b5 регистра приема сообщения UART2 b5:b4).

Контрольная сумма: 0xD37B (байты b6 и b7 регистра приема сообщения UART2 b7:b6 — добавляются драйвером автоматически).


Пример функциональной диаграммы работы контроллера в качестве SLAVE-узла Modbus поверх RS-232. Получая от MASTERа запрос на передачу данных, контроллер в ответ передает состояние запрошенного регистра. Контроллер передает 2 байта данных (+2 байта CRC), т.к. регистры Modbus имеют разрядность 16 бит.

6 8 5 2.png

Строка ответа, при обмене данными между устройствами, будет выглядеть так:

<- 20 03 02 00 10 C6 A0

Адрес отвечающего устройства: 0x20 (байт b0 регистра сообщения передачи UART1 b1:b0).

Функция: 0x03 - результат чтения значений из нескольких регистров хранения (байт b1 регистра сообщения передачи UART1 b1:b0).

Число возвращаемых байт: 0x02 (байт b2 регистра сообщения передачи UART1 b3:b2).

Значение старшего байта запрашиваемого регистра: 0x00 (байт b3 регистра сообщения передачи UART1 b3:b2).

Значение младшего байта запрашиваемого регистра: 0x10 (байт b4 регистра сообщения передачи UART1 b5:b4).

Контрольная сумма: 0xC6A0 (байт b5 регистра приема сообщения UART1 b5:b4 и байт b6 регистра приема сообщения UART1 b7:b6 соответственно — добавляются драйвером автоматически).

Смотри также

CANNY 7

CANNY 7, LIN Driver