| | |
| | | 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; |
| | |
| | | private Map<String,PlcAgreement> plcAgreement=new LinkedHashMap<String,PlcAgreement>(); |
| | | private String Ip; |
| | | private int Port; |
| | | |
| | | public Socket socket =null;//通讯 |
| | | ModbusTcp(){} |
| | | ModbusTcp(String Ip,int Port){ |
| | | 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 int getValueInt(){ |
| | | return 0; |
| | | //读取数据 |
| | | 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 double getValueDouble(){ |
| | | return 0; |
| | | } |
| | | } |
| | | //写入数据 |
| | | 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); |
| | | } |
| | | } |