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.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); } 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 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(); 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