| ioPAC RTU Controllers C/C++ Sample Code Programming Guide

- Main Page
- Modules
- Data Structures
- Files

Modules | Macros | Enumerations | Functions
Serial
Library

## Modules
| SerialMode
| SerialBaudRate
| SerialDataBits
| SerialParity
| SerialStopBit
| SerialFlowControl

## Macros
#define | MX_RTU_SerialRead MX_RTU_SerialBlockRead
#define | SERIAL_MAX_OQUEUE_LENGTH 4095

## Enumerations
enum | SERIAL_PORT { PORT1 = 0, PORT2, PORT3, PORT4, PORT5, PORT6, PORT7, PORT8, PORT_AMOUNT }
enum | SERIAL_ERR_CODE { SERIAL_ERR_OK = 0, SERIAL_ERR_FD, SERIAL_ERR_OPEN, SERIAL_ERR_CLOSE, SERIAL_ERR_PARAMETER, SERIAL_ERR_SLOT, SERIAL_ERR_PORT, SERIAL_ERR_READ, SERIAL_ERR_WRITE, SERIAL_ERR_IOCTL, SERIAL_ERR_MODE, SERIAL_ERR_ttyUSB, SERIAL_ERR_NOT_SUPPORT_PRODUCT, SERIAL_ERR_SYSTEM_INFO, SERIAL_ERR_HOT_PLUG, SERIAL_ERR_FLUSH, SERIAL_ERR_AMOUNT }

## Functions
SERIAL_ERR_CODE | MX_RTU_SerialOpen (UINT8 slot, SERIAL_PORT port)
SERIAL_ERR_CODE | MX_RTU_SerialWrite (UINT8 slot, SERIAL_PORT port, UINT32 len, UINT8 *str, UINT32 *write_bytes)
SERIAL_ERR_CODE | MX_RTU_SerialNonBlockRead (UINT8 slot, SERIAL_PORT port, UINT32 len, UINT8 *buf, UINT32 *read_bytes)
SERIAL_ERR_CODE | MX_RTU_SerialBlockRead (UINT8 slot, SERIAL_PORT port, UINT32 len, UINT8 *buf, UINT32 *read_bytes)
SERIAL_ERR_CODE | MX_RTU_SerialClose (UINT8 slot, SERIAL_PORT port)
SERIAL_ERR_CODE | MX_RTU_SerialFlowControl (UINT8 slot, SERIAL_PORT port, UINT8 control)
SERIAL_ERR_CODE | MX_RTU_SerialSetSpeed (UINT8 slot, SERIAL_PORT port, UINT32 speed)
SERIAL_ERR_CODE | MX_RTU_SerialSetMode (UINT8 slot, SERIAL_PORT port, UINT8 mode)
SERIAL_ERR_CODE | MX_RTU_SerialGetMode (UINT8 slot, SERIAL_PORT port, UINT8 *mode)
SERIAL_ERR_CODE | MX_RTU_SerialSetParam (UINT8 slot, SERIAL_PORT port, UINT8 parity, UINT8 databits, UINT8 stopbit)
SERIAL_ERR_CODE | MX_RTU_SerialDataInInputQueue (UINT8 slot, SERIAL_PORT port, UINT32 *bytes)
SERIAL_ERR_CODE | MX_RTU_SerialDataInOutputQueue (UINT8 slot, SERIAL_PORT port, UINT32 *bytes)
SERIAL_ERR_CODE | MX_RTU_SerialInputQueueFlush (UINT8 slot, SERIAL_PORT port)
SERIAL_ERR_CODE | MX_RTU_SerialOutputQueueFlush (UINT8 slot, SERIAL_PORT port)
SERIAL_ERR_CODE | MX_RTU_FindFD (UINT8 slot, SERIAL_PORT port, INT32 *fd)

## Detailed Description

Sample Code:
serial_echo.c

## Macro Definition Documentation

#define MX_RTU_SerialRead MX_RTU_SerialBlockRead

#define SERIAL_MAX_OQUEUE_LENGTH 4095

## Enumeration Type Documentation

enum SERIAL_PORT

Enumerator
PORT1 | 0
PORT2 | 1
PORT3 | 2
PORT4 | 3
PORT5 | 4
PORT6 | 5
PORT7 | 6
PORT8 | 7
PORT_AMOUNT | 8

enum SERIAL_ERR_CODE

