Modbus - RS232 Bricklet

This is the description of the Modbus protocol for the RS232 Bricklet. General information and technical specifications for the RS232 Bricklet are summarized in its hardware description.

API

A general description of the Modbus protocol structure can be found here.

Basic Functions

BrickletRS232.write
Function ID:
  • 1
Request:
  • message – Type: char[60]
  • length – Type: uint8, Range: [0 to 60]
Response:
  • written – Type: uint8, Range: [0 to 60]

Writes a string of up to 60 characters to the RS232 interface. The string can be binary data, ASCII or similar is not necessary.

The length of the string has to be given as an additional parameter.

The return value is the number of bytes that could be written.

See set_configuration for configuration possibilities regarding baudrate, parity and so on.

BrickletRS232.read
Function ID:
  • 2
Request:
  • empty payload
Response:
  • message – Type: char[60]
  • length – Type: uint8, Range: [0 to 60]

Returns the currently buffered message. The maximum length of message is 60. If the returned length is 0, no new data was available.

Instead of polling with this function, you can also use callbacks. See enable_read_callback and CALLBACK_READ callback.

BrickletRS232.set_configuration
Function ID:
  • 6
Request:
  • baudrate – Type: uint8, Range: See meanings, Default: 11
  • parity – Type: uint8, Range: See meanings, Default: 0
  • stopbits – Type: uint8, Range: See meanings, Default: 1
  • wordlength – Type: uint8, Range: See meanings, Default: 8
  • hardware_flowcontrol – Type: uint8, Range: See meanings, Default: 0
  • software_flowcontrol – Type: uint8, Range: See meanings, Default: 0
Response:
  • no response

Sets the configuration for the RS232 communication.

Hard-/Software flow control can either be on or off but not both simultaneously on.

The following meanings are defined for the elements of this function:

For baudrate:

  • 0 = 300
  • 1 = 600
  • 2 = 1200
  • 3 = 2400
  • 4 = 4800
  • 5 = 9600
  • 6 = 14400
  • 7 = 19200
  • 8 = 28800
  • 9 = 38400
  • 10 = 57600
  • 11 = 115200
  • 12 = 230400

For parity:

  • 0 = None
  • 1 = Odd
  • 2 = Even
  • 3 = Forced Parity 1
  • 4 = Forced Parity 0

For stopbits:

  • 1 = 1
  • 2 = 2

For wordlength:

  • 5 = 5
  • 6 = 6
  • 7 = 7
  • 8 = 8

For hardware_flowcontrol:

  • 0 = Off
  • 1 = On

For software_flowcontrol:

  • 0 = Off
  • 1 = On
BrickletRS232.get_configuration
Function ID:
  • 7
Request:
  • empty payload
Response:
  • baudrate – Type: uint8, Range: See meanings, Default: 11
  • parity – Type: uint8, Range: See meanings, Default: 0
  • stopbits – Type: uint8, Range: See meanings, Default: 1
  • wordlength – Type: uint8, Range: See meanings, Default: 8
  • hardware_flowcontrol – Type: uint8, Range: See meanings, Default: 0
  • software_flowcontrol – Type: uint8, Range: See meanings, Default: 0

Returns the configuration as set by set_configuration.

The following meanings are defined for the elements of this function:

For baudrate:

  • 0 = 300
  • 1 = 600
  • 2 = 1200
  • 3 = 2400
  • 4 = 4800
  • 5 = 9600
  • 6 = 14400
  • 7 = 19200
  • 8 = 28800
  • 9 = 38400
  • 10 = 57600
  • 11 = 115200
  • 12 = 230400

For parity:

  • 0 = None
  • 1 = Odd
  • 2 = Even
  • 3 = Forced Parity 1
  • 4 = Forced Parity 0

For stopbits:

  • 1 = 1
  • 2 = 2

For wordlength:

  • 5 = 5
  • 6 = 6
  • 7 = 7
  • 8 = 8

For hardware_flowcontrol:

  • 0 = Off
  • 1 = On

For software_flowcontrol:

  • 0 = Off
  • 1 = On
BrickletRS232.set_break_condition
Function ID:
  • 10
Request:
  • break_time – Type: uint16, Unit: 1 ms, Range: [0 to 216 - 1]
