Metadata-Version: 2.4
Name: melsec-mc-protocol
Version: 1.0.1
Summary: 三菱MC协议客户端实现
Author: DINO.Z
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: author
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: license-file
Dynamic: requires-python
Dynamic: summary

# Melsec MC Protocol

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

## 功能特性

- 支持二进制协议
- 支持读取单个寄存器
- 支持写入单个寄存器
- 支持批量读取寄存器
- 支持批量写入寄存器
- 支持多种设备类型：X（输入继电器）、Y（输出继电器）、M（内部继电器）、D（数据寄存器）、W（链接寄存器）、R（文件寄存器）

## 安装

```bash
pip install melsec-mc-protocol
```

## 使用示例

```python
from melsec_mc import MelsecMCProtocol, DeviceType

# 创建MC协议客户端实例
plc = MelsecMCProtocol(host="192.168.1.100", port=1025)

# 连接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失败")
```

## API 文档

### MelsecMCProtocol 类

#### 初始化

```python
MelsecMCProtocol(host: str, port: int = 1025, protocol_type: MCProtocolType = MCProtocolType.BINARY)
```
- `host`: PLC IP地址
- `port`: 端口号，默认1025
- `protocol_type`: 协议类型，默认二进制协议

#### 方法

- `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`: 批量写入寄存器

### DeviceType 枚举

- `X`: 输入继电器
- `Y`: 输出继电器
- `M`: 内部继电器
- `D`: 数据寄存器
- `W`: 链接寄存器
- `R`: 文件寄存器

## 注意事项

- 仅支持二进制协议，ASCII协议暂未实现
- 批量操作的数量限制为1-960
- 默认超时时间为5秒

## 许可证

MIT License
