4. Serial Port Protocol ======================= Version History ----------------- +---------+------------+---------------------------+ | Version | Date | Description | +=========+============+===========================+ | v1.0 | 2023-08-20 | Initial version | +---------+------------+---------------------------+ | v1.1 | 2023-09-15 | Added string command type | +---------+------------+---------------------------+ Protocol Overview ---------------------- This protocol defines two UART communication formats: - Basic control command (data type 0x01) - String transmission command (data type 0x02) Common Parameters ---------------------- +--------------+-----------------------+ | Parameter | Value | +==============+=======================+ | Baud rate | 1152000 bps | +--------------+-----------------------+ | Data bits | 8 bits | +--------------+-----------------------+ | Stop bits | 1 bit | +--------------+-----------------------+ | Parity | None | +--------------+-----------------------+ | Frame header | 0xAA 0xBB | +--------------+-----------------------+ | Checksum | Low 8 bits of the sum | +--------------+-----------------------+ Basic Control Command (0x01) ---------------------------- Frame Structure ^^^^^^^^^^^^^^^ :: Main controller +--------------+-----------+----------+---------+------------+----------+ | Frame header | Data type | Main cmd | Sub-cmd | Data field | Checksum | +--------------+-----------+----------+---------+------------+----------+ | 0xAABB | 0x01 | 1 byte | 1 byte | 8 bytes | 1 byte | +--------------+-----------+----------+---------+------------+----------+ AI Camera response +--------------+-----------+----------+---------+------------+----------+ | Frame header | Data type | Main cmd | Sub-cmd | Data field | Checksum | +--------------+-----------+----------+---------+------------+----------+ | 0xBBAA | 0x01 | 1 byte | 1 byte | 8 bytes | 1 byte | +--------------+-----------+----------+---------+------------+----------+ Function Description -------------------- .. py:function:: AI_Uart_CMD(uart, cmd=0, cmd_type=0, cmd_data=[0]*8) Send a basic control command Parameters: - cmd: main command byte (see 5.1 command list) - cmd_type: sub-command type - cmd_data: must be an 8-byte list String Command (0x02) --------------------- Frame Structure ^^^^^^^^^^^^^^^ :: +--------------+-----------+----------+---------+------------+---------------+----------------+----------+ | Frame header | Data type | Main cmd | Sub-cmd | Extra data | String length | String content | Checksum | +--------------+-----------+----------+---------+------------+---------------+----------------+----------+ | 0xAABB | 0x02 | 1 byte | 1 byte | 3 bytes | 1 byte | N bytes | 1 byte | +--------------+-----------+----------+---------+------------+---------------+----------------+----------+ Function Description -------------------- .. py:function:: AI_Uart_CMD_String(uart, cmd=0xFE, cmd_type=0xFE, cmd_data=[0]*3, str_buf='') Send a string command Parameters: - cmd_data: must be a 3-byte list - str_buf: UTF-8 encoded string (length calculated automatically) Command Reference ----------------- Basic Command List ------------------ +----------+-----------+-------------------------+-----------------------------------------------------+ | Main cmd | Sub-cmd | Command name | Data field format | +==========+===========+=========================+=====================================================+ | 0x01 | 0xFF | Camera initialization | None | +----------+-----------+-------------------------+-----------------------------------------------------+ | 0x02 | 0x01 | Set parameter - integer | [Param ID(1B), Value(4B), Reserved(3B)] | +----------+-----------+-------------------------+-----------------------------------------------------+ | 0x02 | 0x02 | Set parameter - float | [Param ID(1B), Value(4B), Reserved(3B)] | +----------+-----------+-------------------------+-----------------------------------------------------+ | 0x03 | 0x00-0xFF | Function control | See function control sub-command table (Appendix A) | +----------+-----------+-------------------------+-----------------------------------------------------+ String Command List ------------------- +----------+---------+----------------------+---------------------------+ | Main cmd | Sub-cmd | Command name | String format description | +==========+=========+======================+===========================+ | 0xFE | 0x00 | Plain text message | UTF-8 encoded text | +----------+---------+----------------------+---------------------------+ | 0xFE | 0x01 | Debug info | "[DEBUG]" + text content | +----------+---------+----------------------+---------------------------+ | 0xFD | 0x00 | JSON control command | Standard JSON format | +----------+---------+----------------------+---------------------------+ Serial Port Commands -------------------- .. figure:: /_static/products/aicamera-k230d/_static/image/uart_comm.png :align: center :width: 1080 AI Camera Response Data Format ------------------------------ Code Examples ------------- Basic Command Example ^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python # Query device status AI_Uart_CMD(uart1, cmd=0x01) # Set parameter AI_Uart_CMD(uart1, cmd=0x02, cmd_type=0x01, cmd_data=[0x01,0x00,0xFF]) String Command Example ^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python # Send a normal message AI_Uart_CMD_String(uart1, str_buf="Hello World") # Send a JSON control command AI_Uart_CMD_String(uart1, cmd=0xFD, cmd_data=[0x01,0x02,0x03], str_buf='{"led":1,"beep":0}') Checksum Calculation -------------------- Checksum = sum(all data bytes) & 0xFF Algorithm implementation: .. code-block:: python def calc_checksum(data): return sum(data) & 0xFF Error Handling ^^^^^^^^^^^^^^ - A frame with a checksum error should be discarded - An erroneous command received should return the 0xEE error code AI Camera Response Frame Format ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: +--------------+-----------+-------------+------------+---------------+----------+ | Frame header | Data type | Status code | Extra data | Response data | Checksum | +--------------+-----------+-------------+------------+---------------+----------+