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

231 lines
5.3 KiB
Java
Raw Normal View History

2014-10-20 21:32:52 +02:00
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.io.SerialPort;
import org.hwo.io.servicelink.ServiceLink;
import org.hwo.io.servicelink.ServiceLinkException;
import org.hwo.io.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);
}
synchronized public void reset(int ch)
{
}
synchronized public void reset()
{
}
synchronized public void update()
{
System.err.println(String.format("BRKVAL: 0x%04x", serviceLink.getServiceRegisterCache().getCachedInteger((byte)0, (byte)0, (short)0x200)));
System.err.println(String.format("SPLIM : 0x%04x", serviceLink.getServiceRegisterCache().getCachedInteger((byte)0, (byte)0, (short)0x201)));
}
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();
} catch (ServiceLinkException e) {
e.printStackTrace();
}
}
public String[] getDeviceName()
{
return new String[]{"PulsCounter Board","0.1","alpha"};
}
public Date getDeviceTime()
{
return new Date();
}
public SerialPort getSerialPort() {
return serviceLink.getSerialPort();
}
public void setSerialPort(SerialPort 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();
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));
}
@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++) {
counters[i] = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x1000 + i);
}
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));
return serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0400 );
}
@Override
public String getPhysicalInterfaceName() {
checkOpen();
return serviceLink.getSerialPort().getPortName();
}
@Override
public void setPhysicalInterfaceName(String interfaceName) {
try {
chprop.setProperty("io.port", interfaceName);
saveProps();
SerialPort sport = SerialPort.newInstance();
sport.setPortName(interfaceName);
setSerialPort(sport);
} catch (ServiceLinkException sle){
sle.printStackTrace();
}
}
@Override
public String[] getPhysicalInterfaceNames() {
return SerialPort.getPortNames();
}
}