Enumerator
SERIAL_ERR_OK | 0
SERIAL_ERR_FD | 1
SERIAL_ERR_OPEN | 2
SERIAL_ERR_CLOSE | 3
SERIAL_ERR_PARAMETER | 4
SERIAL_ERR_SLOT | 5
SERIAL_ERR_PORT | 6
SERIAL_ERR_READ | 7
SERIAL_ERR_WRITE | 8
SERIAL_ERR_IOCTL | 9
SERIAL_ERR_MODE | 10
SERIAL_ERR_ttyUSB | 11
SERIAL_ERR_NOT_SUPPORT_PRODUCT | 12
SERIAL_ERR_SYSTEM_INFO | 13
SERIAL_ERR_HOT_PLUG | 14
SERIAL_ERR_FLUSH | 15
SERIAL_ERR_AMOUNT | 16

## Function Documentation

SERIAL_ERR_CODE MX_RTU_SerialOpen | ( | UINT8 | slot,
|  | SERIAL_PORT | port
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialWrite | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT32 | len,
|  | UINT8 * | str,
|  | UINT32 * | write_bytes
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [in] | len | length of sent data.
  [in] | str | sent data.
  [out] | write_bytes | written bytes by serial.

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialNonBlockRead | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT32 | len,
|  | UINT8 * | buf,
|  | UINT32 * | read_bytes
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [in] | len | length of received buffer.
  [out] | buf | received buffer.
  [out] | read_bytes | received bytes by serial.

Returns
  SERIAL_ERR_CODE

Note
  A non-blocking read will (or at least should) always return immediately, but it might not return any data, if none is available at the moment.

SERIAL_ERR_CODE MX_RTU_SerialBlockRead | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT32 | len,
|  | UINT8 * | buf,
|  | UINT32 * | read_bytes
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [in] | len | length of received buffer.
  [out] | buf | received buffer.
  [out] | read_bytes | received bytes by serial.

Returns
  SERIAL_ERR_CODE

Note
  A blocking read will wait until there is data available (or a timeout, if any, expires), and then returns from the function call.

SERIAL_ERR_CODE MX_RTU_SerialClose | ( | UINT8 | slot,
|  | SERIAL_PORT | port
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialFlowControl | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT8 | control
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [in] | control | SerialFlowControl

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialSetSpeed | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT32 | speed
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [in] | speed | SerialBaudRate

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialSetMode | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT8 | mode
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [in] | mode | SerialMode

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialGetMode | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT8 * | mode
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [out] | mode | SerialMode

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialSetParam | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT8 | parity,
|  | UINT8 | databits,
|  | UINT8 | stopbit
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [in] | parity | SerialParity
  [in] | databits | SerialDataBits Support: Built-in COM ports: SERIAL_DATA_BITS_5, SERIAL_DATA_BITS_6, SERIAL_DATA_BITS_7, SERIAL_DATA_BITS_8, Expansion COM Ports: SERIAL_DATA_BITS_7, SERIAL_DATA_BITS_8,
  [in] | stopbit | SerialStopBit

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialDataInInputQueue | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT32 * | bytes
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [out] | bytes | bytes of received data in input queue.

Returns
  SERIAL_ERR_CODE

SERIAL_ERR_CODE MX_RTU_SerialDataInOutputQueue | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | UINT32 * | bytes
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [out] | bytes | bytes of sent data in output queue.

Returns
  SERIAL_ERR_CODE

Note
  Please checking (SERIAL_MAX_OQUEUE_LENGTH - bytes) > written bytes or not before calling MX_RTU_SerialWrite.

SERIAL_ERR_CODE MX_RTU_SerialInputQueueFlush | ( | UINT8 | slot,
|  | SERIAL_PORT | port
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.

Returns
  SERIAL_ERR_CODE

Note
  Flushes data received but not read.

SERIAL_ERR_CODE MX_RTU_SerialOutputQueueFlush | ( | UINT8 | slot,
|  | SERIAL_PORT | port
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.

Returns
  SERIAL_ERR_CODE

Note
  Flushes data written but not transmitted.

SERIAL_ERR_CODE MX_RTU_FindFD | ( | UINT8 | slot,
|  | SERIAL_PORT | port,
|  | INT32 * | fd
| ) |  |

Parameters

  [in] | slot | slot 0: Built-in COM Ports, slot 1 ~ 12: Expansion COM Ports.
  [in] | port | port 0 ~ 3.
  [out] | fd | file descriptor of slot and port.

Returns
  SERIAL_ERR_CODE