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.