MinimalModbus: A Python driver for the Modbus RTU protocol via serial port (via RS485 or RS232).
This Python file was changed (committed) at $Date: 2012-09-08 23:32:11 +0200 (Sat, 08 Sep 2012) $, which was $Revision: 166 $.
Default value for the baudrate in Baud (int).
Default value for the parity. See the pySerial module for documentation. Defaults to serial.PARITY_NONE
Default value for the bytesize (int).
Default value for the number of stopbits (int).
Default value for the timeout value in seconds (float).
Default value for port closure setting.
Instrument class for talking to instruments (slaves) via the Modbus RTU protocol (via RS485 or RS232).
The serial port object as defined by the pySerial module. Created by the constructor.
Slave address (int). Most often set by the constructor (see the class documentation).
Set this to True to print the communication details. Defaults to False.
If this is True, the serial port will be closed after each call. Defaults to CLOSE_PORT_AFTER_EACH_CALL. To change it, set the value minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True .
Read one bit from the slave.
Write one bit to the slave.
Read an integer from one 16-bit register in the slave, possibly scaling it.
The slave register can hold integer values in the range 0 to 65535 (“Unsigned INT16”).
If a value of 77.0 is stored internally in the slave register as 770, then use numberOfDecimals=1 which will divide the received data by 10 before returning the value.
Similarly numberOfDecimals=2 will divide the received data by 100 before returning the value.
Some manufacturers allow negative values for some registers. Instead of an allowed integer range 0 to 65535, a range -32768 to 32767 is allowed. This is implemented as any received value in the upper range (32768 to 65535) is interpreted as negative value (in the range -32768 to -1).
Use the parameter signed=True if reading from a register that can hold negative values. Then upper range data will be automatically converted into negative return values (two’s complement).
| signed | Data type in slave | Alternative name | Range |
|---|---|---|---|
| False | Unsigned INT16 | Unsigned short | 0 to 65535 |
| True | INT16 | Short | -32768 to 32767 |
Write an integer to one 16-bit register in the slave, possibly scaling it.
The slave register can hold integer values in the range 0 to 65535 (“Unsigned INT16”).
To store for example value=77.0, use numberOfDecimals=1 if the slave register will hold it as 770 internally. This will multiply value by 10 before sending it to the slave register.
Similarly numberOfDecimals=2 will multiply value by 100 before sending it to the slave register.
For discussion on negative values, the range and on alternative names, see read_register().
Use the parameter signed=True if writing to a register that can hold negative values. Then negative input will be automatically converted into upper range data (two’s complement).
Read a long integer (32 bits) from the slave.
Long integers (32 bits = 4 bytes) are stored in two consecutive 16-bit registers in the slave.
| signed | Data type in slave | Alternative name | Range |
|---|---|---|---|
| False | Unsigned INT32 | Unsigned long | 0 to 4294967295 |
| True | INT32 | Long | -2147483648 to 2147483647 |
Write a long integer (32 bits) to the slave.
Long integers (32 bits = 4 bytes) are stored in two consecutive 16-bit registers in the slave.
Uses Modbus function code 16.
For discussion on number of bits, number of registers, the range and on alternative names, see read_long().
Read a floating point number from the slave.
Floats are stored in two or more consecutive 16-bit registers in the slave. The encoding is according to the standard IEEE 754.
There are differences in the byte order used by different manufacturers. A floating point value of 1.0 is encoded (in single precision) as 3f800000 (hex). In this implementation the data will be sent as '\x3f\x80' and '\x00\x00' to two consecutetive registers . Make sure to test that it makes sense for your instrument. It is pretty straight-forward to change this code if some other byte order is required by anyone (see support section).
| Type of floating point number in slave | Size | Registers | Range |
|---|---|---|---|
| Single precision (binary32) | 32 bits (4 bytes) | 2 registers | 1.4E-45 to 3.4E38 |
| Double precision (binary64) | 64 bits (8 bytes) | 4 registers | 5E-324 to 1.8E308 |
Write a floating point number to the slave.
Floats are stored in two or more consecutive 16-bit registers in the slave.
Uses Modbus function code 16.
For discussion on precision, number of registers and on byte order, see read_float().
Read a string from the slave.
Each 16-bit register in the slave are interpreted as two characters (1 byte = 8 bits). For example 16 consecutive registers can hold 32 characters (32 bytes).
Write a string to the slave.
Each 16-bit register in the slave are interpreted as two characters (1 byte = 8 bits). For example 16 consecutive registers can hold 32 characters (32 bytes).
Uses Modbus function code 16.
If the textstring is longer than the 2*numberOfRegisters, an error is raised. Shorter strings are padded with spaces.
Read integers from 16-bit registers in the slave.
The slave registers can hold integer values in the range 0 to 65535 (“Unsigned INT16”).
Any scaling of the register data, or converting it to negative number (two’s complement) must be done manually.
Write integers to 16-bit registers in the slave.
The slave register can hold integer values in the range 0 to 65535 (“Unsigned INT16”).
Uses Modbus function code 16.
The number of registers that will be written is defined by the length of the values list.
Any scaling of the register data, or converting it to negative number (two’s complement) must be done manually.