Showing
1 changed file
with
21 additions
and
15 deletions
@@ -334,27 +334,33 @@ public class TkDeviceScriptServiceImpl | @@ -334,27 +334,33 @@ public class TkDeviceScriptServiceImpl | ||
334 | } | 334 | } |
335 | private String multData(String functionCode,List<Integer>datas){ | 335 | private String multData(String functionCode,List<Integer>datas){ |
336 | StringBuilder dataStr = new StringBuilder(); | 336 | StringBuilder dataStr = new StringBuilder(); |
337 | + int dataLength = datas.size(); | ||
337 | if(TkModBusFunctionCode.WRITE_MANY_REGISTER.getCode().equals(functionCode)){ | 338 | if(TkModBusFunctionCode.WRITE_MANY_REGISTER.getCode().equals(functionCode)){ |
338 | - dataStr.append(dataLength(datas.size()*2)); | 339 | + dataStr.append(dataLength(dataLength*2)); |
339 | for(Integer item: datas){ | 340 | for(Integer item: datas){ |
340 | dataStr.append(modelHex(item)); | 341 | dataStr.append(modelHex(item)); |
341 | } | 342 | } |
342 | }else{ | 343 | }else{ |
343 | - int filter = datas.size()%8; | ||
344 | - int num = datas.size()/8; | ||
345 | - dataStr.append(dataLength(filter == 0 ?num:num+1)); | ||
346 | - byte one = 0x00; | ||
347 | - int index =1; | ||
348 | - for(Integer item: datas){ | ||
349 | - byte byteVal=item.byteValue(); | ||
350 | - one =(byte)(one<< 1); | ||
351 | - one ^= byteVal; | ||
352 | - if(index%8==0 || datas.size() == index){ | ||
353 | - dataStr.append(ByteUtils.byteToHex(one)); | ||
354 | - one=0x00; | ||
355 | - } | ||
356 | - index++; | ||
357 | 344 | ||
345 | + int residue = dataLength%8; | ||
346 | + int num = dataLength/8; | ||
347 | + dataStr.append(dataLength(residue == 0 ?num:num+1)); | ||
348 | + | ||
349 | + Integer wordVal=0; | ||
350 | + for(int dataIndex=0,wordIndex=0;dataIndex<dataLength;dataIndex++){ | ||
351 | + Integer itemVal = datas.get(dataIndex); | ||
352 | + if(itemVal == 1){ | ||
353 | + byte one = 1; | ||
354 | + one<<=wordIndex; | ||
355 | + wordVal+=one; | ||
356 | + } | ||
357 | + if(dataIndex%8==7 || dataLength == dataIndex+1){ | ||
358 | + dataStr.append(ByteUtils.byteToHex(wordVal.byteValue())); | ||
359 | + wordVal=0; | ||
360 | + wordIndex=0; | ||
361 | + }else{ | ||
362 | + wordIndex++; | ||
363 | + } | ||
358 | } | 364 | } |
359 | } | 365 | } |
360 | return dataStr.toString(); | 366 | return dataStr.toString(); |