PlcConnectUtil.java
7.12 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.huaheng.pc.plc.util;
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import com.huaheng.pc.plc.domain.PlcEquipmentOffset;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.poi.ss.formula.functions.T;
import java.util.Map;
/**
* 西门子plc连接工具类
*/
public class PlcConnectUtil {
public static SiemensS7NetV siemensS7NetV_1=null;
public static SiemensS7NetV siemensS7NetV_2=null;
public static SiemensS7NetV siemensS7NetV_3=null;
public static SiemensS7NetV siemensS7NetV_4=null;
public static SiemensS7NetV start(String ipAddress,int order,int port){
if(order==1){
siemensS7NetV_1=new SiemensS7NetV(SiemensPLCS.S1500,ipAddress);
siemensS7NetV_1.setPort(port);
return siemensS7NetV_1;
}else if(order==2){
siemensS7NetV_2=new SiemensS7NetV(SiemensPLCS.S1500,ipAddress);
siemensS7NetV_2.setPort(port);
return siemensS7NetV_2;
}else if(order==3){
siemensS7NetV_3=new SiemensS7NetV(SiemensPLCS.S1500,ipAddress);
siemensS7NetV_3.setPort(port);
return siemensS7NetV_3;
}else if(order==4){
siemensS7NetV_4=new SiemensS7NetV(SiemensPLCS.S1500,ipAddress);
siemensS7NetV_4.setPort(port);
return siemensS7NetV_4;
}
return null;
}
public static OperateResult stop(SiemensS7NetV siemensS7Net){
return siemensS7Net.ConnectClose();
}
public static Boolean checkConnect(int order){
OperateResult result=null;
try {
if(order==1){
result= siemensS7NetV_1.ConnectServer();
}else if(order==2){
result= siemensS7NetV_2.ConnectServer();
}else if(order==3){
result= siemensS7NetV_3.ConnectServer();
}else if(order==4){
result= siemensS7NetV_4.ConnectServer();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
boolean tag=result.IsSuccess;
return tag;
}
/**
* 获取地址
* @param offset
* @return
*/
public static String getAddressByOffset(PlcEquipmentOffset offset) {
String dataType = offset.getDataType();
String address = null;
switch (dataType) {
case "int":
//short a=7; //int 16位
address = offset.getMethodName() + "INT" + offset.getOffset();
break;
case "dint":
//Integer aa=74; dint 32位
address = offset.getMethodName() + "DINT" + offset.getOffset();
break;
case "dword":
//32位 int aa=6666;
address = offset.getMethodName() + "D" + offset.getOffset();
break;
case "word":
//short aaa=21;
address = offset.getMethodName() + "MW" + offset.getOffset();
break;
case "bool":
//true 8位
address = offset.getMethodName() + "X" + offset.getOffset();
break;
case "char":
//byte b=8; 8位
address = offset.getMethodName() + "CHAR" + offset.getOffset();
break;
case "string":
//string1stringgstringggstringgggstringgggg
address = offset.getMethodName() + "CHAR" + offset.getOffset() + ",20";
break;
default:
address = offset.getMethodName() + "CHAR" + offset.getOffset() + ",20";
break;
}
return address;
}
/**
* 读plc数据
* @param dataType
* @param address
* @param siemensS7Net
* @return
*/
public Map<String,Object> readData(String dataType, String address,SiemensS7NetV siemensS7Net) {
Map<String,Object> map=new HashedMap<>();
map.put("dataType",dataType);
OperateResultExOne<T> result=null;
switch (dataType) {
case "int":
//short a=7; //int 16位
OperateResultExOne<Integer> result_data=siemensS7Net.ReadUInt16(address);
map.put("Content",result_data.Content);
map.put("IsSuccess",result_data.IsSuccess);
map.put("Message",result_data.Message);
break;
case "dint":
//Integer aa=74; dint 32位
OperateResultExOne<Integer> result_dint=siemensS7Net.ReadInt32(address);
map.put("Content",result_dint.Content);
map.put("IsSuccess",result_dint.IsSuccess);
map.put("Message",result_dint.Message);
break;
case "dword":
//32位 int aa=6666;
OperateResultExOne<Long> result_dword2=siemensS7Net.ReadUInt32(address);
map.put("Content",result_dword2.Content);
map.put("IsSuccess",result_dword2.IsSuccess);
map.put("Message",result_dword2.Message);
break;
case "word":
//short aaa=21;
OperateResultExOne<Short> result_word=siemensS7Net.ReadInt16(address);
map.put("Content",result_word.Content);
map.put("IsSuccess",result_word.IsSuccess);
map.put("Message",result_word.Message);
break;
case "bool":
//true 8位
OperateResultExOne<Boolean> result_bool=siemensS7Net.ReadBool(address);
map.put("Content",result_bool.Content);
map.put("IsSuccess",result_bool.IsSuccess);
map.put("Message",result_bool.Message);
break;
case "char":
//byte b=8; 8位
OperateResultExOne<Byte> result_char=siemensS7Net.ReadByte(address);
map.put("Content",result_char.Content);
map.put("IsSuccess",result_char.IsSuccess);
map.put("Message",result_char.Message);
break;
case "string":
//string1stringgstringggstringgggstringgggg
OperateResultExOne<String> result_string=siemensS7Net.ReadString(address);
map.put("Content",result_string.Content);
map.put("IsSuccess",result_string.IsSuccess);
map.put("Message",result_string.Message);
break;
default:
OperateResultExOne<String> result_default=siemensS7Net.ReadString(address);
map.put("Content",result_default.Content);
map.put("IsSuccess",result_default.IsSuccess);
map.put("Message",result_default.Message);
break;
}
return map;
}
public static void main(String[] args) {
siemensS7NetV_1=new SiemensS7NetV(SiemensPLCS.S1500,"127.0.0.1");
OperateResult result=siemensS7NetV_1.ConnectServer();
System.out.println(result.IsSuccess);
}
}