Appearance
DTU-Modbus API
API Overview
Function | Description |
---|---|
Ql_iotMBInit() | Initializes Modbus components. |
Ql_iotMBCloudRecv() | Converts the format of the TSL data issued by Developer Center to Modbus format and sends the data to Modbus sub-device. |
Ql_iotMBLocalRecv() | Forwards Modbus data received by the serial port to Modbus components and processes the Modbus data. |
Ql_iotMBDeinit() | De-initializes Modbus components. |
API Description
Ql_iotMBInit
This function initializes ModBus components. This function also configures the serial port list used to download configuration files (including device information, QuecThing configuration and serial port configuration; The file is generated by the tool provided by Acceleronix. Contact Acceleronix Technical Support for details), registers callback functions used to send data to the serial port, and initializes Modbus.
Prototype
c
qbool Ql_iotMBInit(quint16_t uartList[],quint32_t uartNum,QIot_MBSend_f sendFunc,QIot_MBInitCb_f initCb);
Parameter
- Input Parameter
- quint16_t
uartList
: List of serial ports used to download configuration files. You should open the corresponding serial ports. - quint32_t
uartNum
: Number of serial ports used to burn configuration files. You can set it to 0 if the feature of serial port downloading files is not required. - QIot_MBSend_f
sendFunc
: Callback function for sending data to the serial port. See QIot_MBSend_f for details. - QIot_MBInitCb_f
initCb
: Callback function of initializing Modbus. See QIot_MBInitCb_f for details.
- quint16_t
Return Value
True
: Successful executionFalse
: Failed execution
Example
c
quint16_t portList[] = {0,1,2,3,4,5,6,7,8,9};
#define QIOT_MODBUS_PORT_MAXNUM 100
static pointer_t portFd[QIOT_MODBUS_PORT_MAXNUM] = {[0 ...(QIOT_MODBUS_PORT_MAXNUM-1)] = SOCKET_FD_INVALID};
quint32_t i;
for (i = 0; i < sizeof(portList)/sizeof(quint16_t); i++)
{
portFd[i] = modbusUartInit(portList[i],9600,QIOT_MBUART_DATABITS_8,QIOT_MBUART_PARITY_NONE,QIOT_MBUART_STOPBITS_1);
}
/* Initialize modbus */
Ql_iotMBInit(portList,sizeof(portList)/sizeof(quint16_t),modbusUartSend,modbusInitCb);
QIot_MBSend_f
This callback function is called when Modbus components need to send data to the serial port. The serial port sending ability should be implemented in this callback function.
Prototype
c
typedef qbool (*QIot_MBSend_f)(quint16_t port, const quint8_t *buf, quint32_t bufLen);
Parameter
- Input Parameter
- quint16_t
port
: Serial port ID. - const quint8_t *
buf
: Data to be sent. - quint32_t
bufLen
: Length of data to be sent.
- quint16_t
Return Value
True
: Successful executionFalse
: Failed execution
QIot_MBInitCb_f
After Modbus components are initialized, this callback function is called to receive the device information from the configuration files. This function handles the necessary operations for serial port communication, including sending and receiving data, as well as establishing a connection between the device and Developer Center.
Prototype
c
typedef void (*QIot_MBInitCb_f)(char *pk, char *ps,QIot_MBUartCfg_t *uartInfo[], quint32_t uartNum);
Parameter
- Input Parameter
- char *
pk
: ProductKey generated when you create the product in Developer Center. - char *
ps
: ProductSecret generated when you create the product in Developer Center. - QIot_MBUartCfg_t *
uartInfo
: Serial port configuration in the configuration files. See Ql_iotMBCloudRecv() for details. - quint32_t
uartNum
: Number of serial ports in the configuration files.
- char *
QIot_MBUartCfg_t
The structure of serial port configurations:
c
typedef struct
{
quint16_t port;
quint32_t baudrate;
QIot_MBDataBits_e dataBits;
QIot_MBParity_e parity;
QIot_MBStopBits_e stopBits;
}QIot_MBUartCfg_t;
Parameter
Type | Parameter | Description |
---|---|---|
quint16_t | port | Serial port ID |
quint32_t | baudrate | Baud rate |
QIot_MBDataBits_e | dataBits | Data bit. See QIot_MBDataBits_e for details. |
QIot_MBParity_e | parity | Parity bit. See QIot_MBParity_e for details. |
QIot_MBStopBits_e | stopBits | Stop bit. See QIot_MBStopBits_e for details. |
QIot_MBDataBits_e
The enumeration of data bits:
c
typedef enum
{
QIOT_MBUART_DATABITS_5 = 0,
QIOT_MBUART_DATABITS_6 = 1,
QIOT_MBUART_DATABITS_7 = 2,
QIOT_MBUART_DATABITS_8 = 3,
}QIot_MBDataBits_e;
Parameter
Parameter | Description |
---|---|
QIOT_MBUART_DATABITS_5 | 5 bit |
QIOT_MBUART_DATABITS_6 | 6 bit |
QIOT_MBUART_DATABITS_7 | 7 bit |
QIOT_MBUART_DATABITS_8 | 8 bit |
QIot_MBParity_e
The enumeration of parity bits:
c
typedef enum
{
QIOT_MBUART_PARITY_NONE = 0,
QIOT_MBUART_PARITY_EVEN = 1,
QIOT_MBUART_PARITY_ODD = 2,
QIOT_MBUART_PARITY_MARK = 3,
QIOT_MBUART_PARITY_SPACE = 4,
}QIot_MBParity_e;
Parameter
Parameter | Description |
---|---|
QIOT_MBUART_PARITY_NONE | None |
QIOT_MBUART_PARITY_EVEN | Even parity |
QIOT_MBUART_PARITY_ODD | Odd parity |
QIOT_MBUART_PARITY_MARK | Always 1 |
QIOT_MBUART_PARITY_SPACE | Always 0 |
QIot_MBStopBits_e
The enumeration of stops bits:
c
typedef enum
{
QIOT_MBUART_STOPBITS_1 = 0,
QIOT_MBUART_STOPBITS_1_5 = 1,
QIOT_MBUART_STOPBITS_2 = 2,
}QIot_MBStopBits_e;
Parameter
Parameter | Description |
---|---|
QIOT_MBUART_STOPBITS_1 | 1 bit |
QIOT_MBUART_STOPBITS_1_5 | 1.5 bit |
QIOT_MBUART_STOPBITS_2 | 2 bit |
Ql_iotMBCloudRecv
This function converts the format of the TSL data issued by Developer Center to Modbus format and sends the data to Modbus sub-device.
Prototype
c
qbool Ql_iotMBCloudRecv(const void *ttlvHead);
Parameter
- Input Parameter
- const void *
ttlvHead
: TTLV data table.
- const void *
Return Value
True
: Successful executionFalse
: Failed execution
Example
c
void *ttlvHead = NULL;
qbool ret = Ql_iotMBCloudRecv(ttlvHead);
Ql_iotTtlvFree(&ttlvHead);
Ql_iotMBLocalRecv
This function forwards Modbus data received by the serial port to Modbus components and processes the Modbus data.
Prototype
c
qbool Ql_iotMBLocalRecv(quint16_t port,quint8_t *data,quint32_t len);
Parameter
- Input Parameter
- quint16_t
port
: Serial port ID. - quint8_t *
data
: Modbus data received by the serial port. - quint32_t
len
: Length of Modbus data received by the serial port.
- quint16_t
Return Value
True
: Successful executionFalse
: Failed execution
Example
c
int uartId;
quint8_t buf[1024];
qint32_t bufLen = read(sockFd, buf, sizeof(buf));
Ql_iotMBLocalRecv(uartId, buf, bufLen);
Ql_iotMBDeinit
This function de-initializes Modbus components.
Prototype
c
qbool Ql_iotMBDeinit(void);
Parameter
None
Return Value
True
: Successful executionFalse
: Failed execution
Example
c
qbool ret = Ql_iotMBDeinit();