use of javafish.clients.opc.variant.Variant in project TeachingInSimulation by ScOrPiOzzy.
the class FX3UPLCLogic method onReceivedLocal.
@Override
protected void onReceivedLocal(Terminal terminal) {
// plc工作电压
if (term_L == terminal || term_N == terminal || term_GND == terminal) {
MesureResult volt = R.matchRequiredVolt(Voltage.IS_AC, term_L, term_N, 220, 2);
boolean tmp = volt != null;
R r = R.getR(SIGNAL_EVN);
if (!workable && tmp) {
// 开始工作
workable = true;
// 输出24V电压
if (r == null) {
r = R.create(SIGNAL_EVN, Voltage.IS_DC, term_24V, term_0V, 24);
}
r.shareVoltage();
if (client != null) {
Pool.getOPCPool().submit(new Runnable() {
@Override
public void run() {
try {
client.connect();
} catch (ConnectivityException e) {
log.error("连接OPC server 失败!!" + e.getMessage(), e);
}
}
});
}
} else if (workable && !tmp) {
// 停止工作
workable = false;
if (r != null) {
r.shutPowerDown();
}
if (client != null) {
Pool.getOPCPool().submit(new Runnable() {
@Override
public void run() {
client.disconnect();
log.info("OPC 客户端断开连接");
}
});
}
}
if (client != null) {
client.setWorkable(workable);
}
return;
} else if (terminal == rda || terminal == rdb) {
}
if (!workable || client == null) {
return;
}
// 能有信号的连接头 以X开头
String prex = String.valueOf(terminal.getPO().getId().charAt(0)).toUpperCase();
if ("X".equals(prex)) {
MesureResult volt = R.matchRequiredVolt(Voltage.IS_DC, terminal, term_SS, 24, 2);
if (volt == null) {
volt = R.matchRequiredVolt(Voltage.IS_DC, term_SS, terminal, 24, 2);
}
// 满足电压的输入,将对应的X信号写入PLC
// FIXME 格式化信号格式
String signal = Signal.formatSignal(terminal.getName(), 3);
inputSignalMap.put(signal, volt != null);
assist.dealWithHSXSignal(signal, volt);
client.updateItemValue(deviceName + ".X." + signal, new Variant(volt != null));
}
}
use of javafish.clients.opc.variant.Variant in project TeachingInSimulation by ScOrPiOzzy.
the class FX3UPLCLogicAssist method dealWithHSXSignal.
/**
* 处理高速输入信号 <br>
* dealWith High-Speed input signal
* @param signalX
* @param i
*/
public void dealWithHSXSignal(String signalX, final MesureResult result) {
if (!hs.containsKey(signalX)) {
return;
}
hs.put(signalX, result != null);
if ("X000".equals(signalX) || "X001".equals(signalX)) {
boolean tmp = hs.get("X000").booleanValue() && hs.get("X001").booleanValue();
if (tmp && !counting) {
// System.out.println("FX3UPLCLogicAssist.dealWithHSXSignal(counting = true)");
counting = true;
// FIXME 渣代码
Pool.getCachedPool("计数器").execute(new Runnable() {
private char[] currentPulseCount;
public void run() {
pulsePerMillis = MathUtil.parseFloat(result.getData(Encoder.KEY_PulsePerMillis), pulsePerMillis);
dir = MathUtil.parseInt(result.getData(Encoder.KEY_PulseDir), dir);
float higher = 0;
float lower = 0;
try {
VariantList currentPulseCount = logic.getOPCClient().getItemValue(logic.getDeivceName() + ".C.CN251").getArray();
lower = currentPulseCount.get(0).getWord() & 0xFFFF;
higher = currentPulseCount.get(1).getWord() & 0xFFFF;
// System.out.println("FX3UPLCLogicAssist.dealWithHSXSignal(...).new Runnable() {...}.run()" + currentPulseCount);
} catch (VariantTypeException e) {
log.error(e.getMessage(), e);
}
while (counting && logic.isWorkable()) {
lower += pulsePerMillis * dir;
// System.err.println(currentPulseCount);
if (lower >= 65535) {
lower = 0;
higher += 1;
} else if (lower <= 0) {
lower = 65534;
higher -= 1;
if (higher < 0) {
higher = 65534;
}
}
VariantList variantList = new VariantList(Variant.VT_ARRAY);
variantList.add(new Variant((short) lower & 0xFFFF));
variantList.add(new Variant((short) higher & 0xFFFF));
Variant variant = new Variant(variantList);
logic.getOPCClient().updateItemValue(logic.getDeivceName() + ".C.CN251", variant);
// }
try {
Thread.sleep(1);
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
}
}
counting = false;
}
});
} else if (!tmp && counting) {
// System.out.println("FX3UPLCLogicAssist.dealWithHSXSignal(counting = false)");
counting = false;
}
}
}
use of javafish.clients.opc.variant.Variant in project TeachingInSimulation by ScOrPiOzzy.
the class FX3UPLCLogicAssist method ddrva.
private void ddrva(String addr, Variant variant) {
if (variant.getBoolean()) {
String value = entry.get(addr);
List<String> arr = StringUtil.split(value, ',');
final String outputAddr = arr.get(3);
final Map<String, String> data = new HashMap<String, String>();
data.put("pulseAmount", arr.get(1).substring(1));
data.put("pulseFrequency", arr.get(2).substring(1));
dealWithYSignal(outputAddr, new Variant(true), data);
String str_pulseAmount = data.get("pulseAmount");
String str_pulseFrequency = data.get("pulseFrequency");
// 频率
final Integer pulseFrequency = MathUtil.parseInt(str_pulseFrequency);
final Integer pulseAmount = MathUtil.parseInt(str_pulseAmount);
int[] d = getPulseSome(pulseFrequency);
int period = d[0];
final int pulsePeriod = d[1];
// System.out.println("FX3UPLCLogicAssist.servo() 每" + period + "毫秒发送" + pulsePeriod + "个脉冲信号");
Integer current = dest.get(outputAddr);
// true表示正,false表示负,
final boolean dir = current < pulseAmount;
// System.out.println("FX3UPLCLogicAssist.ddrva(当前" + current + ") 目标" + pulseAmount + ";dir=" + dir);
final String dirAddr = Signal.formatSignal(arr.get(4), 4);
// Map<String, String> dirData = new HashMap<String, String>();
// dirData.put("dir", dir);
dealWithYSignal(dirAddr, new Variant(dir));
// logic.getOPCClient().updateItemValue(logic.getDeivceName() + ".Y." + dirAddr, new Variant(dir));
final Timer timer = new Timer("DDRVA");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (dir) {
dest.put(outputAddr, dest.get(outputAddr) + pulsePeriod);
if (dest.get(outputAddr) >= pulseAmount) {
dest.put(outputAddr, pulseAmount);
logic.getOPCClient().updateItemValue(logic.getDeivceName() + ".M.M8029", new Variant(true));
// 数完脉冲后 断开对应Y端口
dealWithYSignal(outputAddr, new Variant(false), null);
timer.cancel();
}
} else {
dest.put(outputAddr, dest.get(outputAddr) - pulsePeriod);
if (dest.get(outputAddr) <= pulseAmount) {
dest.put(outputAddr, pulseAmount);
logic.getOPCClient().updateItemValue(logic.getDeivceName() + ".M.M8029", new Variant(true));
// 数完脉冲后 断开对应Y端口
dealWithYSignal(outputAddr, new Variant(false), null);
timer.cancel();
}
}
// System.out.println("FX3UPLCLogicAssist.servo(...).new TimerTask() {...}.run()" + count);
count(outputAddr);
}
}, 0, period);
} else {
Variant m570 = logic.getItemValue(logic.getDeivceName() + ".M.M570");
Variant m571 = logic.getItemValue(logic.getDeivceName() + ".M.M571");
Variant m572 = logic.getItemValue(logic.getDeivceName() + ".M.M572");
Variant m573 = logic.getItemValue(logic.getDeivceName() + ".M.M573");
if (!(m570.getBoolean() || m571.getBoolean() || m572.getBoolean() || m573.getBoolean())) {
logic.getOPCClient().updateItemValue(logic.getDeivceName() + ".M.M8029", new Variant(false));
}
}
}
use of javafish.clients.opc.variant.Variant in project TeachingInSimulation by ScOrPiOzzy.
the class FX3UPLCLogicAssist method M0510.
/**
* HFA 方向
* @param variant
*/
public void M0510(Variant variant) {
if (variant.getBoolean()) {
Variant value = logic.getItemValue(logic.getDeivceName() + ".D.D110");
int vt_int = value.getWord() & 0xFFFF;
Map<String, String> data = new HashMap<String, String>();
data.put("HFA", String.valueOf(vt_int));
logic.s_rs485(data);
} else {
Map<String, String> data = new HashMap<String, String>();
data.put("HFA", "0");
logic.s_rs485(data);
}
}
use of javafish.clients.opc.variant.Variant in project TeachingInSimulation by ScOrPiOzzy.
the class FX3UPLCLogicAssist method count.
private void count(final String outputAddr) {
int count = dest.get(outputAddr);
int higher = count / 65536;
int lower = count % 65536;
VariantList variantList = new VariantList(Variant.VT_ARRAY);
variantList.add(new Variant((short) lower & 0xFFFF));
variantList.add(new Variant((short) higher & 0xFFFF));
Variant new_variant = new Variant(variantList);
if ("Y0".equals(outputAddr)) {
logic.getOPCClient().updateItemValue(logic.getDeivceName() + ".D.D8340", new_variant);
} else if ("Y1".equals(outputAddr)) {
logic.getOPCClient().updateItemValue(logic.getDeivceName() + ".D.D8350", new_variant);
}
}
Aggregations