From 935275e79de8415ee3ca0b9a45da548aa6a2ee34 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期一, 30 六月 2025 08:21:36 +0800
Subject: [PATCH] 更新最新代码,上片页面领取任务传递固定值修改为动态获取线路
---
JiuMuMesParent/common/servicebase/src/main/java/com/mes/service/ModbusTcp.java | 206 +++++++++++++++++++++++++++++++++++++++------------
1 files changed, 158 insertions(+), 48 deletions(-)
diff --git a/JiuMuMesParent/common/servicebase/src/main/java/com/mes/service/ModbusTcp.java b/JiuMuMesParent/common/servicebase/src/main/java/com/mes/service/ModbusTcp.java
index 36430e7..6248f29 100644
--- a/JiuMuMesParent/common/servicebase/src/main/java/com/mes/service/ModbusTcp.java
+++ b/JiuMuMesParent/common/servicebase/src/main/java/com/mes/service/ModbusTcp.java
@@ -3,19 +3,17 @@
import com.mes.tools.HexConversion;
import com.mes.utils.HexUtil;
import lombok.extern.slf4j.Slf4j;
-import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
-import java.net.UnknownHostException;
-import java.text.SimpleDateFormat;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
+
+import static com.mes.tools.HexConversion.intToBytesDesc;
@Component
@Slf4j
@@ -27,73 +25,176 @@
private int Port;
public Socket socket =null;//閫氳
- ModbusTcp(){}
- public ModbusTcp(String Ip,int Port){
+ public String fileName ="";
+ public ModbusTcp(){}
+ public ModbusTcp(String Ip,int Port,String fileName){
this.Ip=Ip;
this.Port=Port;
+ this.fileName=fileName;
try {
- socket=new Socket(Ip,Port);
- }catch (UnknownHostException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
+ plcAgreement.put(this.fileName,new PlcAgreement(this.fileName));
+ } catch (Exception e) {
+ log.info("file error锛�"+this.fileName+",{}",e.getMessage());
}
}
//杩炴帴
- //@Scheduled(fixedDelay = 1000)
- public void a()throws Exception{
- //read();
- log.info("123");
+ public void connect(){
+ try {
+ this.socket=new Socket(Ip,Port);
+ this.socket.setSoTimeout(300);
+ }catch (Exception e) {
+ log.info("The IP address of the host cannot be determined:{}",e.getMessage());
+ }
+ }
+ //鍏抽棴杩炴帴
+ public void close(){
+ try {
+ this.socket.close();
+ }catch (Exception e) {
+ //log.info("杩炴帴鍏抽棴寮傚父:{}",e.getMessage());
+ }
+ }
+ //杩斿洖杩炴帴鐘舵��
+ public boolean isConnect(){
+ try {
+ this.socket.isConnected();
+ }catch (Exception e) {
+ log.info("Connection status exception:{}",this.socket);
+ }
+ return false;
}
//璇诲彇鏁版嵁
- public void read(PlcAgreement plcAgreement)throws Exception{
- int bufSizes = 0;
- byte[] msgs = new byte[2048];
- //鍐欏叆璇诲彇鍦板潃
- DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
- outToServer.write(HexConversion.stringToInt(plcAgreement.requestHead));
- outToServer.flush();
- //璇诲彇鍐呭
- DataInputStream in = new DataInputStream(socket.getInputStream());
- bufSizes = in.read(msgs);
- String message = HexConversion.byteToHexString(bufSizes, msgs);//鍗佽繘鍒跺瓧鑺傛暟缁勮浆鍗佸叚杩涘埗瀛楃涓�
- //鑾峰彇鍙傛暟鍊�
- Map<String, PlcParameter> plcParameters=plcAgreement.getPlcParameters();
- for (String key:plcParameters.keySet()){
- PlcParameter plcParameter=plcParameters.get(key);
- if("bit".equals(plcParameter.getType())){
- byte font=msgs[plcParameter.getAddressStart()];
- String[] fontBitString=String.format("%8s", Integer.toBinaryString((int)font)).replace(" ", "0").split("");
- byte[] bit=new byte[1];
- bit[0]=Byte.parseByte(fontBitString[plcParameter.getAddressLength()]);
- plcParameter.setReadByte(bit);
- }else{
- plcParameter.setReadByte(Arrays.copyOfRange(msgs,plcParameter.getAddressStart(),(plcParameter.getAddressStart()+plcParameter.getAddressLength())));
+ public boolean read(PlcAgreement plcAgreement){
+ try {
+ if (!this.socket.isConnected()){
+ // log.info("閫氳杩炴帴澶辫触:{}",this.socket.isConnected());
+ return false;
}
+ int bufSizes = 0;
+ byte[] msgs = new byte[2048];
+ byte[] content = new byte[2048];
+ //鍐欏叆璇诲彇鍦板潃
+ DataOutputStream outToServer = new DataOutputStream(this.socket.getOutputStream());
+ outToServer.write(HexConversion.stringToInt(plcAgreement.requestHead));
+ outToServer.flush();
+ //璇诲彇鍐呭
+ DataInputStream in = new DataInputStream(this.socket.getInputStream());
+ bufSizes = in.read(msgs);
+ if(bufSizes<plcAgreement.plcAddressLength+9){
+ log.info("Read byte length <1:{},content锛歿}",bufSizes,msgs);
+ return false;
+ }
+ content=Arrays.copyOfRange(msgs,9,2048);
+ //鑾峰彇鍙傛暟鍊�
+ Map<String, PlcParameter> plcParameters=plcAgreement.getPlcParameters();
+ for (String key:plcParameters.keySet()){
+ PlcParameter plcParameter=plcParameters.get(key);
+ if("bit".equals(plcParameter.getType())){
+ byte font=content[plcParameter.getAddressStart()];
+ String[] fontBitString=String.format("%8s", Integer.toBinaryString((int)font)).replace(" ", "0").split("");
+ byte[] bit=new byte[1];
+ bit[0]=Byte.parseByte(fontBitString[plcParameter.getAddressLength()]);
+ plcParameter.setReadByte(bit);
+ }else{
+ String plcAddressBegin = plcAgreement.plcAddressBegin;
+ Integer addressIndex = plcParameter.getAddressStart();
+ int address = addressIndex -HexUtil.hexToInt(plcAddressBegin)*2;
+ plcParameter.setReadByte(Arrays.copyOfRange(content,address,(address+plcParameter.getAddressLength())));
+ }
+ }
+ return true;
+ }catch (Exception e) {
+ //log.info("璇诲彇寮傚父:{}",plcAgreement);
}
+ return false;
}
//鍐欏叆鏁版嵁
public void write(PlcParameter plcParameter){
try {
if (plcParameter.getWriteValue() != null && !"".equals(plcParameter.getWriteValue())) {
- //鍐欏叆鍙戦�佹暟鎹�
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- out.write(HexConversion.stringToInt(plcParameter.getWriteValue().toString()));
+ //鍐欏叆鍙戦�佹暟鎹� 0000 0000 0009 0110 0024 0001 02 0006
+ byte []sendByte=new byte[13+plcParameter.getAddressLength()];
+ byte []sendLength=intToBytesDesc(7+plcParameter.getAddressLength(),2);
+ byte []sendAddress=intToBytesDesc(plcParameter.getAddressStart()/2,2);
+ byte []sendFontLength=intToBytesDesc(plcParameter.getAddressLength()/2,2);
+ byte []sendContent=plcParameter.getWriteByte();
+ //byte []sendContent=intToBytesDesc(Integer.parseInt(plcParameter.getWriteValue().toString()),plcParameter.getAddressLength());
+ sendByte[4]=sendLength[0];
+ sendByte[5]=sendLength[1];
+ sendByte[6]=(byte)1;
+ sendByte[7]=(byte)16;
+ sendByte[8]=sendAddress[0];
+ sendByte[9]=sendAddress[1];
+ sendByte[10]=sendFontLength[0];
+ sendByte[11]=sendFontLength[1];
+ sendByte[12]=(byte)plcParameter.getAddressLength();
+ for(int i=0;i<sendContent.length;i++){
+ sendByte[i+13]=sendContent[i];
+ }
+ log.info("sendByte:{}",sendByte);
+ DataOutputStream out = new DataOutputStream(this.socket.getOutputStream());
+ out.write(sendByte);
out.flush();
+ //log.info("sendByte:{}",sendByte);
+
}
} catch (IOException e) {
- log.info("鍐欏叆鏁版嵁寮傚父");
+ log.info("鍐欏叆鏁版嵁寮傚父:{}",plcParameter);
throw new RuntimeException(e);
}
}
+ //鏁版嵁澶勭悊
+ public void handleData(PlcParameter plcParameter){
+ //鍐欏叆鍙戦�佹暟鎹� 0000 0000 0009 0110 0024 0001 02 0006
+ try {
+ int sendLength=plcParameter.getAddressLength()+7;//鍙戦�侀暱搴�
+ byte [] addressLength=intToBytesDesc(plcParameter.getAddressLength(),1);//瀛楄妭闀垮害
+ byte [] addressLengthFont=intToBytesDesc((plcParameter.getAddressLength()/2),2);//瀛楅暱搴�
+ byte start[]=intToBytesDesc(plcParameter.getAddressStart()/2,2);//璧峰鍦板潃
+ byte content[]=intToBytesDesc(Integer.valueOf(plcParameter.getWriteValue().toString()) ,2);//璧峰鍦板潃
+ if (sendLength>14&&
+ addressLength.length>0&&
+ addressLengthFont.length>1&&
+ start.length>1&&
+ content.length>1){
+ byte head[]=new byte[]{0,0,0,(byte)sendLength,1,16,start[0],start[1],addressLengthFont[0],addressLengthFont[1],addressLength[0],content[0],content[1]};
+ this.write(head);
+ }else{
+ log.info("鍙戦�佸唴瀹逛笉绗﹀悎锛� 瀛楄妭闀垮害 {},瀛楅暱搴︼細{},璧峰鍦板潃,{},鍐呭:{}",addressLength,addressLengthFont,start,content);
+ }
+ } catch (Exception e) {
+ log.info("鏁版嵁澶勭悊寮傚父锛� 鍐呭 {}",plcParameter);
+ }
+ }
//鍐欏叆鏁版嵁
- public void write(String key,String writeValue)throws Exception{
- if (writeValue != null && !"".equals(writeValue)) {
- //鍐欏叆鍙戦�佹暟鎹�
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- out.write(HexConversion.stringToInt(writeValue));
- out.flush();
+ public void write(byte []sendByte){
+ try {
+ if (sendByte != null) {
+ //鍐欏叆鍙戦�佹暟鎹�
+ DataOutputStream out = new DataOutputStream(this.socket.getOutputStream());
+ out.write(sendByte);
+ out.flush();
+ log.info("鍐欏叆鎴愬姛锛� 鍐呭 {}",sendByte);
+ }
+ } catch (Exception e) {
+ log.info("鍐欏叆澶辫触锛� 鍐呭 {}",sendByte);
+ }
+ }
+ //鍐欏叆鏁版嵁String
+ public void writeString(String sendString,String startAddress){
+ try {
+ String result=message(sendString,startAddress);
+ if (result != null && !"".equals(result)) {
+ //鍐欏叆鏁版嵁
+ DataOutputStream out = new DataOutputStream(socket.getOutputStream());
+ out.write(HexUtil.stringToInt(result));
+ out.flush();
+ log.info("鍐欏叆鎴愬姛锛氬湴鍧� {},鍐呭 {},瀛楄妭锛歿}",startAddress,sendString,HexUtil.stringToInt(result));
+ //this.close();
+ }
+ } catch (Exception e) {
+ log.info("鍐欏叆寮傚父锛氬湴鍧� {},鍐呭 {}",startAddress,sendString);
}
}
//鍐�
@@ -108,4 +209,13 @@
public PlcAgreement getPlcAgreement(String key){
return plcAgreement.get(key);
}
+
+ public void consoleLogInfo(PlcAgreement thisPlcAgreement){
+ String logInfo=this.fileName+" ";
+ Map<String,PlcParameter> plcParameterMap=thisPlcAgreement.getPlcParameters();
+ for (String key:plcParameterMap.keySet()) {
+ logInfo+=key+":"+plcParameterMap.get(key).getValueString()+",";
+ }
+ log.info(logInfo);
+ }
}
--
Gitblit v1.8.0