Linuxcnc pymodbus example for remote I/O module

  • Grotius
  • Grotius's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
27 Apr 2018 23:13 - 28 Apr 2018 01:21 #109736 by Grotius
Hi,

This document can save you time. It is for integrating in custom linux applications.
For the device on the picture all basic function's are working.

Tested on linuxcnc 2.8 with debian 64bit stretch with builbot reprository set for update to master :
deb buildbot.linuxcnc.org/ stretch master-rtpreempt

With package manager install module : pymodbus

In some linuxcnc versions you have to be member of usergroup plugdevs and serial to get sudo acces to read i/o.
How to become a member of those groups, is a matter of googling. In linux mint it is very easy to select this groups.



#MODBUS this section must be above everything in your code.
from __future__ import division
#/MODBUS


#MODBUS
import pymodbus
import serial
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient #initialize a serial RTU client instance
from pymodbus.transaction import ModbusRtuFramer
from pymodbus.constants import Endian              # Nodig voor 32-bit float getallen (2 registers / 4 bytes)
from pymodbus.payload import BinaryPayloadDecoder  # Nodig voor 32-bit float getallen (2 registers / 4 bytes)
from pymodbus.payload import BinaryPayloadBuilder  # Nodig om 32-bit floats te schrijven naar register
from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder

method = "rtu"
port = "/dev/ttyUSB0"
baudrate = 9600
stopbits = 1
bytesize = 8
parity = "N"
timeout = 0.01
retries = 2
 
client = ModbusClient(method = method, port = port, stopbits = stopbits, bytesize = bytesize, parity = parity, baudrate = baudrate, timeout = timeout, retries = retries)
#connection = client.connect()
#/MODBUS


#Glade button example 

  def on_modbus_tool_pressed(self, widget, data=None):
    connection = client.connect()
    print connection
    y = 10  #10 volt
    client.write_register(0, float(4095 / 10) * y, unit=1)
    client.close()
    
    connection = client.connect()
    print connection
    b = 8  #8 volt
    client.write_register(1, float(4095 / 10) * b, unit=1)
    client.close()

    connection = client.connect()
    print connection
    client.write_coil(0, True)
    client.write_coil(1, True)
    client.write_coil(2, True)
    client.write_coil(3, True)
    client.close()

    connection = client.connect()
    print connection
    client.write_coil(0, False)
    client.write_coil(1, False)
    client.write_coil(2, False)
    client.write_coil(3, False)
    client.close()

    connection = client.connect()
    print connection
    analog_input = client.read_input_registers(0, 4, unit=1) 
    print data 
    print "value_0:", analog_input.registers[0]
    print "value_1:", analog_input.registers[1]
    print "value_2:", analog_input.registers[2]
    print "value_3:", analog_input.registers[3]
    client.close()

    connection = client.connect()
    print connection
    digital_input = client.read_discrete_inputs(0, 4, unit=1) 
    print data 
    print "value_0:", digital_input.bits[0]
    print "value_1:", digital_input.bits[1]
    print "value_2:", digital_input.bits[2]
    print "value_3:", digital_input.bits[3]
    client.close()

ali shop : modbus rtu module
Attachments:
Last edit: 28 Apr 2018 01:21 by Grotius.

Please Log in or Create an account to join the conversation.

Moderators: HansU
Time to create page: 0.066 seconds
Powered by Kunena Forum