Giter Club home page Giter Club logo

plc-python's Introduction

PLC & Python

PLC

PLC

  • Programmable Logic Controller,簡稱PLC
  • 先來看wiki的 說明
  • 今天 demo要用的PLC為FATEK PLC

PLC 如何通訊?

  • 各家自有協定
  • 各種工業用常用protocol: modbus, canbus, ethercat...
  • 今天要說明的是最常見的Modbus

Modbus in Python

Python與PLC共舞

  • demo1: 使用modbus控制PLC的relay輸出,點亮家中的照明用電燈  - 家用110V E27 LED燈泡:
    家用110V E27 LED燈泡

  • demo2: 使用modbus取得開關狀態-->常見的有保全使用的磁簧開關
    磁簧開關

    • DI和DO混放,所以要查表才能了解
    • FATEK說明書的Modbus Table節錄
      PLC的Modbus Table
  • demo3: 讀電錶的資訊

會後補充

  • 程式的說明有再增加一個jupyter notebook的說明範例,可以比較清楚的看到執行的結果,有興趣可以參考這篇

實作補充

  • 在實際案場你在使用modbus/tcp時,也許會遇到設備回回來的header長度欄位是錯的,但後面帶的pdu資料卻是正確的,這時modbus_tk就沒辨法正常運作,可以參考以下的解法:

  • 修改 python資料夾下 Lib\site-packages\modbus_tk 中的 modbus_tcp.py (我使用的版本是 '0.5.10'),把 _recv 函式改成如下

    def _recv(self, expected_length=-1):
        """
        Receive the response from the slave
        Do not take expected_length into account because the length of the response is
        written in the mbap. Used for RTU only
        """
        to_be_recv_length = expected_length-2+6 #MB/TCP data length must add 6 byte header, sub 2 byte crc
        expected_pdu_length = expected_length-2
        response = to_data('')
        length = 255
        while len(response) < to_be_recv_length: #for H5A: length error issue
            rcv_byte = self._sock.recv(1)
            if rcv_byte:
                response += rcv_byte
                if len(response) == 6:
                    #to_be_recv_length = struct.unpack(">HHH", response)[2]
                    #length = to_be_recv_length + 6
                    if response[5] != expected_pdu_length:
                        print('length field of header error, fix: %s-->%s' %(response[5], expected_pdu_length))
                        response = response[:5] + struct.pack("B", expected_pdu_length)

            else:
                break
        retval = call_hooks("modbus_tcp.TcpMaster.after_recv", (self, response))
        if retval is not None:
            return retval
        return response

Exception code 補充

pic

plc-python's People

Contributors

maloyang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.