| | |
| | | @Slf4j |
| | | public class PlcAgreement { |
| | | |
| | | public Socket socket =null;//通讯 |
| | | //public Socket socket =null;//通讯 |
| | | /** |
| | | * 协议参数 |
| | | */ |
| | | private List<String> parameterKeys=null; |
| | | private List<String> parameterKeys=new ArrayList<>(); |
| | | private Map<String,PlcParameter> parameters=null; |
| | | /** |
| | | * 协议路径 |
| | |
| | | public int plcAddressLength=0; |
| | | //类似序列号(4)+协议标志(4)+长度(4)+从站地址(2)+功能代码(2)+起始地址(4)+读取数量(4) "000100000006010300000032" |
| | | public String requestHead=null; |
| | | |
| | | PlcAgreement(){ |
| | | jsonFilePath = System.getProperty("user.dir") + "../../JsonFile/PlcCacheGlass.json"; |
| | | private PlcAgreement(){} |
| | | PlcAgreement(String fileName){ |
| | | //jsonFilePath = "D:/mes/JsonFile/"+fileName+".json"; |
| | | jsonFilePath = System.getProperty("user.dir") + "/JsonFile/"+fileName+".json"; |
| | | boolean initSuccess=initword(); |
| | | log.info("初始化PlcCacheGlass:"+initSuccess); |
| | | //log.info("初始化PlcCacheGlass:"+initSuccess); |
| | | } |
| | | //初始化word |
| | | public boolean initword() { |
| | |
| | | fileReader.close(); |
| | | |
| | | JSONObject jsonFile = new JSONObject(content.toString()); |
| | | |
| | | //log.info("读取内容:{}",jsonFile); |
| | | JSONArray jsonArray = jsonFile.getJSONArray("parameterInfo"); |
| | | |
| | | this.plcAddressBegin=jsonFile.getStr("plcAddressBegin");//设置起始位地址 |
| | | this.plcAddressLength=Integer.valueOf(jsonFile.getStr("plcAddressLength"));//设置地址长度 |
| | | this.requestHead=jsonFile.getStr("requestHead");//设置请求头部 |
| | | |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject parameterObj = jsonArray.getJSONObject(i); |
| | | String code = parameterObj.getStr("code"); |
| | | String code = parameterObj.getStr("codeId"); |
| | | PlcParameter plcParameter = new PlcParameter( |
| | | code, |
| | | Integer.valueOf(parameterObj.getStr("addressIndex")), |
| | | Integer.valueOf(parameterObj.getStr("addressLength")),""); //参数实例 |
| | | Integer.valueOf(parameterObj.getStr("addressLength")), |
| | | parameterObj.getStr("type")); //参数实例 |
| | | parameterKeys.add(code); |
| | | parameters.put(code,plcParameter); |
| | | } |
| | |
| | | } |
| | | 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 PlcParameter getPlcParameter(int index){ |
| | | String key=parameterKeys.get(index); |
| | | if(key!=null&& !key.isEmpty()){ |
| | | return this.getPlcParameter(key); |
| | | } |
| | | } |
| | | //写入数据 |
| | | 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 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; |