[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")
public class MKSVacDevice extends Sensor<Double> {
// 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<Double> {
}
}
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<Double> {
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<Double> {
@Override
@ -194,8 +193,5 @@ public class MKSVacDevice extends Sensor<Double> {
}
}
private int getChannel() {
return meta().getInt("channel", 5);
}
}
}