Response:
  • no response

Sets a break condition (the TX output is forced to a logic 0 state). The parameter sets the hold-time of the break condition.

New in version 2.0.2 (Plugin).

BrickletRS232.read_frame
Function ID:
  • 14
Request:
  • empty payload
Response:
  • message – Type: char[60]
  • length – Type: uint8, Range: [0 to 60]

Returns up to one frame of bytes from the read buffer. The frame size is configured with set_frame_readable_callback_configuration. If the returned length is 0, no new data was available.

New in version 2.0.4 (Plugin).

Advanced Functions

BrickletRS232.get_identity
Function ID:
  • 255
Request:
  • empty payload
Response:
  • uid – Type: char[8]
  • connected_uid – Type: char[8]
  • position – Type: char, Range: ['a' to 'h', 'z']
  • hardware_version – Type: uint8[3]
    • 0: major – Type: uint8, Range: [0 to 255]
    • 1: minor – Type: uint8, Range: [0 to 255]
    • 2: revision – Type: uint8, Range: [0 to 255]
  • firmware_version – Type: uint8[3]
    • 0: major – Type: uint8, Range: [0 to 255]
    • 1: minor – Type: uint8, Range: [0 to 255]
    • 2: revision – Type: uint8, Range: [0 to 255]
  • device_identifier – Type: uint16, Range: [0 to 216 - 1]

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.

The device identifier numbers can be found here

Callback Configuration Functions

BrickletRS232.enable_read_callback
Function ID:
  • 3
Request:
  • empty payload
Response:
  • no response

Enables the CALLBACK_READ callback. This will disable the CALLBACK_FRAME_READABLE callback.

By default the callback is disabled.

BrickletRS232.disable_read_callback
Function ID:
  • 4
Request:
  • empty payload
Response:
  • no response

Disables the CALLBACK_READ callback.

By default the callback is disabled.

BrickletRS232.is_read_callback_enabled
Function ID:
  • 5
Request:
  • empty payload
Response:
  • enabled – Type: bool, Default: false

Returns true if the CALLBACK_READ callback is enabled, false otherwise.

BrickletRS232.set_frame_readable_callback_configuration
Function ID:
  • 11
Request:
  • frame_size – Type: uint8, Unit: 1 B, Range: [0 to 100], Default: 0
Response:
  • no response

Configures the CALLBACK_FRAME_READABLE callback. The frame size is the number of bytes, that have to be readable to trigger the callback. A frame size of 0 disables the callback. A frame size greater than 0 enables the callback and disables the CALLBACK_READ callback.

By default the callback is disabled.

New in version 2.0.4 (Plugin).

BrickletRS232.get_frame_readable_callback_configuration
Function ID:
  • 12
Request:
  • empty payload
Response:
  • frame_size – Type: uint8, Unit: 1 B, Range: [0 to 100], Default: 0

Returns the callback configuration as set by set_frame_readable_callback_configuration.

New in version 2.0.4 (Plugin).

Callbacks

BrickletRS232.CALLBACK_READ
Function ID:
  • 8
Response:
  • message – Type: char[60]
  • length – Type: uint8, Range: [1 to 60]

This callback is called if new data is available. The message has a maximum size of 60 characters. The actual length of the message is given in addition.

To enable this callback, use enable_read_callback.

BrickletRS232.CALLBACK_ERROR
Function ID:
  • 9
Response:
  • error – Type: uint8, Range: See meanings

This callback is called if an error occurs. Possible errors are overrun, parity or framing error.

New in version 2.0.1 (Plugin).

The following meanings are defined for the elements of this function:

For error:

  • 1 = Overrun
  • 2 = Parity
  • 4 = Framing
BrickletRS232.CALLBACK_FRAME_READABLE
Function ID:
  • 13
Response:
  • frame_count – Type: uint8, Range: [0 to 255]

This callback is called if at least one frame of data is readable. The frame size is configured with set_frame_readable_callback_configuration. The frame count parameter is the number of frames that can be read. This callback is triggered only once until read or read_frame is called. This means, that if you have configured a frame size of X bytes, you can read exactly X bytes using the read_frame function, every time the callback triggers without checking the frame count response value.

New in version 2.0.4 (Plugin).