diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSBaratronDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSBaratronDevice.java new file mode 100644 index 00000000..43c8e6e8 --- /dev/null +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSBaratronDevice.java @@ -0,0 +1,112 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package inr.numass.readvac.devices; + +import hep.dataforge.control.measurements.Measurement; +import hep.dataforge.control.measurements.Sensor; +import hep.dataforge.control.measurements.SimpleMeasurement; +import hep.dataforge.control.ports.ComPortHandler; +import hep.dataforge.control.ports.PortHandler; +import hep.dataforge.description.ValueDef; +import hep.dataforge.exceptions.ControlException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author Alexander Nozik + */ +@ValueDef(name = "address") +@ValueDef(name = "channel") +@ValueDef(name = "port") +@ValueDef(name = "delay") +@ValueDef(name = "timeout") +public class MKSBaratronDevice extends Sensor { + + private PortHandler handler; + + public void setHandler(PortHandler handler) { + this.handler = handler; + } + + private String talk(String request) throws ControlException { + String answer = getHandler().sendAndWait(String.format("@%s%s\r", getDeviceAddress(), request), timeout()); + + Matcher match = Pattern.compile("(.*)\r").matcher(answer); + if (match.matches()) { + return match.group(1); + } else { + throw new ControlException(answer); + } + } + + private String getDeviceAddress() { + //PENDING cache this? + return meta().getString("address", "253"); + } + + private int timeout() { + return meta().getInt("timeout", 400); + } + + @Override + protected Measurement createMeasurement() { + return new BaratronMeasurement(); + } + + + public boolean isConnected() { + return getState("connection").booleanValue(); + } + + @Override + public String type() { + return meta().getString("type", "MKS baratron"); + } + + /** + * @return the handler + */ + private PortHandler getHandler() throws ControlException { + if (handler == null || !handler.isOpen()) { + String port = meta().getString("port"); + getLogger().info("Connecting to port {}", port); +// handler = PortFactory.buildPort(port); + handler = new ComPortHandler(port); + handler.setDelimeter("\r"); + handler.open(); + } + return handler; + } + + private int getChannel() { + return meta().getInt("channel", 2); + } + + private class BaratronMeasurement extends SimpleMeasurement { + + @Override + protected synchronized Double doMeasure() throws Exception { + + String answer = talk("AV" + getChannel()); + if (answer == null || answer.isEmpty()) { +// invalidateState("connection"); + this.onProgressUpdate("No connection"); + return null; + } + double res = Double.parseDouble(answer); + if (res <= 0) { + this.onProgressUpdate("Non positive"); +// invalidateState("power"); + return null; + } else { + this.onProgressUpdate("OK"); + return res; + } + } + + } +} diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSVacDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSVacDevice.java index 2a254472..49041c74 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSVacDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSVacDevice.java @@ -29,21 +29,16 @@ import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder; @ValueDef(name = "timeout") public class MKSVacDevice extends Sensor { -// private static final String DELIMETER = ";FF"; private PortHandler handler; -// public MKSVacDevice(String name, Context context, Meta meta) { -// super(name, context, meta); -// } - - public void setHandler(PortHandler handler){ + public void setHandler(PortHandler handler) { this.handler = handler; - } + } private String talk(String requestContent) throws ControlException { String answer = getHandler().sendAndWait(String.format("@%s%s;FF", getDeviceAddress(), requestContent), timeout()); - Matcher match = Pattern.compile("@253ACK(.*);FF").matcher(answer); + Matcher match = Pattern.compile("@" + getDeviceAddress() + "ACK(.*);FF").matcher(answer); if (match.matches()) { return match.group(1); } else { @@ -94,25 +89,24 @@ public class MKSVacDevice extends Sensor { } } - private Double readPressure(int channel) { - try { - String answer = talk("PR" + channel + "?"); - if (answer == null || answer.isEmpty()) { - invalidateState("connection"); - return null; - } - double res = Double.parseDouble(answer); - if (res <= 0) { - return null; - } else { - return res; - } - } catch (ControlException ex) { - invalidateState("connection"); - return null; - } - } - +// private Double readPressure(int channel) { +// try { +// String answer = talk("PR" + channel + "?"); +// if (answer == null || answer.isEmpty()) { +// invalidateState("connection"); +// return null; +// } +// double res = Double.parseDouble(answer); +// if (res <= 0) { +// return null; +// } else { +// return res; +// } +// } catch (ControlException ex) { +// invalidateState("connection"); +// return null; +// } +// } public boolean isConnected() { return getState("connection").booleanValue(); } @@ -168,11 +162,16 @@ public class MKSVacDevice extends Sensor { getLogger().info("Connecting to port {}", port); // handler = PortFactory.buildPort(port); handler = new ComPortHandler(port); + handler.setDelimeter(";FF"); handler.open(); } return handler; } + private int getChannel() { + return meta().getInt("channel", 5); + } + private class MKSVacMeasurement extends SimpleMeasurement { @Override @@ -194,8 +193,5 @@ public class MKSVacDevice extends Sensor { } } - private int getChannel() { - return meta().getInt("channel", 5); - } } }