package promauto.jroboplc.io;
import promauto.jroboplc.IConsoleCmd;
import promauto.jroboplc.ModuleStatus;
import promauto.jroboplc.SvcConsole;
import promauto.jroboplc.Tag;
public class ModuleIO_PA_DIO_IN extends ModuleIO implements IConsoleCmd {
private byte[] bufout = new byte[2];
private byte[] bufin = new byte[9];
private final int datacount = 64;
private Tag[] data = new Tag[datacount];
/* (non-Javadoc)
* @see promauto.jroboplc.Module#load()
*/
@Override
public boolean load() {
// create data tags
for (int i=0; i<datacount; i++)
data[i] = tagTable.createTag("Input"+ ((i<10)?"0":"") + i, 0);
return super.load();
}
/**
* Executes module and returns result of module execution:<p>
* @return result of execution, see ModuleIO.STATUS_xxx
*/
@Override
public ModuleStatus execute() {
// if (!tagEnable.getBoolean())
// return ModuleStatus.OK;
ModuleStatus status = super.execute();
if (status==ModuleStatus.OK) {
for (int attempt=0; attempt<=tagRetrains.getInteger(); attempt++) {
// System.out.println(".");
// send
status=ModuleStatus.NO_ANSWER;
// init data request command
bufout[0] = 0x55;
bufout[1] = (byte)(0x40 + tagAddress.getInteger() & 0xFF);
if (port.writeBytesAA55(bufout)) {
// receive
if (port.readBytesAA55(bufin)>0) {
// System.out.println("\n>>>" + attempt + ": " + RpHyperstring.bytesToHexString(bufin));
// checking control sum
status=ModuleStatus.BAD_CS;
if (ModuleIO_PA.checkCS(bufin, tagAddress.getInteger())) {
// process data
int mask = 1;
for (int i=0; i<64; i++) {
data[i].set( ((bufin[i/8] & mask) == 0)? 0: 1 );
if ((mask = mask << 1) > 0x80)
mask = 1;
}
status=ModuleStatus.OK;
break;
}
}
}
port.discard();
}
if (status!=ModuleStatus.OK)
tagErrorCounter.set(tagErrorCounter.getInteger() + 1);
}
tagErrorCode.set(status.ordinal());
return status;
}
@Override
public ModuleStatus getStatus() {
return ModuleStatus.getStatus(tagErrorCode.getInteger());
// Integer i = tagErrorCode.getInteger();
// System.out.println(" " + ModuleStatus.values()[i]);
//
// return ModuleStatus.OK;
//
// return ModuleStatus.values()[tagErrorCode.getInteger()];
}
///////////////////////////////// Console commands ///////////////////////////////////////
@Override
public boolean processConsoleCommand(SvcConsole svcConsole, int reqId,
String[] cmd) throws Exception {
boolean result = super.processConsoleCommand( svcConsole, reqId, cmd );
if (result) {
// Command - show
if (cmd[1].equals("status") || cmd[1].equals("s")) {
StringBuilder sb = new StringBuilder("Inputs:");
for (int i=0; i<datacount; i++) {
if (i%4==0) sb.append(" ");
if (i%8==0) sb.append(" ");
if (i%16==0) sb.append("\n");
sb.append(data[i].getInteger());
}
sb.append("\n");
svcConsole.write(sb.toString());
writeConsoleTags(svcConsole, "", "Input.*");
} else
// somebody else's command
result = false;
}
return result;
}
@Override
public void writeConsoleHelp(SvcConsole svcConsole) {
super.writeConsoleHelp(svcConsole);
svcConsole.write(
" " + moduleName + " s|status\n" +
""
);
}
}