modbus4j例子packagecom.modbus;importjava.nio.ByteBuffer;importcom.serotonin.modbus4j.ModbusFactory;importcom.serotonin.modbus4j.ModbusMaster;importcom.serotonin.modbus4j.exception.ModbusInitException;importcom.serotonin.modbus4j.exception.ModbusTransportException;importcom.serotonin.modbus4j.ip.IpParameters;importcom.serotonin.modbus4j.msg.ModbusRequest;importcom.serotonin.modbus4j.msg.ModbusResponse;importcom.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;importcom.serotonin.modbus4j.msg.WriteRegistersRequest;importcom.serotonin.modbus4j.msg.WriteRegistersResponse;importcom.serotonin.util.queue.ByteQueue;/***modbus处理类*@author?**/publicclassModbusHandler{staticModbusFactorymodbusFactory;static{if(modbusFactory==null){modbusFactory=newModbusFactory();}}/***得到WriteRegistersRequest**@paramip*@paramport*@paramslaveId*@paramstart*@paramvalues*/publicstaticWriteRegistersRequestgetWriteRegistersRequest(intslaveId,intstart,short[]values){WriteRegistersRequestrequest=null;try{request=newWriteRegistersRequest(slaveId,start,values);}catch(ModbusTransportExceptione){e.printStackTrace();}returnrequest;}/***得到WriteRegistersRequest**@paramip*@paramport*@paramslaveId*@paramstart*@paramvalues*/publicstaticWriteRegistersResponsegetWriteRegistersResponse(ModbusMastertcpMaster,WriteRegistersRequestrequest){WriteRegistersResponseresponse=null;try{response=(WriteRegistersResponse)tcpMaster.send(request);}catch(ModbusTransportExceptione){e.printStackTrace();}returnresponse;}/***写入**@paramip*@paramport*@paramslaveId*@paramstart*@paramvalues*/publicstaticintmodbusWTCP(Stringip,intport,intslaveId,intstart,short[]values){ModbusMastertcpMaster=getTcpMaster(ip,port,slaveId);if(tcpMaster==null){System.out.println(tcpMasterisnull);return0;}tcpMaster=initTcpMaster(tcpMaster);WriteRegistersRequestrequest=getWriteRegistersRequest(slaveId,start,values);WriteRegistersResponseresponse=getWriteRegistersResponse(tcpMaster,request);if(response.isException()){return0;}else{return1;}}/***初始化?tcpMaster**@paramtcpMaster*@return*/publicstaticModbusMasterinitTcpMaster(ModbusMastertcpMaster){if(tcpMaster==null)returnnull;try{tcpMaster.init();returntcpMaster;}catch(ModbusInitExceptione){returnnull;}}/***得到ModbusRequest**@paramsalveId*@paramstart*@paramreadLenth*@paramtcpMaster*/publicstaticModbusRequestgetRequest(intsalveId,intstart,intreadLenth,ModbusMastertcpMaster){ModbusRequestmodbusRequest=null;try{modbusRequest=newReadHoldingRegistersRequest(salveId,start,readLenth);returnmodbusRequest;}catch(ModbusTransportExceptione){e.printStackTrace();returnnull;}}/***得到ModbusResponse**@paramsalveId*@paramstart*@paramreadLenth*@paramtcpMaster*/publicstaticModbusResponsegetModbusResponse(ModbusMastertcpMaster,ModbusRequestrequest){ModbusResponsemodbusResponse=null;try{modbusResponse=tcpMaster.send(request);returnmodbusResponse;}catch(ModbusTransportExceptione){e.printStackTrace();returnnull;}}/***获取设备数据*@paramip*信息机地址ip*@paramport*端口默认端口502*@paramsalveId*从站地址*@paramstart*数据报文的起始位置*@paramreadLenth*读取的长度*@return结果*/publicstaticByteQueuemodbusRTCP(Stringip,intport,intsalveId,intstart,intreadLenth){ModbusMastertcpMaster=getTcpMaster(ip,port,salveId);//得到tcpMasterif(tcpMaster==null){System.out.println(tcpMasterisnull);returnnull;}returnmodbusRTCP0(ip,port,salveId,start,readLenth,tcpMaster);}/***获取tcpmaster**@paramip*@paramport*@paramsalveId*/publicstaticModbusMastergetTcpMaster(Stringip,intport,intsalveId){IpParametersparams=newIpParameters();params.setHost(ip);//设置ipif(port==0)params.setPort(502);//设置端口,默认为502elseparams.setPort(port);ModbusMastertcpMaster=modbusFactory.createTcpMaster(params,true);//获取ModbusMaster对象returntcpMaster;}/***modbus读取**@paramip*@paramport*@paramsalveId*@paramstart*@paramreadLenth*@paramtcpMaster*@return*/publicstaticByteQueuemodbusRTCP0(Stringip,intport,intsalveId,intstart,intreadLenth,ModbusMastertcpMaster){if(tcpMaster==null){System.out.println(tcpMasterisnull);returnnull;}tcpMaster=initTcpMaster(tcpMaster);//初始化tcpmasterif(tcpMaster==null){System.out.println(tcpMasterisnull);returnnull;}ModbusRequestmodbusRequest=getRequest(salveId,start,readLenth,tcpMaster);//得到requst对象if(modbusRequest==null){System.out.println(requestisnull);returnnull;}ModbusResponseresponse=getModbusResponse(tcpMaster,modbusRequest);//发送请求,得到ResponseByteQueuebyteQueue=newByteQueue(12);response.write(byteQueue);System.out.println(功能+modbusRequest.getFunctionCode());System.out.println(从站地址:+modbusRequest.getSlaveId());System.out.println(收到的响应信息大小+byteQueue.size());System.out.println(收到的响应信息小:+byteQueue);returnbyteQueue;}/***Convertbyte[]tohex*string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串*@paramsrcbyte[]data*@returnhexstring*/publicstaticStringbytesToHexString(byte[]src){StringBuilderstringBuilder=newStringBuilder();if(src==null||src.lengthreturnnull;}for(inti=0;iintv=src[i]&0xFF;Stringhv=Integer.toHexString(v);if(hv.length()stringBuilder.append(0);}stringBuilder.append(hv);}returnstringBuilder.toString();}/*****************************************