LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Rotary table WEISS Labview

Solved!
Go to solution

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.

0 Kudos
Message 11 of 23
(305 Views)

So please post the NI Modbus free library version and describe which error and where, so we can check it.

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
0 Kudos
Message 12 of 23
(295 Views)

@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()

 

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 13 of 23
(285 Views)

@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. 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 14 of 23
(284 Views)

Ask them for the MODBUS protocol. 

0 Kudos
Message 15 of 23
(273 Views)

That's what I have been doing, but would nice having only the Labview application.

0 Kudos
Message 16 of 23
(243 Views)

henk1000_0-1721889673424.png

henk1000_1-1721889688815.png

 

 

0 Kudos
Message 17 of 23
(240 Views)

@henk1000 wrote:

henk1000_0-1721889673424.png

henk1000_1-1721889688815.png

 

 


Thanks, that helps. Error 56 is a timeout error: 

 

https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019Lz2SAE&l=en-US

 

 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 18 of 23
(226 Views)

I don't have that modbus library, can you set the timeout? 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 19 of 23
(225 Views)

What happens if you try this ? 

LVNinja_0-1721917000027.png

 

0 Kudos
Message 20 of 23
(216 Views)