Skip to main content

三菱MC协议客户端实现

Project description

Melsec MC Protocol

三菱MC协议客户端实现,用于与三菱PLC进行通信。

功能特性

  • 支持二进制协议和ASCII协议
  • 支持3E帧和4E帧
  • 支持多种PLC系列:Q、L、QnA、iQ-L、iQ-R
  • 支持读取单个寄存器
  • 支持写入单个寄存器
  • 支持批量读取寄存器
  • 支持批量写入寄存器
  • 支持多种设备类型:X(输入继电器)、Y(输出继电器)、M(内部继电器)、D(数据寄存器)、W(链接寄存器)、R(文件寄存器)、S(状态继电器)、T(定时器)、C(计数器)、L(锁存继电器)、B(链接继电器Q系列)、F(特殊继电器)、V(变址寄存器)、Z(变址寄存器)、U(链接寄存器QnA系列)、G(链接继电器A系列)、P(边缘继电器)、I(立即输入继电器)、Q(立即输出继电器)、SM(特殊辅助继电器)、SD(特殊数据寄存器)、TS(定时器设定值)、TC(定时器当前值)、CS(计数器设定值)、CC(计数器当前值)

安装

pip install melsec-mc-protocol

使用示例

示例1: 二进制协议 + 3E帧 + Q系列

from melsec_mc import MelsecMCProtocol, DeviceType, MCProtocolType, FrameType, PLCSeries

# 创建MC协议客户端实例
plc = MelsecMCProtocol(
    host="192.168.1.100", 
    port=1025, 
    protocol_type=MCProtocolType.BINARY, 
    frame_type=FrameType.TYPE3E, 
    plc_series=PLCSeries.Q
)

# 连接PLC
if plc.connect():
    print("连接PLC成功")
    
    try:
        # 读取单个数据寄存器D100
        value = plc.read_single_register(DeviceType.D, 100)
        print(f"D100 = {value}")
        
        # 写入单个数据寄存器D100
        success = plc.write_single_register(DeviceType.D, 100, 1234)
        print(f"写入D100: {'成功' if success else '失败'}")
        
        # 批量读取数据寄存器D0-D9
        values = plc.read_batch_registers(DeviceType.D, 0, 10)
        print(f"D0-D9 = {values}")
        
        # 批量写入数据寄存器D0-D4
        write_values = [100, 200, 300, 400, 500]
        success = plc.write_batch_registers(DeviceType.D, 0, write_values)
        print(f"批量写入D0-D4: {'成功' if success else '失败'}")
        
        # 读取输入继电器X0
        x_value = plc.read_single_register(DeviceType.X, 0)
        print(f"X0 = {x_value}")
        
        # 读取输出继电器Y0
        y_value = plc.read_single_register(DeviceType.Y, 0)
        print(f"Y0 = {y_value}")
        
        # 读取内部继电器M0
        m_value = plc.read_single_register(DeviceType.M, 0)
        print(f"M0 = {m_value}")
        
    except Exception as e:
        print(f"操作失败: {e}")
    
    finally:
        # 断开连接
        plc.disconnect()
        print("断开PLC连接")
else:
    print("连接PLC失败")

示例2: ASCII协议 + 3E帧 + L系列

from melsec_mc import MelsecMCProtocol, DeviceType, MCProtocolType, FrameType, PLCSeries

# 创建MC协议客户端实例
plc = MelsecMCProtocol(
    host="192.168.1.101", 
    port=1025, 
    protocol_type=MCProtocolType.ASCII, 
    frame_type=FrameType.TYPE3E, 
    plc_series=PLCSeries.L
)

# 连接PLC
if plc.connect():
    print("连接PLC成功")
    
    try:
        # 读取单个数据寄存器D100
        value = plc.read_single_register(DeviceType.D, 100)
        print(f"D100 = {value}")
        
        # 写入单个数据寄存器D100
        success = plc.write_single_register(DeviceType.D, 100, 5678)
        print(f"写入D100: {'成功' if success else '失败'}")
        
    except Exception as e:
        print(f"操作失败: {e}")
    
    finally:
        # 断开连接
        plc.disconnect()
        print("断开PLC连接")
else:
    print("连接PLC失败")

示例3: 二进制协议 + 4E帧 + iQ-R系列

from melsec_mc import MelsecMCProtocol, DeviceType, MCProtocolType, FrameType, PLCSeries

