|
|
|
@ -5,13 +5,6 @@ import com.sourceforge.snap7.moka7.S7Client;
|
|
|
|
import com.zhehekeji.web.service.PlcService;
|
|
|
|
import com.zhehekeji.web.service.PlcService;
|
|
|
|
import io.swagger.models.auth.In;
|
|
|
|
import io.swagger.models.auth.In;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.plc4x.java.api.PlcConnection;
|
|
|
|
|
|
|
|
import org.apache.plc4x.java.api.PlcDriverManager;
|
|
|
|
|
|
|
|
import org.apache.plc4x.java.api.messages.PlcReadRequest;
|
|
|
|
|
|
|
|
import org.apache.plc4x.java.api.messages.PlcReadResponse;
|
|
|
|
|
|
|
|
import org.apache.plc4x.java.api.types.PlcResponseCode;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
@ -64,6 +57,7 @@ public class PLCConnectionExample {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取一个连接
|
|
|
|
* 获取一个连接
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@ -278,14 +272,15 @@ public class PLCConnectionExample {
|
|
|
|
public boolean writePlcDataStatusErr(String plcId,int digit){
|
|
|
|
public boolean writePlcDataStatusErr(String plcId,int digit){
|
|
|
|
|
|
|
|
|
|
|
|
if (digit==1){
|
|
|
|
if (digit==1){
|
|
|
|
return writePlcDataStatus(addressMap.get(plcId+"-ET-out:"),digit,false);
|
|
|
|
return writePlcDataStatus((plcId+"-ET-out"),digit,true);
|
|
|
|
}else {
|
|
|
|
}else {
|
|
|
|
return writePlcDataStatus(addressMap.get(plcId+"-ED-out"),digit,false);
|
|
|
|
return writePlcDataStatus((plcId+"-ED-out"),digit,true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean writePlcDataStatus(int startOffset,int digit,boolean value){
|
|
|
|
|
|
|
|
|
|
|
|
public boolean writePlcDataStatus(String startOffset,int digit,boolean value){
|
|
|
|
|
|
|
|
|
|
|
|
S7Client client = getConnection();
|
|
|
|
S7Client client = getConnection();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
@ -295,9 +290,9 @@ public class PLCConnectionExample {
|
|
|
|
S7.SetBitAt(buffer, 0, digit, value);
|
|
|
|
S7.SetBitAt(buffer, 0, digit, value);
|
|
|
|
|
|
|
|
|
|
|
|
// 写回DB121
|
|
|
|
// 写回DB121
|
|
|
|
int result = client.WriteArea(S7.S7AreaDB, dbNumber, startOffset, sizeToRead, buffer);
|
|
|
|
int result = client.WriteArea(S7.S7AreaDB, dbNumber, addressMap.get(startOffset), sizeToRead, buffer);
|
|
|
|
if (result == 0) {
|
|
|
|
if (result == 0) {
|
|
|
|
log.info("写入成功");
|
|
|
|
log.info("写入成功位置:"+startOffset+":"+digit);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
updateConnection(client);
|
|
|
|
updateConnection(client);
|
|
|
|
@ -315,7 +310,6 @@ public class PLCConnectionExample {
|
|
|
|
|
|
|
|
|
|
|
|
// 分拣线PLC型号是西门子1518,机架号0,插槽1,IP是10.69.105.122。
|
|
|
|
// 分拣线PLC型号是西门子1518,机架号0,插槽1,IP是10.69.105.122。
|
|
|
|
//交互数据块地址是DB121
|
|
|
|
//交互数据块地址是DB121
|
|
|
|
/*
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// 创建S7Client实例
|
|
|
|
// 创建S7Client实例
|
|
|
|
S7Client client = new S7Client();
|
|
|
|
S7Client client = new S7Client();
|
|
|
|
@ -328,24 +322,25 @@ public static void main(String[] args) {
|
|
|
|
|
|
|
|
|
|
|
|
// 定义数据块和偏移量
|
|
|
|
// 定义数据块和偏移量
|
|
|
|
int dbNumber = 121; // DB121
|
|
|
|
int dbNumber = 121; // DB121
|
|
|
|
int startOffset = 48; // 起始地址
|
|
|
|
int startOffset = 74; // 起始地址
|
|
|
|
int sizeToRead = 4; // 读取4个字节
|
|
|
|
int sizeToRead = 1; // 读取4个字节
|
|
|
|
//
|
|
|
|
//
|
|
|
|
byte[] buffer = new byte[sizeToRead];
|
|
|
|
byte[] buffer = new byte[sizeToRead];
|
|
|
|
boolean bitValue = true;
|
|
|
|
boolean bitValue = true;
|
|
|
|
|
|
|
|
|
|
|
|
// // 读取DB121的数据
|
|
|
|
// // 读取DB121的数据
|
|
|
|
int result = client.ReadArea(S7.S7AreaDB, dbNumber, startOffset, sizeToRead, buffer);
|
|
|
|
int result = client.ReadArea(S7.S7AreaDB, dbNumber, startOffset, sizeToRead, buffer);
|
|
|
|
if (result == 0) {
|
|
|
|
if (result == 0) {
|
|
|
|
log.info("读取成功");
|
|
|
|
log.info("读取成功");
|
|
|
|
// 获取指定 bit 的值
|
|
|
|
// 获取指定 bit 的值
|
|
|
|
// bitValue= S7.GetBitAt(buffer, 0, 1);
|
|
|
|
bitValue= S7.GetBitAt(buffer, 0, 1);
|
|
|
|
// log.info(bitValue);
|
|
|
|
// log.info(bitValue);
|
|
|
|
|
|
|
|
//
|
|
|
|
int i = ((buffer[0] & 0xFF) << 24) |
|
|
|
|
// int i = ((buffer[0] & 0xFF) << 24) |
|
|
|
|
((buffer[1] & 0xFF) << 16) |
|
|
|
|
// ((buffer[1] & 0xFF) << 16) |
|
|
|
|
((buffer[2] & 0xFF) << 8) |
|
|
|
|
// ((buffer[2] & 0xFF) << 8) |
|
|
|
|
(buffer[3] & 0xFF);
|
|
|
|
// (buffer[3] & 0xFF);
|
|
|
|
log.info(i);
|
|
|
|
System.out.println(bitValue);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
log.info("读取失败,错误码: " + result);
|
|
|
|
log.info("读取失败,错误码: " + result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -353,23 +348,27 @@ public static void main(String[] args) {
|
|
|
|
// 修改第一个字节的第一个位为true(即设置bit 0)
|
|
|
|
// 修改第一个字节的第一个位为true(即设置bit 0)
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
S7.SetDIntAt(buffer,0,3001);
|
|
|
|
// S7.SetDIntAt(buffer,0,3001);
|
|
|
|
|
|
|
|
|
|
|
|
// 写回DB121
|
|
|
|
// 写回DB121
|
|
|
|
result = client.WriteArea(S7.S7AreaDB, dbNumber, startOffset, sizeToRead, buffer);
|
|
|
|
S7.SetBitAt(buffer, 0, 1, true);
|
|
|
|
if (result == 0) {
|
|
|
|
//
|
|
|
|
log.info("写入成功");
|
|
|
|
// // 写回DB121
|
|
|
|
} else {
|
|
|
|
result = client.WriteArea(S7.S7AreaDB, dbNumber,startOffset, sizeToRead, buffer);
|
|
|
|
log.info("写入失败,错误码: " + result);
|
|
|
|
// if (result == 0) {
|
|
|
|
}
|
|
|
|
// log.info("写入成功位置:"+startOffset+":"+0);
|
|
|
|
|
|
|
|
//// return true;
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
//// updateConnection(client);
|
|
|
|
|
|
|
|
// log.info("写入失败,错误码: " + result);
|
|
|
|
|
|
|
|
//// return false;
|
|
|
|
|
|
|
|
// }
|
|
|
|
// 断开连接
|
|
|
|
// 断开连接
|
|
|
|
client.Disconnect();
|
|
|
|
client.Disconnect();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
log.info("连接失败");
|
|
|
|
log.info("连接失败");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|