1. PanasonicDevice(松下 PLC 串口通信)
命名空间: TestWorks.Device.Panasonic
实现接口: IPLC
提供基于 Mewtocol 协议的松下 PLC 串口读写操作,支持位(R)和字(DT)的多种数据类型。
1.1 属性
| 名称 | 类型 | 描述 |
|---|---|---|
Name |
string |
设备实例名称 |
IsConnected |
bool |
当前是否已建立通信连接(只读) |
LogService |
ILogService |
日志服务注入接口,用于记录操作信息 |
1.2 构造函数
public PanasonicDevice(PanasonicConfig config, string name)
| 参数 | 类型 | 说明 |
|---|---|---|
config |
PanasonicConfig |
串口配置对象(包含 PortName, BaudRate, DataBits, StopBits, Parity) |
name |
string |
设备唯一标识名称 |
1.3 方法
Connect()
建立与 PLC 的串口连接,并执行一次测试读取(R1)以验证通信。
public bool Connect()
| 返回 | 说明 |
|---|---|
bool |
true 表示连接成功且通信正常;false 表示失败 |
异常: 捕获所有异常并记录日志,不向外抛出。
Disconnect()
关闭当前串口连接并释放资源。
public void Disconnect()
GetR(string address)
读取单个 R 位(继电器/内部位)的值。
public bool? GetR(string address)
| 参数 | 类型 | 说明 |
|---|---|---|
address |
string |
地址,如 “R1″、”R100” |
| 返回 | 说明 |
|---|---|
bool? |
true/false 表示读取成功;null 表示读取失败或未连接 |
示例:
bool? value = device.GetR("R10");
if (value.HasValue) {
Console.WriteLine($"R10 = {value.Value}");
}
SetR(string address, bool val)
写入单个 R 位的值。
public bool SetR(string address, bool val)
| 参数 | 类型 | 说明 |
|---|---|---|
address |
string |
地址,如 “R1” |
val |
bool |
要写入的值 |
| 返回 | 说明 |
|---|---|
bool |
true 表示写入成功;false 表示失败 |
ReadDTInt16(string address)
读取 DT 数据寄存器(16 位有符号整数)。
public short? ReadDTInt16(string address)
| 参数 | 类型 | 说明 |
|---|---|---|
address |
string |
地址,如 “DT100” |
| 返回 | 说明 |
|---|---|
short? |
成功返回数值,失败返回 null |
ReadDTInt32(string address)
读取 DT 数据寄存器(32 位有符号整数)。
public int? ReadDTInt32(string address)
| 参数 | 类型 | 说明 |
|---|---|---|
address |
string |
地址,如 “DT200” |
| 返回 | 说明 |
|---|---|
int? |
成功返回数值,失败返回 null |
ReadDTFloat(string address)
读取 DT 数据寄存器(单精度浮点数)。
public float? ReadDTFloat(string address)
| 参数 | 类型 | 说明 |
|---|---|---|
address |
string |
地址,如 “DT300” |
| 返回 | 说明 |
|---|---|
float? |
成功返回数值,失败返回 null |
WriteDTInt16(string address, short val)
写入 DT 数据寄存器(16 位有符号整数)。
public bool WriteDTInt16(string address, short val)
WriteDTInt32(string address, int val)
写入 DT 数据寄存器(32 位有符号整数)。
public bool WriteDTInt32(string address, int val)
WriteDTFloat(string address, float val)
写入 DT 数据寄存器(单精度浮点数)。
public bool WriteDTFloat(string address, float val)