# 创建MC协议客户端实例
plc = MelsecMCProtocol(
    host="192.168.1.102", 
    port=1025, 
    protocol_type=MCProtocolType.BINARY, 
    frame_type=FrameType.TYPE4E, 
    plc_series=PLCSeries.iQ_R
)

# 连接PLC
if plc.connect():
    print("连接PLC成功")
    
    try:
        # 批量读取状态继电器S0-S9
        values = plc.read_batch_registers(DeviceType.S, 0, 10)
        print(f"S0-S9 = {values}")
        
        # 批量读取定时器当前值T0-T4
        values = plc.read_batch_registers(DeviceType.TC, 0, 5)
        print(f"T0-T4当前值 = {values}")
        
    except Exception as e:
        print(f"操作失败: {e}")
    
    finally:
        # 断开连接
        plc.disconnect()
        print("断开PLC连接")
else:
    print("连接PLC失败")

API 文档

MelsecMCProtocol 类

初始化

MelsecMCProtocol(host: str, port: int = 1025, protocol_type: MCProtocolType = MCProtocolType.BINARY, frame_type: FrameType = FrameType.TYPE3E, plc_series: PLCSeries = PLCSeries.Q)
  • host: PLC IP地址
  • port: 端口号,默认1025
  • protocol_type: 协议类型,默认二进制协议
  • frame_type: 帧类型,默认3E帧
  • plc_series: PLC系列,默认Q系列

方法

  • connect() -> bool: 建立连接,返回是否连接成功
  • disconnect(): 断开连接
  • read_single_register(device_type: DeviceType, address: int) -> int: 读取单个寄存器
  • write_single_register(device_type: DeviceType, address: int, value: int) -> bool: 写入单个寄存器
  • read_batch_registers(device_type: DeviceType, start_address: int, count: int) -> List[int]: 批量读取寄存器
  • write_batch_registers(device_type: DeviceType, start_address: int, values: List[int]) -> bool: 批量写入寄存器

MCProtocolType 枚举

  • BINARY: 二进制协议
  • ASCII: ASCII协议

FrameType 枚举

  • TYPE3E: 3E帧
  • TYPE4E: 4E帧

PLCSeries 枚举

  • Q: Q系列
  • L: L系列
  • QnA: QnA系列
  • iQ_L: iQ-L系列
  • iQ_R: iQ-R系列

DeviceType 枚举

  • X: 输入继电器
  • Y: 输出继电器
  • M: 内部继电器
  • D: 数据寄存器
  • W: 链接寄存器
  • R: 文件寄存器
  • S: 状态继电器
  • T: 定时器
  • C: 计数器
  • L: 锁存继电器
  • B: 链接继电器(Q系列)
  • F: 特殊继电器
  • V: 变址寄存器
  • Z: 变址寄存器
  • U: 链接寄存器(QnA系列)
  • G: 链接继电器(A系列)
  • P: 边缘继电器
  • I: 立即输入继电器
  • Q: 立即输出继电器
  • SM: 特殊辅助继电器
  • SD: 特殊数据寄存器
  • TS: 定时器设定值
  • TC: 定时器当前值
  • CS: 计数器设定值
  • CC: 计数器当前值

注意事项

  • 支持二进制协议和ASCII协议
  • 支持3E帧和4E帧
  • 支持多种PLC系列
  • 批量操作的数量限制为1-960
  • 默认超时时间为5秒

许可证

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

melsec_mc_protocol-1.1.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

melsec_mc_protocol-1.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file melsec_mc_protocol-1.1.0.tar.gz.

File metadata

  • Download URL: melsec_mc_protocol-1.1.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for melsec_mc_protocol-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f75b6994bca9e33736a5e6b0b69d9902cec29c1f9bf64514de88fc503e008e2f
MD5 47f73338ab9e1ebddee9ad3c62453ce2
BLAKE2b-256 6b199c43e85d4fa6ac412e16ffc06c8f1cb4c3b8ec696ab1bb57948626683361

See more details on using hashes here.

File details

Details for the file melsec_mc_protocol-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for melsec_mc_protocol-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65df89d7f9505bc6cd49fa960af7ea4f14ffdc1fa1c1f9a4d9a9a5a0a33360ec
MD5 4fce739c8ff2f491b1c0d9a561f68a9d
BLAKE2b-256 fba2c68a8b8a233944e6dc6faaef8298abdc15d1d086796932289c396a50ff5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page