Python Modbus

Поделиться
HTML-код
  • Опубликовано: 13 фев 2023
  • Modbus is a communication standard to transfer values between computers. It is commonly used with Programmable Logic Controllers (PLCs), Human Machine Interfaces (HMIs), and other networking applications. It has several limitations that motivated the creation of newer standards such as OPC UA. Although it is an old standard, several legacy systems still support this protocol. There are several serial or network connection possibilities including RS232, RS422, RS485 (serial) or TCP/IP (network).
    Modbus Source Code: apmonitor.com/dde/index.php/M...
    A Modbus server has coils, discrete inputs, input registers, and holding registers. The coils and discrete inputs are a 1 or 0 (on or off) while the registers are 16-bit values (0-65,535 for unsigned integer). A floating point 32-bit number uses two registers, and a double precision 64-bit number uses four registers. The coil numbers start with 0 and span from 00001 to 09999, discrete input numbers start with 1 and span from 10001 to 19999, and holding register numbers start with 4 and span from 40001 to 49999.
    The coils and discrete inputs hold 1 bit each as a 0 or 1 (off/on). The input registers and holding registers have 16 bits each. A 16-bit unsigned integer can store integer values between 0 and 65,535 while a signed integer uses an extra bit to store the positive or negative sign and can store values between -32,768 to 32,767. A 32-bit (7 digits of accuracy) or 64-bit (14 digits of accuracy) number can also be sent, and the values are sent across multiple 16-bit registers with 2 registers for 32-bit numbers and 4 registers for 64-bit numbers.
    The pymodbus Python package is a full-featured set of methods that includes a Modbus server and client. Install the pymodbus package with pip from the PyPI repository. The code in these examples use version 3.0.2.
  • НаукаНаука

