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;
|
|
|
|
|
2015-05-05 16:05:22 +02:00
|
|
|
import org.hwo.bitfields.BitField;
|
2014-10-20 21:32:52 +02:00
|
|
|
import org.hwo.io.SerialPort;
|
2015-05-05 16:05:22 +02:00
|
|
|
import org.hwo.io.SerialPortExeption;
|
2016-09-06 16:49:11 +02:00
|
|
|
import org.hwo.io.NewSerialPort.NewSerialPort;
|
|
|
|
import org.hwo.servicelink.ServiceLink;
|
2017-03-10 15:11:01 +01:00
|
|
|
import org.hwo.servicelink.exceptions.ServiceLinkException;
|
|
|
|
import org.hwo.servicelink.exceptions.ServiceLinkRequestFailedException;
|
2014-10-20 21:32:52 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:05:22 +02:00
|
|
|
public void close(){
|
|
|
|
if (this.serviceLink != null)
|
|
|
|
this.serviceLink.close();
|
2014-10-20 21:32:52 +02:00
|
|
|
}
|
2015-05-05 16:05:22 +02:00
|
|
|
|
2014-10-20 21:32:52 +02:00
|
|
|
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();
|
2016-09-06 16:49:11 +02:00
|
|
|
serviceLink.getSerialPort().setTimeOut(250);
|
2015-05-05 16:05:22 +02:00
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-10-20 21:32:52 +02:00
|
|
|
} catch (ServiceLinkException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] getDeviceName()
|
|
|
|
{
|
|
|
|
return new String[]{"PulsCounter Board","0.1","alpha"};
|
|
|
|
}
|
|
|
|
|
|
|
|
public Date getDeviceTime()
|
|
|
|
{
|
|
|
|
return new Date();
|
|
|
|
}
|
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
public NewSerialPort getSerialPort() {
|
2014-10-20 21:32:52 +02:00
|
|
|
return serviceLink.getSerialPort();
|
|
|
|
}
|
|
|
|
|
2016-09-06 16:49:11 +02:00
|
|
|
public void setSerialPort(NewSerialPort serialPort) throws ServiceLinkException {
|
2014-10-20 21:32:52 +02:00
|
|
|
|
|
|
|
if (this.serviceLink == null)
|
|
|
|
this.serviceLink = new ServiceLink(serialPort);
|
|
|
|
|
|
|
|
if (this.serviceLink.isOpen())
|
|
|
|
this.serviceLink.close();
|
|
|
|
|
|
|
|
this.serviceLink.setSerialPort(serialPort);;
|
|
|
|
this.serviceLink.open();
|
2016-09-06 16:49:11 +02:00
|
|
|
this.serviceLink.getSerialPort().setTimeOut(500);
|
2014-10-20 21:32:52 +02:00
|
|
|
|
2015-05-05 16:05:22 +02:00
|
|
|
/* try {
|
2014-10-20 21:32:52 +02:00
|
|
|
this.serviceLink.readInt((byte)0, (byte)0, 0);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-05-05 16:05:22 +02:00
|
|
|
*/
|
2014-10-20 21:32:52 +02:00
|
|
|
System.err.println("BRKVAL: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0020));
|
|
|
|
System.err.println("SPLIM: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0021));
|
2015-05-05 16:05:22 +02:00
|
|
|
System.err.println("tCounter: " + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0500));
|
|
|
|
System.err.println("tInterface:" + this.serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0501));
|
2014-10-20 21:32:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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++) {
|
2015-05-05 16:05:22 +02:00
|
|
|
Integer v = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x1000 + i);
|
|
|
|
if (v != null) {
|
|
|
|
counters[i] = v;
|
|
|
|
} else {
|
|
|
|
counters[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-10-20 21:32:52 +02:00
|
|
|
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));
|
|
|
|
|
2015-05-05 16:05:22 +02:00
|
|
|
Integer r = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0400 );
|
|
|
|
if (r == null)
|
|
|
|
return 0;
|
|
|
|
return r;
|
2014-10-20 21:32:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-05-05 16:05:22 +02:00
|
|
|
public String getPhysicalInterfaceName() {
|
2014-10-20 21:32:52 +02:00
|
|
|
return serviceLink.getSerialPort().getPortName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPhysicalInterfaceName(String interfaceName) {
|
|
|
|
try {
|
|
|
|
chprop.setProperty("io.port", interfaceName);
|
|
|
|
saveProps();
|
|
|
|
|
2015-05-05 16:05:22 +02:00
|
|
|
if ((this.serviceLink == null) || (serviceLink.getSerialPort() == null)) {
|
2016-09-06 16:49:11 +02:00
|
|
|
NewSerialPort nsp = new NewSerialPort(interfaceName);
|
|
|
|
setSerialPort(nsp);
|
2015-05-05 16:05:22 +02:00
|
|
|
} else {
|
|
|
|
serviceLink.getSerialPort().setPortName(interfaceName);
|
|
|
|
}
|
|
|
|
|
2014-10-20 21:32:52 +02:00
|
|
|
|
|
|
|
} catch (ServiceLinkException sle){
|
|
|
|
sle.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] getPhysicalInterfaceNames() {
|
|
|
|
return SerialPort.getPortNames();
|
|
|
|
}
|
2015-05-05 16:05:22 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Integer getPhysicalInputs() throws SerialPortExeption {
|
|
|
|
Integer result;
|
|
|
|
|
|
|
|
result = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x0502);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2014-10-20 21:32:52 +02:00
|
|
|
|
|
|
|
}
|