BasePLC.cs
2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using RCS.Model.Comm;
using RCS.Model.Entity;
namespace RCS.Communication
{
/// <summary>
/// PLC顶层接口
/// </summary>
public abstract class BasePLC
{
#region 变量、属性
/// <summary>
/// PLC的IP地址
/// </summary>
public string IP { get; set; }
#endregion
#region PLC操作
/// <summary>
/// 连接,通常指定PLC的初始化请在连接中进行
/// </summary>
/// <returns></returns>
public abstract BllResult Connect();
/// <summary>
/// 断开,通常在断开之前请先停止逻辑处理,延迟后再调用
/// </summary>
/// <returns></returns>
public abstract BllResult DisConnect();
/// <summary>
/// 获取连接状态
/// hack:对于使用OPC实现,此链接状态指示为client与OPC服务器的连接状态;所以使用OPC的情况下需要额外检测读写是否正确。
/// </summary>
/// <returns></returns>
public abstract BllResult GetConnectStatus();
/// <summary>
/// 读取地址
/// </summary>
/// <param name="equipmentProps"></param>
/// <returns></returns>
public abstract BllResult Reads(List<Base_EquipmentProp> equipmentProps);
/// <summary>
/// 写入地址
/// </summary>
/// <param name="equipmentProps"></param>
/// <returns></returns>
public abstract BllResult<List<string>> Writes(List<Base_EquipmentProp> equipmentProps);
#endregion
#region 字节位操作
//index从0开始
//获取取第index位
public int GetBit(byte b, int index) { return ((b & (1 << index)) > 0) ? 1 : 0; }
//将第index位设为1
public byte SetBit(byte b, int index) { return (byte)(b | (1 << index)); }
//将第index位设为0
public byte ClearBit(byte b, int index) { return (byte)(b & (byte.MaxValue - (1 << index))); }
//将第index位取反
public byte ReverseBit(byte b, int index) { return (byte)(b ^ (byte)(1 << index)); }
#endregion
}
}