[no commit message]

This commit is contained in:
Alexander Nozik 2016-03-29 19:11:12 +03:00
parent 63ca107233
commit 2b9132b116
2 changed files with 138 additions and 30 deletions

View File

@ -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<Double> {
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<Double> 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<Double> {
@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;
}
}
}
}

View File

@ -29,21 +29,16 @@ import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder;
@ValueDef(name = "timeout") @ValueDef(name = "timeout")
public class MKSVacDevice extends Sensor<Double> { public class MKSVacDevice extends Sensor<Double> {
// private static final String DELIMETER = ";FF";
private PortHandler handler; private PortHandler handler;
// public MKSVacDevice(String name, Context context, Meta meta) { public void setHandler(PortHandler handler) {
// super(name, context, meta);
// }
public void setHandler(PortHandler handler){
this.handler = handler; this.handler = handler;
} }
private String talk(String requestContent) throws ControlException { private String talk(String requestContent) throws ControlException {
String answer = getHandler().sendAndWait(String.format("@%s%s;FF", getDeviceAddress(), requestContent), timeout()); 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()) { if (match.matches()) {
return match.group(1); return match.group(1);
} else { } else {
@ -94,25 +89,24 @@ public class MKSVacDevice extends Sensor<Double> {
} }
} }
private Double readPressure(int channel) { // private Double readPressure(int channel) {
try { // try {
String answer = talk("PR" + channel + "?"); // String answer = talk("PR" + channel + "?");
if (answer == null || answer.isEmpty()) { // if (answer == null || answer.isEmpty()) {
invalidateState("connection"); // invalidateState("connection");
return null; // return null;
} // }
double res = Double.parseDouble(answer); // double res = Double.parseDouble(answer);
if (res <= 0) { // if (res <= 0) {
return null; // return null;
} else { // } else {
return res; // return res;
} // }
} catch (ControlException ex) { // } catch (ControlException ex) {
invalidateState("connection"); // invalidateState("connection");
return null; // return null;
} // }
} // }
public boolean isConnected() { public boolean isConnected() {
return getState("connection").booleanValue(); return getState("connection").booleanValue();
} }
@ -168,11 +162,16 @@ public class MKSVacDevice extends Sensor<Double> {
getLogger().info("Connecting to port {}", port); getLogger().info("Connecting to port {}", port);
// handler = PortFactory.buildPort(port); // handler = PortFactory.buildPort(port);
handler = new ComPortHandler(port); handler = new ComPortHandler(port);
handler.setDelimeter(";FF");
handler.open(); handler.open();
} }
return handler; return handler;
} }
private int getChannel() {
return meta().getInt("channel", 5);
}
private class MKSVacMeasurement extends SimpleMeasurement<Double> { private class MKSVacMeasurement extends SimpleMeasurement<Double> {
@Override @Override
@ -194,8 +193,5 @@ public class MKSVacDevice extends Sensor<Double> {
} }
} }
private int getChannel() {
return meta().getInt("channel", 5);
}
} }
} }