| | |
| | | @Slf4j |
| | | public class PlcAgreement { |
| | | |
| | | public Socket socket =null;//通讯 |
| | | //public Socket socket =null;//通讯 |
| | | /** |
| | | * 协议参数 |
| | | */ |
| | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | //读取数据 |
| | | public void read()throws Exception{ |
| | | int bufSizes = 0; |
| | | byte[] msgs = new byte[2048]; |
| | | //写入读取地址 |
| | | DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream()); |
| | | outToServer.write(HexConversion.stringToInt(this.requestHead)); |
| | | outToServer.flush(); |
| | | //读取内容 |
| | | DataInputStream in = new DataInputStream(socket.getInputStream()); |
| | | bufSizes = in.read(msgs); |
| | | String message = HexConversion.byteToHexString(bufSizes, msgs);//十进制字节数组转十六进制字符串 |
| | | //获取参数值 |
| | | for (String key:parameters.keySet()){ |
| | | parameters.get(key).setReadValue(message); |
| | | //获取此地址全部参数 |
| | | public Map<String,PlcParameter> getPlcParameters(){ |
| | | return parameters; |
| | | } |
| | | //通过参数名称获取 |
| | | public PlcParameter getPlcParameter(String name){ |
| | | return parameters.get(name); |
| | | } |
| | | //写入数据 |
| | | public void write(String key,String writeValue)throws Exception{ |
| | | parameters.get(key); |
| | | if (writeValue != null && !"".equals(writeValue)) { |
| | | //写入发送数据 |
| | | DataOutputStream out = new DataOutputStream(socket.getOutputStream()); |
| | | out.write(HexConversion.stringToInt(writeValue)); |
| | | out.flush(); |
| | | //通过参数序号获取 |
| | | public PlcParameter getPlcParameter(int index){ |
| | | String key=parameterKeys.get(index); |
| | | if(key!=null&& !key.isEmpty()){ |
| | | return this.getPlcParameter(key); |
| | | } |
| | | } |
| | | //写 |
| | | 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 String getValueString(String key){ |
| | | return parameters.get(key).toString(); |
| | | log.info("无效下标:{},下标范围:0-{}",index,parameterKeys.size()); |
| | | return null; |
| | | } |
| | | public int getValueInt(){ |
| | | return 0; |