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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

plc-python's Issues

FBS PLC

請問能夠上傳這篇中PLC方面的程式嗎?
感謝

請教PLC型號與週邊設備

您好,

看了您的python與PLC共舞教學覺得非常有幫助, 但對於PLC這些設備感到非常陌生, 想跟您請教您使用的PLC是哪一款, 哪裡可以買到, 如果要控制LED燈開關的話, 還需要再加上哪些設備呢?

謝謝您的教學.

使用mb_demo0搜尋後都是無

老師您好,感謝您的PLC與python的教學,讓我這個門外漢能夠有所了解,
我這邊想詢問的是在試了您的mb_demo0.py的參數搜索後每個組合都是無法成功接收到數據的,
如:
scan @ 9600 E 1
modbus test Error: Response length is invalid 0
scan @ 9600 E 2
modbus test Error: Response length is invalid 0
scan @ 19200 N 1

請問老師這邊是否能夠有相關的幫助呢?
我所使用的是三菱 FX3G-40MR/ES,使用rs232連接至電腦,
期望老師或著各路好手能夠幫助小弟解惑,謝謝!

rs232 rj45

mbComPort = 'COM7' # your RS-485 port. for linux --> "/dev/ttyUSB2"
baudrate = 9600
databit = 8 #7, 8
parity = 'N' #N, O, E
stopbit = 1 #1, 2
mbTimeout = 100 # ms

想請問若是用RJ45 ethernet 連接 有哪邊可以參考語法嗎
謝謝您

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.