严智鑫
2024-10-12 acf24306aa69bb7f864e9bfa59995ccece785d30
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
package com.mes.service;
 
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;
 
@Component
@Slf4j
public class ModbusTcp {
 
    //同IP下会有多个协议地址  key=地址区   PlcAgreement为协议内容  plcAgreements为协议组
    private Map<String,PlcAgreement> plcAgreement=new LinkedHashMap<String,PlcAgreement>();
    private String Ip;
    private int Port;
 
    public Socket socket =null;//通讯
    ModbusTcp(){}
    public ModbusTcp(String Ip,int Port){
        this.Ip=Ip;
        this.Port=Port;
        try {
            socket=new Socket(Ip,Port);
        }catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //连接
    //@Scheduled(fixedDelay = 1000)
    public void a()throws Exception{
        //read();
        log.info("123");
    }
 
    //读取数据
    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 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()));
                out.flush();
            }
        } catch (IOException e) {
            log.info("写入数据异常");
            throw new RuntimeException(e);
        }
    }
    //写入数据
    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 String message(String senddate, String address) {
        String Herd = "0110" + address;
        int length = senddate.length() / 4;
        String dates = Herd + HexUtil.intTo2ByteHex(length) + HexUtil.intTo1ByteHex(length * 2) + senddate;
        int lengths = dates.length() / 2;
        String date = "00000000" + HexUtil.intTo2ByteHex(lengths) + dates;
        return date;
    }
    public PlcAgreement getPlcAgreement(String key){
        return plcAgreement.get(key);
    }
}