07-24-2024 08:41 AM
That is the Python code. It works for me. I have tried 3 libraries of Modbus in Labview including the NI Modbus library. Tried writing to single registers, multiple registers, coils, etc. Device number, time out, address, etc.
Always comes with an error when Labview executes the block to read.
07-24-2024 09:37 AM
So please post the NI Modbus free library version and describe which error and where, so we can check it.
07-24-2024 10:14 AM
@henk1000 wrote:
That is the Python code. It works for me. I have tried 3 libraries of Modbus in Labview including the NI Modbus library. Tried writing to single registers, multiple registers, coils, etc. Device number, time out, address, etc.
Always comes with an error when Labview executes the block to read.
Seems pretty simple enough, please attach your LV code to see what could be the problem.
from pymodbus.client import ModbusTcpClient
def read_modbus(client, a, b):
"""A dummy docstring."""
v = client.read_holding_registers(a,b).registers
position = hex_to_dec(v[0], v[1], 10000)
speed = hex_to_dec(v[4], v[5], 10000)
torque = hex_to_dec(v[8], v[9], 1000)
motor_state = v[12]
print(f"position: {position}")
print(f"speed: {speed}")
print(f"torque: {torque}")
print(f"motor_state: {motor_state}")
def hex_to_dec(reg1, reg2, factor):
"""A dummy docstring."""
a = str(hex(reg1))
a = a[2:]
b = hex(reg2)
b = b[2:]
c = "0x" + b + a
c = int(c, 16) / factor # normaly 10000
return c
def writeModbus(client, a, b):
"""A dummy docstring."""
try:
write_to_motor = client.write_registers(a, b)
assert not write_to_motor.isError()
except ConnectionError as errorcom:
raise SystemExit from errorcom
def modBus():
"""A dummy docstring."""
try:
client = ModbusTcpClient('192.168.1.200', port=502)
print("Modbus connected")
except ConnectionError as errorcom:
print("Can not connect to PLC!")
raise SystemExit from errorcom
return client
connect = modBus()
read_modbus(connect, 60, 13) # Read from 60, 13 registers
writeModbus(connect, 0, 1) # Write on register 0, value 1
connect.close()
07-24-2024 10:15 AM
@henk1000 wrote:
That is the Python code. It works for me. I have tried 3 libraries of Modbus in Labview including the NI Modbus library. Tried writing to single registers, multiple registers, coils, etc. Device number, time out, address, etc.
Always comes with an error when Labview executes the block to read.
Well if you are having so much trouble with the LabVIEW modbus libs and the Python modbus lib works, just call the python code in LabVIEW with a python node, see here in LabVIEW for examples:
Examples for using the Python Node are available in the LabVIEW Example Finder (found in Help >> Find Examples...) under Communicating with External Applications >> Using External Code >> Integrating Python Code.
Or if you must make it work in pure LabVIEW, post the error that you got.
07-24-2024 10:48 AM
Ask them for the MODBUS protocol.
07-25-2024 01:28 AM
That's what I have been doing, but would nice having only the Labview application.
07-25-2024 01:41 AM
07-25-2024 08:52 AM
@henk1000 wrote:
Thanks, that helps. Error 56 is a timeout error:
https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019Lz2SAE&l=en-US
07-25-2024 08:53 AM
I don't have that modbus library, can you set the timeout?
07-25-2024 09:16 AM
What happens if you try this ?