org.hwo.pulscounter/src/org/hwo/pulscounter/NewPulsCounterDevice.java

250 lines
6.0 KiB
Java

package org.hwo.pulscounter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import org.hwo.bitfields.BitField;
import org.hwo.io.SerialPort;
import org.hwo.io.SerialPortExeption;
import org.hwo.io.NewSerialPort.NewSerialPort;
import org.hwo.servicelink.ServiceLink;
import org.hwo.servicelink.ServiceLinkException;
import org.hwo.servicelink.ServiceLinkRequestFailedException;
public class NewPulsCounterDevice implements IPulsCounter {
private ServiceLink serviceLink;
private Properties chprop;
public NewPulsCounterDevice()
{
this.chprop = new Properties();
File pf = new File("chnames.prop");
try {
chprop.load(new FileInputStream(pf));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String defaultPort = chprop.getProperty("io.port", "COM1");
System.err.println("Default Port: " + defaultPort);
setPhysicalInterfaceName(defaultPort);
}
public void close(){
if (this.serviceLink != null)
this.serviceLink.close();
}
private void saveProps(){
File pf = new File("chnames.prop");
try {
chprop.store(new FileOutputStream(pf), "");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void checkOpen(){
if (!serviceLink.isOpen())
try {
serviceLink.open();
serviceLink.getSerialPort().setTimeOut(250);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ServiceLinkException e) {
e.printStackTrace();
}
}
public String[] getDeviceName()
{
return new String[]{"PulsCounter Board","0.1","alpha"};
}
public Date getDeviceTime()
{
return new Date();
}
public NewSerialPort getSerialPort() {
return serviceLink.getSerialPort();
}
public void setSerialPort(NewSerialPort serialPort) throws ServiceLinkException {
if (this.serviceLink == null)
this.serviceLink = new ServiceLink(serialPort);
if (this.serviceLink.isOpen())
this.serviceLink.close();
this.serviceLink.setSerialPort(serialPort);;
this.serviceLink.open();
this.serviceLink.getSerialPort().setTimeOut(500);
/* try {
this.serviceLink.readInt((byte)0, (byte)0, 0);
} catch (IOException e) {
e.printStackTrace();
}
*/
System.err.println("BRKVAL: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0020));
System.err.println("SPLIM: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0021));
System.err.println("tCounter: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0500));
System.err.println("tInterface:" + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0501));
}
@Override
public String getChannelName(Integer channel) {
return chprop.getProperty(String.format("channel.%d", channel),String.format("Kanal %d",channel+1));
}
@Override
public void setChannelName(Integer channel, String name) {
chprop.setProperty(String.format("channel.%d", channel), name);
saveProps();
}
@Override
public int[] getChannelCounters() {
checkOpen();
int nch = getChannels();
int[] counters = new int[ nch ];
for (int i=0;i<nch;i++) {
Integer v = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x1000 + i);
if (v != null) {
counters[i] = v;
} else {
counters[i] = 0;
}
}
return counters;
}
@Override
public void setChannelCounter(Integer channel, Integer count) {
checkOpen();
try {
serviceLink.writeInt((byte)13, (byte)0, 0x1000 + channel, count);
} catch (ServiceLinkRequestFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceLinkException e) {
e.printStackTrace();
}
}
@Override
public int[] getChannelOffsets() {
checkOpen();
int nch = getChannels();
int[] offsets = new int[ nch ];
for (int i=0;i<nch;i++) {
offsets[i] = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x1100 + i);
}
return offsets;
}
@Override
public void setChannelOffset(Integer channel, Integer offset) {
checkOpen();
try {
serviceLink.writeInt((byte)13, (byte)0, 0x1100 + channel, offset);
} catch (ServiceLinkRequestFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceLinkException e) {
e.printStackTrace();
}
}
@Override
public int getChannels() {
checkOpen();
System.err.println("BRKVAL: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0020));
System.err.println("SPLIM: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0021));
Integer r = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0400 );
if (r == null)
return 0;
return r;
}
@Override
public String getPhysicalInterfaceName() {
return serviceLink.getSerialPort().getPortName();
}
@Override
public void setPhysicalInterfaceName(String interfaceName) {
try {
chprop.setProperty("io.port", interfaceName);
saveProps();
if ((this.serviceLink == null) || (serviceLink.getSerialPort() == null)) {
NewSerialPort nsp = new NewSerialPort(interfaceName);
setSerialPort(nsp);
} else {
serviceLink.getSerialPort().setPortName(interfaceName);
}
} catch (ServiceLinkException sle){
sle.printStackTrace();
}
}
@Override
public String[] getPhysicalInterfaceNames() {
return SerialPort.getPortNames();
}
@Override
public Integer getPhysicalInputs() throws SerialPortExeption {
Integer result;
result = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0502);
return result;
}
}