Modbus4j.jar 实现ModbusTCP
【Modbus4j.jar 实现ModbusTCP】
[cpp]view plain
copy
- 在这章是用"http://download.csdn.net/detail/linxingqianglai/9330571" target="_blank">modbus4j.jar和"http://download.csdn.net/detail/linxingqianglai/9330571" target="_blank">seroUtils.jar导入包实现modbus协议。下面的例子是modbus tcp的demo,下一章是讲modbus urt协议的详解
- package com.example.testhzau;
- import java.util.Date;
- import com.serotonin.modbus4j.ModbusFactory;
- import com.serotonin.modbus4j.ModbusMaster;
- import com.serotonin.modbus4j.exception.ModbusInitException;
- import com.serotonin.modbus4j.exception.ModbusTransportException;
- import com.serotonin.modbus4j.ip.IpParameters;
- import com.serotonin.modbus4j.msg.ModbusRequest;
- import com.serotonin.modbus4j.msg.ModbusResponse;
- import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;
- import com.serotonin.modbus4j.msg.WriteRegistersRequest;
- import com.serotonin.modbus4j.msg.WriteRegistersResponse;
- import com.serotonin.util.queue.ByteQueue;
- public class ReadAWriteUtil {
- public static void modbusWTCP(String ip, int port, int slaveId, int start, short[] values) {
- ModbusFactory modbusFactory = new ModbusFactory();
- // 设备ModbusTCP的Ip与端口,如果不设定端口则默认为502
- IpParameters params = new IpParameters();
- params.setHost(ip);
- if (502 != port) {
- params.setPort(port);
- }// 设置端口,默认502
- ModbusMaster tcpMaster = null;
- // 参数1:IP和端口信息 参数2:保持连接激活
- tcpMaster = modbusFactory.createTcpMaster(params, true);
- try {
- tcpMaster.init();
- System.out.println("===============" + 1111111);
- } catch (ModbusInitException e) {
- // System.out.println("11111111111111=="+"此处出现问题了啊!");
- // 如果出现了通信异常信息,则保存到数据库中
- //CommunityExceptionRecord cer = new CommunityExceptionRecord();
- //cer.setDate(new Date());
- //cer.setIp(ip);
- // cer.setRemark(bgName+"出现连接异常");
- // batteryGroupRecordService.saveCommunityException(cer);
- }
- try {
- WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
- WriteRegistersResponse response = (WriteRegistersResponse) tcpMaster.send(request);
- if (response.isException())
- System.out.println("Exception response: message=" + response.getExceptionMessage());
- else
- System.out.println("Success");
- } catch (ModbusTransportException e) {
- e.printStackTrace();
- }
- }
- public static ByteQueue modbusTCP(String ip, int port, int start,int readLenth) {
- ModbusFactory modbusFactory = new ModbusFactory();
- // 设备ModbusTCP的Ip与端口,如果不设定端口则默认为502
- IpParameters params = new IpParameters();
- params.setHost(ip);
- if(502!=port){params.setPort(port); }//设置端口,默认502
- ModbusMaster tcpMaster = null;
- tcpMaster = modbusFactory.createTcpMaster(params, true);
- try {
- tcpMaster.init();
- System.out.println("==============="+1111111);
- } catch (ModbusInitException e) {
- return null;
- }
- ModbusRequest modbusRequest=null;
- try {
- modbusRequest = new ReadHoldingRegistersRequest(1, start, readLenth); //功能码03
- } catch (ModbusTransportException e) {
- e.printStackTrace();
- }
- ModbusResponse modbusResponse=null;
- try {
- modbusResponse = tcpMaster.send(modbusRequest);
- } catch (ModbusTransportException e) {
- e.printStackTrace();
- }
- ByteQueue byteQueue= new ByteQueue(12);
- modbusResponse.write(byteQueue);
- System.out.println("功能码:"+modbusRequest.getFunctionCode());
- System.out.println("从站地址:"+modbusRequest.getSlaveId());
- System.out.println("收到的响应信息大小:"+byteQueue.size());
- System.out.println("收到的响应信息值:"+byteQueue);
- return byteQueue;
- }
- }
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM
- pytorch|使用pytorch从头实现多层LSTM