Skip to content

DTU-Modbus API

API Overview

FunctionDescription
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.

Return Value

  • True: Successful execution
  • False: 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.

Return Value

  • True: Successful execution
  • False: 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.
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

TypeParameterDescription
quint16_tportSerial port ID
quint32_tbaudrateBaud rate
QIot_MBDataBits_edataBitsData bit. See QIot_MBDataBits_e for details.
QIot_MBParity_eparityParity bit. See QIot_MBParity_e for details.
QIot_MBStopBits_estopBitsStop 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

ParameterDescription
QIOT_MBUART_DATABITS_55 bit
QIOT_MBUART_DATABITS_66 bit
QIOT_MBUART_DATABITS_77 bit
QIOT_MBUART_DATABITS_88 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

ParameterDescription
QIOT_MBUART_PARITY_NONENone
QIOT_MBUART_PARITY_EVENEven parity
QIOT_MBUART_PARITY_ODDOdd parity
QIOT_MBUART_PARITY_MARKAlways 1
QIOT_MBUART_PARITY_SPACEAlways 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

ParameterDescription
QIOT_MBUART_STOPBITS_11 bit
QIOT_MBUART_STOPBITS_1_51.5 bit
QIOT_MBUART_STOPBITS_22 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.

Return Value

  • True: Successful execution
  • False: 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.

Return Value

  • True: Successful execution
  • False: 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 execution
  • False: Failed execution

Example

c
qbool ret = Ql_iotMBDeinit();