-
Notifications
You must be signed in to change notification settings - Fork 0
/
RESTdemo.py
142 lines (119 loc) · 5.1 KB
/
RESTdemo.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import logging
import os
import random
import com.Phoenixcontact.REST as PLCnREST
import com.Phoenixcontact.utils as PLCnUtils
'''
--PhoenixContect-China
--STE : SongYantao@Phoenixcontact.com.cn
Version : V0.3
Notice :
require package :requests
Variables on PLC should have HMI_tag
EHMI is required ,just create an empty page
set the following vars in PLC
|---global
|-A (DWORD)
|-B (TIME)
|-C (BOOL)
|-F (LINT)
|---MainInstance
|-Value1 (INT)
|-Value2 (REAL)
|-Value3 (STRING)
'''
def Demo():
# 创建客户端
# Create a client attach to PLCnext
client = PLCnREST.NewClient('192.168.124.10')
client.PLCnUserName = 'admin'
client.PLCnPasswd = '42bad0fd'
client.sessionMode = True
client.connect()
# 创建读取组(方便管理)
# Creat some Group for read function
GlobalGroup = client.registerReadGroups(['A', 'B', 'C']) # 全局变量组 An example group with some global variables
MainInstanceGroup = client.registerReadGroups(
['MainInstance.Value1', 'MainInstance.Value2',
'MainInstance.Value3']) # 任务实例组 An example group with some variables in MainInstance
# if the variable doesn't exist (or doesn't have HMI_Tag) ,the RESTException will be throwed
print('异常处理实验')
print('If there is no variable called \'AAA\',you will see the following message')
try:
errorGroup = client.registerReadGroups(['AAA'])
except PLCnREST.RESTException as E:
print('\t{}'.format(E.message))
def WriteData():
# 写变量
# Write data to plc
tmp1 = random.randint(0, 3000)
tmp2 = ''.join(random.sample('zyxwvutsrqponmlkjihgfedcba', 10))
tmp3 = True if tmp1 < 1500 else False
# Send data
client.writeDatas({'A': tmp1, 'C': tmp3, 'MainInstance.Value3': str(tmp2)})
# 获取组内成员数据类型:
print("Here we can get the variables' type of group")
print("组成内成员数据类型:")
print('\t{}'.format(GlobalGroup.checkMemberType()))
print('\t{}'.format(MainInstanceGroup.checkMemberType()))
print('*' * 100)
# 输出组内所有成员值:
# Here we get all values of group
print('get all values of group:')
print('输出组内所有成员值:')
for i in range(2):
WriteData()
print('\t' + str(GlobalGroup.results_dict))
print('\t' + str(MainInstanceGroup.results_dict))
print('\t------')
# This method is more useful
print('Use \'results_list\' method can get list directly: ')
val_A, val_B, val_C = GlobalGroup.results_list
print('\tval_A : {}\tval_B : {}\tval_C : {}'.format(val_A, val_B, val_C))
print('*' * 100)
# 从组内提取单个变量值:
# Or we just want only one variable's value of group
# But Notice it will handle requests everytime we call,this is just an additional function in some situation
print("Get values by \"group['member']\" ")
print('直接从组内提出某一变量值')
print('\tA : {}\tB : {}\tC:{}'.format(GlobalGroup['A'], GlobalGroup['B'], GlobalGroup['C']))
print('\tValue1 : {}\tValue2 : {}\tValue3 : {}'.format(MainInstanceGroup['MainInstance.Value1'],
MainInstanceGroup['MainInstance.Value2'],
MainInstanceGroup['MainInstance.Value3']))
print('\t------')
for i in range(2):
WriteData()
print('\tA : {}\tC : {}\tvalue3:{}'.format(GlobalGroup['A'], GlobalGroup['C'],
MainInstanceGroup['MainInstance.Value3']))
print('\t------')
print('*' * 100)
# 独立方法读取随机若干变量
# Also we can read variables without group
# 'readDatas_dict' function's return type is dict
WriteData()
print('使用readDatas_dict方法:')
print('Get variables without group by readDatas_dict ')
Res = client.readDatas_dict(['A', 'C', 'F', 'MainInstance.Value3'])
print('\t--{}--{}--{}--{}--'.format(Res['A'], Res['C'], Res['F'], Res['MainInstance.Value3']))
WriteData()
# 'readDatas_list' function's return type is list
print('使用readDatas_list方法:')
print('Get variables without group by readDatas_list ')
varA, varC, varF, var3 = client.readDatas_list(['A', 'C', 'F', 'MainInstance.Value3'])
print('\t--{}--{}--{}--{}--'.format(varA, varC, varF, var3))
print('*' * 100)
# 显示客户端的所有组信息
# show the group information belongs to client
print('show the group information belongs to client:')
print('显示客户端的所有组信息')
print('\t{}'.format(client.reportGroups()))
print('*' * 100)
print('-----Enjoy !\t------')
if __name__ == '__main__':
# setting log function
_localpath = os.path.split(os.path.abspath(__file__))[0]
_logpath = os.path.join(_localpath, 'log/')
log = PLCnUtils.Logger.Log()
log.setLogConfig(level=logging.DEBUG, logPath=_logpath, logFilename='Outputs.log')
# start Demo
Demo()