Giter Club home page Giter Club logo

fx_plc_communication's Introduction

通信协议:

1.读取软元件或寄存器内的值

向PLC发送的hex数据:02 30 30 31 30 31 30 31 03 35 36

**02:**起始字符串

**30:**读命令

**30 31 30 31 :**元件地址,各元件的地址查询地址对照表。例如M8~M15的元件地址是0x0101,将0x0101转换为字符串"0101",各字符对应的ascii码为\x30\x31\x30\x31\

**30 31 :**读取几个字节的数据,将整数1转换为字符串“01”,“01”各字符对应的ascii码为\x30\x31\

​ 1个字节(bytes)=8bit,1个字节包含8个开关量。1个寄存器有16位,占用2bytes,因此一个寄存器可以存放一个16位的整数,也可以用2个寄存器共同存放一个32位的整数

**03:**结束字符串

**35 36:**和校验,0x30+0x30+0x31+0x30+0x31+0x30+0x31+0x03=0x156,0x156转换为字符串"156"。字符串的后两位为"56",各字符对应的ascii码为\x35\x36\

PLC返回的hex数据,下面是读取2个寄存器(4字节)返回的数据:

02 43 33 30 42 46 41 30 31 03 44 33

**02:**起始字符串

**43 33 30 42 46 41 30 31:**数据段

\x43\x33\x30\x42\x46/x41\x30\x31各ascii码对应的字符为“C30BFA01”,因为是2个寄存器,将“C30BFA01”拆分为“C30B“,”FA01”;因为网络传输采用的是小端编码,因此将“C30B“,”FA01”转换为“0BC3”,“01FA”;0x0BC3,0x01FA对应的十进制分别为3011,506,这就是2个寄存器内存储的值

**03:**结束字符串

**35 36:**和校验,计算方法同上

2.软元件的置位与复位

向PLC发送的hex数据:02 37 30 31 30 35 03 30 30

**02:**起始字符串

**37:**置位命令,将开关量置1,38未复位命令,将开关量置0

**30 31 30 35 :**开关量的地址

开关量的地址与上方软元件的地址是不同的,计算方式为:软元件基址*8+偏移量

**03:**结束字符串

**35 36:**和校验,计算方式同上

3.向寄存器内写入值

pass


PLC地址对照表

位元件地址表:

image-20200326201413205

寄存器地址表:

image-20200326201433516


程序的使用

1.在seting.ini中设置串口连接的参数,需要与PLC内的串口参数设置成相同的。

PLC内的串口参数可以通过GX WORK设置

image-20200326201607297

2.代码的使用

from FX_PLC_ctl import config_ser,Set_Reset,read_onoff_element,read_register

if __name__=="__main__":
    #创建串口连接
    ser=config_ser()
    
    #将Y1的值置为1
    Set_Reset(ser,"Y1",1)
    
  	#将Y1的值置为0
    Set_Reset(ser,"Y1",0)
    
    #从Y04的地址开始读取,读取24个开关量
    read_onoff_element(ser,"Y4",24)
    
    #从寄存器D21开始读取,读取3个寄存器的值(16位整数格式)
    read_register(ser,"D21",3)
    
    #关闭串口
    ser.close()

fx_plc_communication's People

Contributors

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