| | |
| | | package com.modbus.data;
|
| | |
|
| | | import java.util.Date;
|
| | |
|
| | | import com.base.ComBase;
|
| | | import com.serotonin.modbus4j.BatchRead;
|
| | | import com.serotonin.modbus4j.BatchResults;
|
| | | import com.serotonin.modbus4j.ModbusMaster;
|
| | | import com.serotonin.modbus4j.code.DataType;
|
| | | import com.serotonin.modbus4j.exception.ErrorResponseException;
|
| | | import com.serotonin.modbus4j.exception.ModbusTransportException;
|
| | | import com.serotonin.modbus4j.locator.BaseLocator;
|
| | |
| | | public static Boolean readCoilStatus(int offset,MyModbusMaster master){
|
| | | // 01 Coil Status
|
| | | BaseLocator<Boolean> loc = BaseLocator.coilStatus(master.getSlaveId(), offset);
|
| | | Boolean value = null;;
|
| | | Boolean value = null;
|
| | | boolean isSuccess = true;
|
| | | try {
|
| | | value = master.getMaster().getValue(loc);
|
| | | } catch (ModbusTransportException | ErrorResponseException e) {
|
| | | master.addErrorCount();
|
| | | //e.printStackTrace();
|
| | | isSuccess = false;
|
| | | } finally {
|
| | | if(isSuccess) {
|
| | | master.clearError();
|
| | | }else {
|
| | | master.addErrorCount();
|
| | | }
|
| | | }
|
| | | return value;
|
| | | }
|
| | |
| | | // 02 Input Status
|
| | | BaseLocator<Boolean> loc = BaseLocator.inputStatus(master.getSlaveId(), offset);
|
| | | Boolean value = null;;
|
| | | boolean isSuccess = true;
|
| | | try {
|
| | | value = master.getMaster().getValue(loc);
|
| | | } catch (ModbusTransportException | ErrorResponseException e) {
|
| | | master.addErrorCount();
|
| | | //e.printStackTrace();
|
| | | isSuccess = false;
|
| | | } finally {
|
| | | if(isSuccess) {
|
| | | master.clearError();
|
| | | }else {
|
| | | master.addErrorCount();
|
| | | }
|
| | | }
|
| | | return value;
|
| | | }
|
| | |
| | | // 03 Holding Register类型数据读取
|
| | | BaseLocator<Number> loc = BaseLocator.holdingRegister(master.getSlaveId(), offset, dataType);
|
| | | Number value = null;
|
| | | boolean isSuccess = true;
|
| | | try {
|
| | | value = master.getMaster().getValue(loc);
|
| | | } catch (ModbusTransportException | ErrorResponseException e) {
|
| | | //e.printStackTrace();
|
| | | master.addErrorCount();
|
| | | isSuccess = false;
|
| | | } finally {
|
| | | if(isSuccess) {
|
| | | master.clearError();
|
| | | }else {
|
| | | master.addErrorCount();
|
| | | }
|
| | | }
|
| | | return value;
|
| | | }
|
| | |
| | | // 04 Input Registers类型数据读取
|
| | | BaseLocator<Number> loc = BaseLocator.inputRegister(master.getSlaveId(), offset, dataType);
|
| | | Number value = null;
|
| | | boolean isSuccess = true;
|
| | | try {
|
| | | value = master.getMaster().getValue(loc);
|
| | | } catch (ModbusTransportException | ErrorResponseException e) {
|
| | | //e.printStackTrace();
|
| | | master.addErrorCount();
|
| | | isSuccess = false;
|
| | | } finally {
|
| | | if(isSuccess) {
|
| | | master.clearError();
|
| | | }else {
|
| | | master.addErrorCount();
|
| | | }
|
| | | }
|
| | | return value;
|
| | | }
|
| | |
|
| | | public static BatchResults<Integer> readMutilRegisters(BatchRead<Integer> batch,MyModbusMaster master){
|
| | | batch.setContiguousRequests(false);
|
| | | boolean isSuccess = true;
|
| | | try {
|
| | | return master.getMaster().send(batch);
|
| | | } catch (ModbusTransportException | ErrorResponseException e) {
|
| | | //e.printStackTrace();
|
| | | isSuccess = false;
|
| | | } finally {
|
| | | if(isSuccess) {
|
| | | master.clearError();
|
| | | }else {
|
| | | master.addErrorCount();
|
| | | }
|
| | | }
|
| | | return null;
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 批量读取使用方法
|
| | | *
|
| | |
| | | // System.out.println(results.getValue(0));
|
| | | // System.out.println(results.getValue(1));
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 构造线圈读取节点
|
| | | * @param offset
|
| | | * @param master
|
| | | * @return
|
| | | */
|
| | | public static BaseLocator<?> createBaseLocator(int offset,MyModbusMaster master) {
|
| | | return BaseLocator.coilStatus(master.getSlaveId(), offset);
|
| | | }
|
| | | |
| | | /**
|
| | | * 构造读取节点
|
| | | * @param offset
|
| | | * @param master
|
| | | * @return
|
| | | */
|
| | | public static BaseLocator<?> createBaseLocator(int offset,int datatype,MyModbusMaster master) {
|
| | | return BaseLocator.holdingRegister(master.getSlaveId(), offset, datatype);
|
| | | }
|
| | | |
| | | /**
|
| | | * 写 [01 Coil Status(0x)]写一个 function ID = 5
|
| | | *
|
| | |
| | | master.addErrorCount();
|
| | | return false;
|
| | | } else {
|
| | | master.clearError();
|
| | | return true;
|
| | | }
|
| | | }
|
| | |
| | | master.addErrorCount();
|
| | | return false;
|
| | | } else {
|
| | | master.clearError();
|
| | | return true;
|
| | | }
|
| | |
|
| | |
| | | master.addErrorCount();
|
| | | return false;
|
| | | } else {
|
| | | master.clearError();
|
| | | return true;
|
| | | }
|
| | |
|
| | |
| | | master.addErrorCount();
|
| | | return false;
|
| | | } else {
|
| | | master.clearError();
|
| | | return true;
|
| | | }
|
| | | }
|
| | |
| | | ModbusMaster tcpMaster = master.getMaster();
|
| | | // 类型
|
| | | BaseLocator<Number> locator = BaseLocator.holdingRegister(master.getSlaveId(), offset, dataType);
|
| | | boolean isSuccess = true;
|
| | | try {
|
| | | tcpMaster.setValue(locator, value);
|
| | | } catch (ModbusTransportException | ErrorResponseException e) {
|
| | | e.printStackTrace();
|
| | | //e.printStackTrace();
|
| | | isSuccess = true;
|
| | | } finally {
|
| | | if(isSuccess) {
|
| | | master.clearError();
|
| | | }else {
|
| | | master.addErrorCount();
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | public static void main(String[] args) {
|
| | | BatchRead<Integer> batch = new BatchRead<Integer>();
|
| | | |
| | | batch.addLocator(0, BaseLocator.holdingRegister(1, 1, DataType.TWO_BYTE_INT_SIGNED));
|
| | | batch.addLocator(1, BaseLocator.coilStatus(1, 1));
|
| | | batch.setContiguousRequests(true);
|
| | | |
| | | MyModbusMaster m = new MyModbusMaster("192.168.10.221", 2);
|
| | | |
| | | ModbusMaster master = m.getMaster();
|
| | | |
| | | BatchResults<Integer> results = new BatchResults<>();
|
| | | try {
|
| | | results = master.send(batch);
|
| | | } catch (ModbusTransportException | ErrorResponseException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | System.out.println(results.getValue(0));
|
| | | //System.out.println(results.getValue(1));
|
| | | }
|
| | |
|
| | | /**
|
| | | * 读取float类型数据
|
| | | * @param obj Short类型
|
| | | * @return
|
| | | */
|
| | | public static float readShortToFloat(Object obj) {
|
| | | return (float)ComBase.changeShortToInt((Short)obj);
|
| | | }
|
| | | |
| | | /**
|
| | | * |
| | | * @param obj
|
| | | * @param signed true:有符号数 false:无符号
|
| | | * @return
|
| | | */
|
| | | public static float readShortToFloat(Object obj,boolean signed) {
|
| | | return (float)ComBase.changeShortToInt((Short)obj,signed);
|
| | | }
|
| | | |
| | | /**
|
| | | * 读取float类型数据
|
| | | * @param obj Integer类型
|
| | | * @return
|
| | | */
|
| | | public static float readIntegerToFloat(Object obj) {
|
| | | return (float)((int)obj);
|
| | | }
|
| | | |
| | | /**
|
| | | * 读取int类型数据
|
| | | * @param obj Short类型
|
| | | * @return
|
| | | */
|
| | | public static int readShortToInt(Object obj) {
|
| | | return ComBase.changeShortToInt((Short)obj);
|
| | | }
|
| | | |
| | | /**
|
| | | * 读取Boolean类型数据
|
| | | * @param obj Boolean类型
|
| | | * @return
|
| | | */
|
| | | public static int readBooleanToInt(Object obj) {
|
| | | return ((Boolean)obj?1:0);
|
| | | }
|
| | | }
|