modbus4j.jar和"http://download.csdn.net/detail/linxingqianglai/9330571" target="_blank">seroUt。Modbus4j.jar 实现ModbusTCP。" />

Modbus4j.jar 实现ModbusTCP

【Modbus4j.jar 实现ModbusTCP】
[cpp]view plain copy

  1. 在这章是用"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协议的详解
[cpp]view plain copy
  1. package com.example.testhzau;
  2. import java.util.Date;
  3. import com.serotonin.modbus4j.ModbusFactory;
  4. import com.serotonin.modbus4j.ModbusMaster;
  5. import com.serotonin.modbus4j.exception.ModbusInitException;
  6. import com.serotonin.modbus4j.exception.ModbusTransportException;
  7. import com.serotonin.modbus4j.ip.IpParameters;
  8. import com.serotonin.modbus4j.msg.ModbusRequest;
  9. import com.serotonin.modbus4j.msg.ModbusResponse;
  10. import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;
  11. import com.serotonin.modbus4j.msg.WriteRegistersRequest;
  12. import com.serotonin.modbus4j.msg.WriteRegistersResponse;
  13. import com.serotonin.util.queue.ByteQueue;
  14. public class ReadAWriteUtil {
  15. public static void modbusWTCP(String ip, int port, int slaveId, int start, short[] values) {
  16. ModbusFactory modbusFactory = new ModbusFactory();
  17. // 设备ModbusTCP的Ip与端口,如果不设定端口则默认为502
  18. IpParameters params = new IpParameters();
  19. params.setHost(ip);
  20. if (502 != port) {
  21. params.setPort(port);
  22. }// 设置端口,默认502
  23. ModbusMaster tcpMaster = null;
  24. // 参数1:IP和端口信息 参数2:保持连接激活
  25. tcpMaster = modbusFactory.createTcpMaster(params, true);
  26. try {
  27. tcpMaster.init();
  28. System.out.println("===============" + 1111111);
  29. } catch (ModbusInitException e) {
  30. // System.out.println("11111111111111=="+"此处出现问题了啊!");
  31. // 如果出现了通信异常信息,则保存到数据库中
  32. //CommunityExceptionRecord cer = new CommunityExceptionRecord();
  33. //cer.setDate(new Date());
  34. //cer.setIp(ip);
  35. // cer.setRemark(bgName+"出现连接异常");
  36. // batteryGroupRecordService.saveCommunityException(cer);
  37. }
  38. try {
  39. WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
  40. WriteRegistersResponse response = (WriteRegistersResponse) tcpMaster.send(request);
  41. if (response.isException())
  42. System.out.println("Exception response: message=" + response.getExceptionMessage());
  43. else
  44. System.out.println("Success");
  45. } catch (ModbusTransportException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. public static ByteQueue modbusTCP(String ip, int port, int start,int readLenth) {
  50. ModbusFactory modbusFactory = new ModbusFactory();
  51. // 设备ModbusTCP的Ip与端口,如果不设定端口则默认为502
  52. IpParameters params = new IpParameters();
  53. params.setHost(ip);
  54. if(502!=port){params.setPort(port); }//设置端口,默认502
  55. ModbusMaster tcpMaster = null;
  56. tcpMaster = modbusFactory.createTcpMaster(params, true);
  57. try {
  58. tcpMaster.init();
  59. System.out.println("==============="+1111111);
  60. } catch (ModbusInitException e) {
  61. return null;
  62. }
  63. ModbusRequest modbusRequest=null;
  64. try {
  65. modbusRequest = new ReadHoldingRegistersRequest(1, start, readLenth); //功能码03
  66. } catch (ModbusTransportException e) {
  67. e.printStackTrace();
  68. }
  69. ModbusResponse modbusResponse=null;
  70. try {
  71. modbusResponse = tcpMaster.send(modbusRequest);
  72. } catch (ModbusTransportException e) {
  73. e.printStackTrace();
  74. }
  75. ByteQueue byteQueue= new ByteQueue(12);
  76. modbusResponse.write(byteQueue);
  77. System.out.println("功能码:"+modbusRequest.getFunctionCode());
  78. System.out.println("从站地址:"+modbusRequest.getSlaveId());
  79. System.out.println("收到的响应信息大小:"+byteQueue.size());
  80. System.out.println("收到的响应信息值:"+byteQueue);
  81. return byteQueue;
  82. }
  83. }

    推荐阅读