use of net.wimpi.modbus.util.BitVector in project openhab1-addons by openhab.
the class SerialFacadeTest method main.
public static void main(String[] args) {
int inChar = -1;
int result = 0;
boolean finished = false;
int slaveId = 88;
String portname = null;
ModbusSerialMaster msm = null;
try {
// 1. Setup the parameters
if (args.length < 2) {
printUsage();
System.exit(1);
} else {
try {
portname = args[0];
slaveId = Integer.parseInt(args[1]);
} catch (Exception ex) {
ex.printStackTrace();
printUsage();
System.exit(1);
}
}
System.out.println(" sending test messages to slave: " + slaveId);
System.out.println("net.wimpi.modbus.debug set to: " + System.getProperty("net.wimpi.modbus.debug"));
System.out.println("Hit enter to start and <s enter> to terminate the test.");
inChar = System.in.read();
if ((inChar == 's') || (inChar == 'S')) {
System.out.println("Exiting");
System.exit(0);
}
// 2. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(38400);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("rtu");
params.setEcho(true);
if (Modbus.debug) {
System.out.println("Encoding [" + params.getEncoding() + "]");
}
// 3. Create the master facade
msm = new ModbusSerialMaster(params);
msm.connect();
do {
if (msm.writeCoil(slaveId, 4, true) == true) {
System.out.println("Set output 5 to true");
} else {
System.err.println("Error setting slave " + slaveId + " output 5");
}
BitVector coils = msm.readCoils(slaveId, 0, 8);
if (coils != null) {
System.out.print("Coils:");
for (int i = 0; i < coils.size(); i++) {
System.out.print(" " + i + ": " + coils.getBit(i));
}
System.out.println();
try {
msm.writeMultipleCoils(slaveId, 0, coils);
} catch (ModbusException ex) {
System.out.println("Error writing coils: " + result);
}
} else {
System.out.println("Outputs: null");
msm.disconnect();
System.exit(-1);
}
BitVector digInp = msm.readInputDiscretes(slaveId, 0, 8);
if (digInp != null) {
System.out.print("Digital Inputs:");
for (int i = 0; i < digInp.size(); i++) {
System.out.print(" " + i + ": " + digInp.getBit(i));
}
System.out.println();
System.out.println("Inputs: " + ModbusUtil.toHex(digInp.getBytes()));
} else {
System.out.println("Inputs: null");
msm.disconnect();
System.exit(-1);
}
InputRegister[] ai = null;
for (int i = 1000; i < 1010; i++) {
ai = msm.readInputRegisters(slaveId, i, 1);
if (ai != null) {
System.out.print("Tag " + i + ": ");
for (int n = 0; n < ai.length; n++) {
System.out.print(" " + ai[n].getValue());
}
System.out.println();
} else {
System.out.println("Tag: " + i + " null");
msm.disconnect();
System.exit(-1);
}
}
Register[] regs = null;
for (int i = 1000; i < 1005; i++) {
regs = msm.readMultipleRegisters(slaveId, i, 1);
if (regs != null) {
System.out.print("RWRegisters " + i + " length: " + regs.length);
for (int n = 0; n < regs.length; n++) {
System.out.print(" " + regs[n].getValue());
}
System.out.println();
} else {
System.out.println("RWRegisters " + i + ": null");
msm.disconnect();
System.exit(-1);
}
}
regs = msm.readMultipleRegisters(slaveId, 0, 10);
System.out.println("Registers: ");
if (regs != null) {
System.out.print("regs :");
for (int n = 0; n < regs.length; n++) {
System.out.print(" " + n + "= " + regs[n]);
}
System.out.println();
} else {
System.out.println("Registers: null");
msm.disconnect();
System.exit(-1);
}
while (System.in.available() > 0) {
inChar = System.in.read();
if ((inChar == 's') || (inChar == 'S')) {
finished = true;
}
}
} while (!finished);
} catch (Exception e) {
System.err.println("SerialFacadeTest driver: " + e);
e.printStackTrace();
}
msm.disconnect();
}
use of net.wimpi.modbus.util.BitVector in project openhab1-addons by openhab.
the class ModbusSerialMaster method readCoils.
// disconnect
/**
* Reads a given number of coil states from the slave.
* <p/>
* Note that the number of bits in the bit vector will be
* forced to the number originally requested.
*
* @param unitid (IN) the slave unit id.
* @param ref the offset of the coil to start reading from.
* @param count the number of coil states to be read.
* @return a <tt>BitVector</tt> instance holding the
* received coil states.
* @throws ModbusException if an I/O error, a slave exception or
* a transaction error occurs.
*/
public synchronized BitVector readCoils(int unitid, int ref, int count) throws ModbusException {
m_ReadCoilsRequest.setUnitID(unitid);
m_ReadCoilsRequest.setReference(ref);
m_ReadCoilsRequest.setBitCount(count);
m_Transaction.setRequest(m_ReadCoilsRequest);
m_Transaction.execute();
BitVector bv = ((ReadCoilsResponse) m_Transaction.getResponse()).getCoils();
bv.forceSize(count);
return bv;
}
use of net.wimpi.modbus.util.BitVector in project openhab1-addons by openhab.
the class ModbusUDPMaster method readCoils.
// disconnect
/**
* Reads a given number of coil states from the slave.
* <p/>
* Note that the number of bits in the bit vector will be
* forced to the number originally requested.
*
* @param ref the offset of the coil to start reading from.
* @param count the number of coil states to be read.
* @return a <tt>BitVector</tt> instance holding the
* received coil states.
* @throws ModbusException if an I/O error, a slave exception or
* a transaction error occurs.
*/
public synchronized BitVector readCoils(int ref, int count) throws ModbusException {
m_ReadCoilsRequest.setReference(ref);
m_ReadCoilsRequest.setBitCount(count);
m_Transaction.setRequest(m_ReadCoilsRequest);
m_Transaction.execute();
BitVector bv = ((ReadCoilsResponse) m_Transaction.getResponse()).getCoils();
bv.forceSize(count);
return bv;
}
use of net.wimpi.modbus.util.BitVector in project openhab1-addons by openhab.
the class ModbusUDPMaster method readInputDiscretes.
// writeMultipleCoils
/**
* Reads a given number of input discrete states from the slave.
* <p/>
* Note that the number of bits in the bit vector will be
* forced to the number originally requested.
*
* @param ref the offset of the input discrete to start reading from.
* @param count the number of input discrete states to be read.
* @return a <tt>BitVector</tt> instance holding the received input discrete
* states.
* @throws ModbusException if an I/O error, a slave exception or
* a transaction error occurs.
*/
public synchronized BitVector readInputDiscretes(int ref, int count) throws ModbusException {
m_ReadInputDiscretesRequest.setReference(ref);
m_ReadInputDiscretesRequest.setBitCount(count);
m_Transaction.setRequest(m_ReadInputDiscretesRequest);
m_Transaction.execute();
BitVector bv = ((ReadInputDiscretesResponse) m_Transaction.getResponse()).getDiscretes();
bv.forceSize(count);
return bv;
}
use of net.wimpi.modbus.util.BitVector in project openhab1-addons by openhab.
the class SerialDITest method main.
public static void main(String[] args) {
SerialConnection con = null;
ModbusSerialTransaction trans = null;
ReadInputDiscretesRequest req = null;
ReadInputDiscretesResponse res = null;
String portname = null;
int unitid = 0;
int ref = 0;
int count = 0;
int repeat = 1;
try {
// 1. Setup the parameters
if (args.length < 4) {
printUsage();
System.exit(1);
} else {
try {
portname = args[0];
unitid = Integer.parseInt(args[1]);
ref = Integer.parseInt(args[2]);
count = Integer.parseInt(args[3]);
if (args.length == 5) {
repeat = Integer.parseInt(args[4]);
}
} catch (Exception ex) {
ex.printStackTrace();
printUsage();
System.exit(1);
}
}
// 2. Set slave identifier for master response parsing
ModbusCoupler.getReference().setUnitID(unitid);
System.out.println("net.wimpi.modbus.debug set to: " + System.getProperty("net.wimpi.modbus.debug"));
// 3. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(115200);
params.setDatabits(7);
params.setParity("None");
params.setStopbits(2);
// params.setEcho(true);
if (Modbus.debug) {
System.out.println("Encoding [" + params.getEncoding() + "]");
}
// 4. Open the connection
con = new SerialConnection(params);
con.open();
// 5. Prepare a request
req = new ReadInputDiscretesRequest(ref, count);
req.setUnitID(unitid);
req.setHeadless();
if (Modbus.debug) {
System.out.println("Request: " + req.getHexMessage());
}
// 6. Prepare the transaction
trans = new ModbusSerialTransaction(con);
trans.setRequest(req);
// 7. Execute the transaction repeat times
int k = 0;
do {
trans.execute();
res = (ReadInputDiscretesResponse) trans.getResponse();
if (Modbus.debug) {
System.out.println("Response: " + res.getHexMessage());
}
BitVector inputs = res.getDiscretes();
byte[] ret = new byte[inputs.size()];
for (int i = 0; i < count; i++) {
System.out.println("Bit " + i + " = " + inputs.getBit(i));
}
k++;
} while (k < repeat);
// 8. Close the connection
con.close();
} catch (Exception ex) {
ex.printStackTrace();
// Close the connection
con.close();
}
}
Aggregations