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_OUT extends ModuleIO implements IConsoleCmd {
private byte[] bufout = new byte[11];
private final int outcount = 64;
private Tag[] outputs = new Tag[outcount];
/* (non-Javadoc)
* @see promauto.jroboplc.Module#load()
*/
@Override
public boolean load() {
// create data tags
for (int i=0; i<outcount; i++)
outputs[i] = tagTable.createTag("Output" + ((i<10)?"0":"") + i, 0);
return super.load();
}
@Override
public ModuleStatus execute() {
// if (!tagEnable.getBoolean())
// return ModuleStatus.OK;
ModuleStatus status = super.execute();
if (status==ModuleStatus.OK) {
bufout[0] = 0x55;
bufout[1] = (byte)(0x20 + tagAddress.getInteger() & 0xFF);
int b=0;
int mask=1;
for (int i=0; i<64; i++) {
if (outputs[i].getInteger()>0)
b = b + mask;
mask = mask << 1;
if ((i+1)%8 == 0) {
bufout[i/8+2] = (byte)(b & 0xFF);
b = 0;
mask=1;
}
}
ModuleIO_PA.setCS(bufout, tagAddress.getInteger());
// System.out.println("\n---" + RpHyperstring.bytesToHexString(bufout));
port.writeBytesAA55(bufout);
if (status!=ModuleStatus.OK)
tagErrorCounter.set(tagErrorCounter.getInteger() + 1);
}
tagErrorCode.set(status.ordinal());
return status;
}
@Override
public ModuleStatus getStatus() {
return ModuleStatus.getStatus(tagErrorCode.getInteger());
// 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<outcount; i++) {
if (i%4==0) sb.append(" ");
if (i%8==0) sb.append(" ");
if (i%16==0) sb.append("\n");
sb.append(outputs[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" +
""
);
}
}