Search code examples
pythonraspberry-pimodbusrs485minimalmodbus

How to read register of digital counter with minimalmodbus


I'm trying to read the values of an industry digital counter with Modbus RTU RS-485. Using USB-RS-485 conversion, and here is the master send code is taken from the following datasheet,

Datasheet Link

Snippets from Autonics CT series datasheet

I am expecting that the read input register is what I'm expecting, and the API of the minimalmodbus expects to specify register number, a number of decimals, and function code.

  • Does the library auto-assign the slave number, or we have to define it?
  • From the datasheet, is it the register number is the address?
  • And how many decimals do I expect if there's two data sequence as a response?
  • Is the CRC16 check already included within the library as i shouldn't code it?

Here's my code by far, modifying examples.

import minimalmodbus
import time

# port name, slave address (in decimal)
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1)

instrument.serial.baudrate = 9600      
instrument.serial.bytesize = 8
instrument.serial.stopbits = 1
instrument.serial.timeout  = 1          
instrument.mode = minimalmodbus.MODE_RTU  
instrument.clear_buffers_before_each_transaction = True
instrument.debug = True

while True:
    # Register number, number of decimals, function code
    # not sure what to expect on number of register, is it 31004, 31005?
    
    digit_count = instrument.read_register(1, 2, 4)
    print(digit_count)
    time.sleep(1) 

I have read other libraries for python Modbus, I would be happy to get any better coding recommendation related to Modbus. Thank you in prior.


Solution

  • Does the library auto-assign the slave number, or we have to define it?

    With MinimalModbus you pass the slave ID through in the minimalmodbus.Instrument('/dev/ttyUSB0', 1) call (the 1 is the Slave ID). How you set the slave ID on the device itself varies (this is not covered by the Modbus over serial line spec; could be a configuration program, DIP switches, based on the serial number etc. Other libraries may take different approaches (for example defaulting to Slave ID 1).

    From the datasheet, is it the register number is the address?

    The header in the spec tables says "No(Address)" so "10001(0000)" means register 1, address 0 (these refer to the same thing; I recommend reading the "Modbus: When 40001 Really Means 1, or 0 Really Means 1" section in this article which explains some of the issues around addressing).

    And how many decimals do I expect if there's two data sequence as a response?

    Not quite sure what you mean by "two data sequence". The Modbus spec only details the sending of bits (coils) and 16 bit values (input/holding registers). From a quick look at your spec it looks like this device just uses a single registers; for instance "OUT1 Output time" has "unit: ×10㎳" so take whatever is in the register and divide by 10 to get ms.

    Is the CRC16 check already included within the library as i shouldn't code it?

    Any decent Modbus library will look after the protocol details (such as CRC) for you (so no you don't need to code this; MinimalModbus will calculate it for you)