import ch.psi.pshell.device.Readable; import ch.psi.pshell.serial.SerialPortDevice; import ch.psi.pshell.serial.SerialPortDeviceConfig; import static ch.psi.utils.BitMask.*; import ch.psi.utils.Convert; import ch.psi.utils.State; import java.io.IOException; /* * */ public class LaserUE extends SerialPortDevice { final Readable readable; final double range; final double offset; public LaserUE(String name, String port, double range, double offset) { super(name, port, 921600, SerialPortDeviceConfig.DataBits.DB_8, SerialPortDeviceConfig.StopBits.SB_1, SerialPortDeviceConfig.Parity.None); this.range = range; this.offset = offset; this.setMode(Mode.FullDuplex); readable = new Readable() { @Override public Object read() throws IOException, InterruptedException { return take(); } @Override public String getName() { return LaserUE.this.getName() + "_readout"; } }; } @Override protected void doInitialize() throws IOException, InterruptedException { super.doInitialize(); //TODO: Start DAQ in ILD1320: http://www.micro-epsilon.com/download/manuals/man--optoNCDT-1320--en.pdf setState(State.Ready); write("OUTPUT RS422\n\r"); } public Readable getReadable() { return readable; } int value = 0; int count = 0; @Override protected void onByte(int rx) { int index = ((rx & BIT7) > 0) ? 2 : (((rx & BIT6) > 0) ? 1 : 0); if (count == index) { if (index == 0) { value = rx & 0x3F; count = 1; } else if (index == 1) { value = ((rx & 0x3F) << 6) + value; count = 2; } else if (index == 2) { value = ((rx & 0x0F) << 12) + value; //double val = ((double)value)/1000; double val = (0.01) * (102.0 * value / 65520.0 - 1) * range; if ((val <= 0.0) || (val > range)) { val = Double.NaN; } else { val+=offset; val = Convert.roundDouble(val, 3); } setCache(val); count = 0; } else { count = 0; } } else { count = 0; } } public static void main(String[] args) throws Exception { LaserUE l = new LaserUE("UE", "COM3", 50.0, 35.0); l.setMonitored(true); l.initialize(); Thread.sleep(10000); l.close(); } }