1

I am reading and writing registers from a device with the following parameters baudrate - 9600 stopbit-1 no_of_bits - 8 parity - odd

i am currently using the minimal modbus library. my code to read register is pasted below

    # serial line
    import traceback
    import minimalmodbus as mmRtu

    regsSp = 6
    portName = 'com4'
    baudrate = 9600

    timeoutSp = 0.5 + regsSp * 0
    mmc = mmRtu.Instrument(portName,5,mode=mmRtu.MODE_RTU)
    mmc.serial.baudrate = baudrate
    mmc.serial.timeout = timeoutSp
    mmc.serial.parity = 'O'

    tb = None
    errCnt = 0
    mmc.address = 5
    try:
        data = mmc.read_registers(3, 6)
    except:
        tb = traceback.format_exc()
        errCnt += 1
    mmc.serial.close()
    print(errCnt)
    print(data)

[ referred to the thread Python modbus library ]

The read register is working perfectly.

However my write_register is failing here. I am reading from the 5th register of my device. The code is below.

    # serial line
    import traceback
    import minimalmodbus as mmRtu

    regsSp = 6
    portName = 'com4'
    baudrate = 9600

    timeoutSp = 0.5 + regsSp * 0
    mmc = mmRtu.Instrument(portName,5,mode=mmRtu.MODE_RTU)
    mmc.serial.baudrate = baudrate
    mmc.serial.timeout = timeoutSp
    mmc.serial.parity = 'O'

     tb = None
    errCnt = 0
    mmc.address = 5
    try:
        data = mmc.read_registers(3, 6)
    except:
        tb = traceback.format_exc()
        errCnt += 1
    mmc.serial.close()
    print(errCnt)
    print(data)

Traceback (most recent call last): File "C:/Users/Admin/PycharmProjects/weight_calibrate/weight_calibrate.py", line 17, in mmc.write_register(4, 5, 0, 6, False) File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\minimalmodbus-0.7-py3.6.egg\minimalmodbus.py", line 296, in write_register self._genericCommand(functioncode, registeraddress, value, numberOfDecimals, signed=signed) File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\minimalmodbus-0.7-py3.6.egg\minimalmodbus.py", line 697, in _genericCommand payloadFromSlave = self._performCommand(functioncode, payloadToSlave) File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\minimalmodbus-0.7-py3.6.egg\minimalmodbus.py", line 795, in _performCommand response = self._communicate(request, number_of_bytes_to_read) File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\minimalmodbus-0.7-py3.6.egg\minimalmodbus.py", line 930, in _communicate raise IOError('No communication with the instrument (no answer)') OSError: No communication with the instrument (no answer)

I have checked that the data packet in the request is perfect, even the CRC. Yet the response from the device is null and that is where the code fails.

minimalmodbus.py

    if len(answer) == 0:
        raise IOError('No communication with the instrument (no answer)')

since answer is b''

I am able to write to the registers of my device using the tools simply modbus master 8.0.7 and the request data packet has the same contents in both the cases. This suggests I have the permissions to write to the port. Also since even a read actually writes the request to the port, file permissions might not be the problem is what i assume.

Please suggest if i am missing any setting here..

I also tried the library modbus_tk and it fails in that case too.

Nags
  • 11
  • 3

2 Answers2

0

I've never used this library, but it appears you are calling the write_register function in the following manner:

write_register(4, 5, 0, 6, False)

The documentation suggests that the first number is the address, and that all subsequent numbers are values to the written starting at the specified address. If you haven't done so already, drop the 'False' and see if your write goes through.

0

I'm sure you have probably worked out the solution now? But you need to call write_register and use functioncode 6 to send the new value to the register.

mmc.write_register(REGISTER, NEW_VALUE, DECIMALS, functioncode=6)
mmc.write_register(5, 18, 1, 6) <- this would set the value of register 5 to 18.

Likewise if you want to send a negative valued number add signed=True to the end of the write command.

mmc.write_register(REGISTER, NEW_VALUE, DECIMALS, functioncode=6, signed=True)
mmc.write_register(5, -18, 1, 6, signed=True) <- this would set the value of register 5 to -18.
Cory C
  • 125
  • 1
  • 9