Комментарии • 29

  • @prashkd7684
    @prashkd7684 Год назад +3

    Would love to see videos on Python application for other Industrial comms like Profinet, EGD 👍

    • @apm
      @apm  Год назад +4

      Here is a recent Python package for Profinet comms: pypi.org/project/pnio-dcp/ EGD communication is only for GE PLCs so it is unlikely that it will get Python support. I included tutorials on MODBUS, MQTT, OPC UA, and Websockets. There are also other communication protocol APIs for apps that could also be an interesting development for industrial control systems that start using video, audio, and image data: apmonitor.com/dde/index.php/Main/DataTransfer

  • @paulramasco6769
    @paulramasco6769 Год назад +2

    This was very informative, thank you. I'm about to start working with some motor drivers that use modbus so it's timely as an overview.

  • @edinetgrunhed6000
    @edinetgrunhed6000 9 месяцев назад +2

    exactly what i'm looking for

  • @PLCAutomations
    @PLCAutomations 10 месяцев назад +1

    Are legacy restrictions on the address space still used in real life? It seemed to me that in the modbas specification this was indicated as outdated back in 1996.

    • @apm
      @apm  10 месяцев назад +1

      Yes, many of the PLC manufacturers use old standards.

  • @edwintoapanta1975
    @edwintoapanta1975 Год назад +1

    hello, it is possible to read/write negatives values from the input and holding register with pymodbus?

    • @apm
      @apm  Год назад

      Yes, either use a signed integer or a 32 bit or 64 bit float

  • @embetronix
    @embetronix Год назад +1

    I want to control a VFD from the computer. Can you give me some ideas? Like how I should proceed, what all would I be needing, etc. Any guidance will be really helpful. Thanks.

    • @apm
      @apm  Год назад

      You will need to see if there is a Modbus or OPC connection to the VFD.

  • @agustinguzman6375
    @agustinguzman6375 Год назад +1

    How do you setup the Client if I'm getting modbus messages via the serial port? (i.e. USB port COM1)

    • @apm
      @apm  Год назад

      Not sure, but you could try a serial python connection instead of the IP address (TCP/IP) approach shown in this video. I’ve only used RS232 connectors for serial Modbus, not USB. Let us know if you get something working.

  • @javierarredondo1827
    @javierarredondo1827 8 месяцев назад +1

    The modbus server script you have had create is for a serial modbus server right? what changes i can do to create a ethernet modbus server?

    • @rrc
      @rrc 8 месяцев назад +1

      The script is for Ethernet (TCP): ModbusTcpClient and ModbusTcpServer

  • @travelcompass591
    @travelcompass591 Год назад +1

    Could we just scrap MODBUS and work with sensors conected via LOAR, 5G, WIfi , etc directly from a Linux such as Ubuntu core 22?

    • @apm
      @apm  Год назад

      That’s a great idea. MQTT broker and client architecture provides an interface for IOT. More information here: apmonitor.com/dde/index.php/Main/DataTransfer

  • @user-ec3dj9pp4j
    @user-ec3dj9pp4j 5 месяцев назад +1

    Hi. Is it possible for pymodbus package to retrieve the data from Modbus TCP/IP driver from Kepware KepServerEX? If yes, how do I get the actual values of a tag? Like in your previous videos on OPC UA Client, you were using the nodeid? Thanks.

    • @apm
      @apm  5 месяцев назад

      The easiest way to get the value is to use an OPC tag browser. It should be compatible with Kepware software.

    • @user-ec3dj9pp4j
      @user-ec3dj9pp4j 5 месяцев назад

      @@apm, i'm using Prosys OPC UA browser and I want to implement this through python. Do you mean that, pymodbus package can use the tag (nodeid, browsername) shown in the browser to get its value from my Modbus TCP/IP driver? Also, last time, I was following your tutorial on OPC UA Client, with that opcua package, can I use it to get the value of a tag from Modbus TCP/IP driver? Thanks in advance.

  • @fburton8
    @fburton8 Год назад +1

    I am planning to trigger activity (image acquisition) on a slave computer using a signal from another master computer. (Not sure if master/slave are acceptable terms these days - apologies if not.) I will also be transferring strings. A preliminary trial shows that using IP (Internet Protocol) and an ethernet cable connecting the two computers works reliably. Seeing this video, I am now wondering if I should be considering Modbus as an alternative. If I want the lowest latency, should I stick with 'simple' IP? I suspect the answer is try both and measure, but if one is known to be significantly faster than the other maybe I don't need to.

    • @apm
      @apm  Год назад +1

      Modbus is fast. Websockets may be the fastest. Here is an overview of methods: apmonitor.com/dde/index.php/Main/DataTransfer

  • @fburton8
    @fburton8 Год назад +1

    What are coils? Does the name come from a very specific initial use for modbus?

    • @prashkd7684
      @prashkd7684 Год назад

      Coils that drive relays.. its a term used in electrical automation industry.

    • @fburton8
      @fburton8 Год назад

      @@prashkd7684 Thanks, that makes sense.

    • @apm
      @apm  Год назад

      @@fburton8 @prashkd7684 is correct. The term coil has its roots in digital outputs operating solenoid coils in an industrial environment.

  • @abhishektiwari007
    @abhishektiwari007 4 месяца назад +1

    Hi @APM,
    Your tutorials are amazing! 🙌
    I've set up a Modbus TCP server in Python following your guidance. Now, I'm trying to make the register data dynamic, updating them based on real-time variables in Python.
    please provide solution achieve this?
    Thanks a bunch!

    • @apm
      @apm  4 месяца назад

      You can write or read from the register to update the values at whatever frequency you prefer. Try setting up a loop in Python to write to and read from the register to test it.

    • @tomaskrampe7080
      @tomaskrampe7080 Месяц назад +1

      ​@@apm Hi, can you advise how to do it?

    • @apm
      @apm  Месяц назад +1

      @@tomaskrampe7080 sure, here is a client is a good test for this:
      from pymodbus.client import ModbusTcpClient as ModbusClient
      from pymodbus.constants import Endian
      from pymodbus.payload import BinaryPayloadBuilder
      from pymodbus.payload import BinaryPayloadDecoder
      import time
      print('Start Modbus Client')
      client = ModbusClient(host='127.0.0.1', port=502)
      reg = 0
      address = 0
      # Initialize data
      data = [0.1, 1.1, 2.1, 3.1, 4.1]
      try:
      for i in range(10): # Run for 10 cycles (1 second)
      print('-' * 5, 'Cycle ', i, '-' * 30)
      time.sleep(0.1) # 10 Hz frequency
      # Increment data by one
      for i, d in enumerate(data):
      data[i] = d + 1
      # Write holding registers (40001 to 40005)
      print('Write', data)
      builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Little)
      for d in data:
      builder.add_16bit_int(int(d))
      payload = builder.build()
      result = client.write_registers(int(reg), payload, skip_encode=True, unit=int(address))
      # Read holding registers
      rd = client.read_holding_registers(reg, len(data)).registers
      print('Read', rd)
      finally:
      client.close()
      ---Set up and run the server first before running the client---
      # Modbus server (TCP)
      from pymodbus.server import StartTcpServer
      from pymodbus.device import ModbusDeviceIdentification
      from pymodbus.datastore import ModbusSequentialDataBlock
      from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
      def run_async_server():
      nreg = 200
      # initialize data store
      store = ModbusSlaveContext(
      di=ModbusSequentialDataBlock(0, [15]*nreg),
      co=ModbusSequentialDataBlock(0, [16]*nreg),
      hr=ModbusSequentialDataBlock(0, [17]*nreg),
      ir=ModbusSequentialDataBlock(0, [18]*nreg))
      context = ModbusServerContext(slaves=store, single=True)
      # initialize the server information
      identity = ModbusDeviceIdentification()
      identity.VendorName = 'APMonitor'
      identity.ProductCode = 'APM'
      identity.VendorUrl = 'apmonitor.com'
      identity.ProductName = 'Modbus Server'
      identity.ModelName = 'Modbus Server'
      identity.MajorMinorRevision = '3.0.2'
      # TCP Server
      StartTcpServer(context=context, host='localhost', identity=identity, address=("127.0.0.1", 502))
      if __name__ == "__main__":
      print('Modbus server started on localhost port 502')
      run_async_server()