use of net.wimpi.modbus.procimg.SimpleRegister in project openhab1-addons by openhab.
the class ModbusSlave method setRegister.
/**
* Performs physical write to device when slave type is "holding" using Modbus FC06 function
*
* @param command command received from OpenHAB
* @param config
* @throws ModbusConnectionException when connection cannot be established
* @throws ModbusException ModbusIOException on IO errors, ModbusSlaveException with protocol level exceptions
* @throws ModbusUnexpectedTransactionIdException when response transaction id does not match the request
*/
protected void setRegister(Command command, ModbusBindingConfig config) throws ModbusConnectionException, ModbusException, ModbusUnexpectedTransactionIdException {
int readIndex = config.readIndex;
int writeRegister = getStart() + config.writeIndex;
Register newValue;
if (command instanceof IncreaseDecreaseType) {
newValue = readCachedRegisterValue(readIndex);
if (newValue == null) {
logger.warn("Not polled value for item {}. Cannot process command {}", config.getItemName(), command);
return;
}
if (command.equals(IncreaseDecreaseType.INCREASE)) {
newValue.setValue(newValue.getValue() + 1);
} else if (command.equals(IncreaseDecreaseType.DECREASE)) {
newValue.setValue(newValue.getValue() - 1);
}
} else if (command instanceof UpDownType) {
newValue = readCachedRegisterValue(readIndex);
if (newValue == null) {
logger.warn("Not polled value for item {}. Cannot process command {}", config.getItemName(), command);
return;
}
if (command.equals(UpDownType.UP)) {
newValue.setValue(newValue.getValue() + 1);
} else if (command.equals(UpDownType.DOWN)) {
newValue.setValue(newValue.getValue() - 1);
}
} else if (command instanceof DecimalType) {
newValue = new SimpleRegister();
newValue.setValue(((DecimalType) command).intValue());
} else if (command instanceof OnOffType) {
newValue = new SimpleRegister();
if (command.equals(OnOffType.ON)) {
newValue.setValue(1);
} else if (command.equals(OnOffType.OFF)) {
newValue.setValue(0);
}
} else if (command instanceof OpenClosedType) {
newValue = new SimpleRegister();
if (command.equals(OpenClosedType.OPEN)) {
newValue.setValue(1);
} else if (command.equals(OpenClosedType.CLOSED)) {
newValue.setValue(0);
}
} else {
logger.warn("Item {} received unsupported command: {}. Not setting register.", config.getItemName(), command);
return;
}
ModbusRequest request = null;
if (writeMultipleRegisters) {
Register[] regs = new Register[1];
regs[0] = newValue;
request = new WriteMultipleRegistersRequest(writeRegister, regs);
} else {
request = new WriteSingleRegisterRequest(writeRegister, newValue);
}
request.setUnitID(getId());
logger.debug("ModbusSlave ({}): FC{} ref={} value={}", name, request.getFunctionCode(), writeRegister, newValue.getValue());
executeWriteRequest(request);
}
use of net.wimpi.modbus.procimg.SimpleRegister in project openhab1-addons by openhab.
the class SerialSlaveTest method main.
public static void main(String[] args) {
ModbusSerialListener listener = null;
SimpleProcessImage spi = new SimpleProcessImage();
String portname = args[0];
if (Modbus.debug) {
System.out.println("jModbus ModbusSerial Slave");
}
try {
// 1. Prepare a process image
spi = new SimpleProcessImage();
spi.addDigitalOut(new SimpleDigitalOut(true));
spi.addDigitalOut(new SimpleDigitalOut(false));
spi.addDigitalIn(new SimpleDigitalIn(false));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(false));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addRegister(new SimpleRegister(251));
spi.addInputRegister(new SimpleInputRegister(45));
// 2. Create the coupler and set the slave identity
ModbusCoupler.getReference().setProcessImage(spi);
ModbusCoupler.getReference().setMaster(false);
ModbusCoupler.getReference().setUnitID(2);
// 3. Set up serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(115200);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("ascii");
params.setEcho(false);
params.setReceiveTimeoutMillis(100);
if (Modbus.debug) {
System.out.println("Encoding [" + params.getEncoding() + "]");
}
// 4. Set up serial listener
listener = new ModbusSerialListener(params);
listener.setListening(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of net.wimpi.modbus.procimg.SimpleRegister in project openhab1-addons by openhab.
the class TCPSlaveTest method main.
public static void main(String[] args) {
ModbusTCPListener listener = null;
SimpleProcessImage spi = null;
int port = Modbus.DEFAULT_PORT;
try {
if (args != null && args.length == 1) {
port = Integer.parseInt(args[0]);
}
System.out.println("jModbus Modbus Slave (Server)");
// 1. prepare a process image
spi = new SimpleProcessImage();
spi.addDigitalOut(new SimpleDigitalOut(true));
spi.addDigitalOut(new SimpleDigitalOut(true));
spi.addDigitalIn(new SimpleDigitalIn(false));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(false));
spi.addDigitalIn(new SimpleDigitalIn(true));
// allow checking LSB/MSB order
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addRegister(new SimpleRegister(251));
spi.addInputRegister(new SimpleInputRegister(45));
// 2. create the coupler holding the image
ModbusCoupler.getReference().setProcessImage(spi);
ModbusCoupler.getReference().setMaster(false);
ModbusCoupler.getReference().setUnitID(15);
// 3. create a listener with 3 threads in pool
if (Modbus.debug) {
System.out.println("Listening...");
}
listener = new ModbusTCPListener(3);
listener.setPort(port);
listener.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of net.wimpi.modbus.procimg.SimpleRegister in project openhab1-addons by openhab.
the class UDPSlaveTest method main.
public static void main(String[] args) {
ModbusUDPListener listener = null;
SimpleProcessImage spi = null;
int port = Modbus.DEFAULT_PORT;
try {
if (args != null && args.length == 1) {
port = Integer.parseInt(args[0]);
}
System.out.println("jModbus Modbus/UDP Slave v0.1");
// 1. Prepare a process image
spi = new SimpleProcessImage();
spi.addDigitalOut(new SimpleDigitalOut(true));
spi.addDigitalIn(new SimpleDigitalIn(false));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(false));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addRegister(new SimpleRegister(251));
spi.addInputRegister(new SimpleInputRegister(45));
ModbusCoupler.getReference().setProcessImage(spi);
ModbusCoupler.getReference().setMaster(false);
ModbusCoupler.getReference().setUnitID(15);
// 2. Setup and start listener
listener = new ModbusUDPListener();
listener.setPort(port);
listener.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of net.wimpi.modbus.procimg.SimpleRegister in project openhab1-addons by openhab.
the class AIAOTest method main.
public static void main(String[] args) {
InetAddress addr = null;
TCPMasterConnection con = null;
ModbusRequest ai_req = null;
WriteSingleRegisterRequest ao_req = null;
ModbusTransaction ai_trans = null;
ModbusTransaction ao_trans = null;
int ai_ref = 0;
int ao_ref = 0;
int port = Modbus.DEFAULT_PORT;
try {
// 1. Setup the parameters
if (args.length < 3) {
printUsage();
System.exit(1);
} else {
try {
String astr = args[0];
int idx = astr.indexOf(':');
if (idx > 0) {
port = Integer.parseInt(astr.substring(idx + 1));
astr = astr.substring(0, idx);
}
addr = InetAddress.getByName(astr);
ai_ref = Integer.parseInt(args[1]);
ao_ref = Integer.parseInt(args[2]);
} catch (Exception ex) {
ex.printStackTrace();
printUsage();
System.exit(1);
}
}
// 2. Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();
if (Modbus.debug) {
System.out.println("Connected to " + addr.toString() + ":" + con.getPort());
}
// 3. Prepare the requests
ai_req = new ReadInputRegistersRequest(ai_ref, 1);
ao_req = new WriteSingleRegisterRequest();
ao_req.setReference(ao_ref);
ai_req.setUnitID(0);
ao_req.setUnitID(0);
// 4. Prepare the transactions
ai_trans = new ModbusTCPTransaction(con);
ai_trans.setRequest(ai_req);
ao_trans = new ModbusTCPTransaction(con);
ao_trans.setRequest(ao_req);
// 5. Prepare holders to update only on change
SimpleRegister new_out = new SimpleRegister(0);
ao_req.setRegister(new_out);
int last_out = Integer.MIN_VALUE;
// 5. Execute the transaction repeatedly
do {
ai_trans.execute();
int new_in = ((ReadInputRegistersResponse) ai_trans.getResponse()).getRegister(0).getValue();
// write only if differ
if (new_in != last_out) {
// update register
new_out.setValue(new_in);
ao_trans.execute();
last_out = new_in;
if (Modbus.debug) {
System.out.println("Updated Register with value from Input Register.");
}
}
} while (true);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 6. Close the connection
con.close();
}
}
Aggregations