PageRenderTime 32ms CodeModel.GetById 12ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/fishlamp.application/src/fishlamp/application/rs232/FishlampComm.java

http://github.com/gbalage/fishlamptimer
Java | 89 lines | 65 code | 16 blank | 8 comment | 3 complexity | 2061dd08414e163b50b9cd3350cfddc1 MD5 | raw file
 1/**
 2 * 
 3 */
 4package fishlamp.application.rs232;
 5
 6import java.io.IOException;
 7import java.io.InputStream;
 8import java.io.OutputStream;
 9import java.util.HashSet;
10import java.util.Set;
11import java.util.TooManyListenersException;
12
13import fishlamp.application.protocol.IFrame;
14import fishlamp.application.protocol.IFrameListener;
15import fishlamp.application.protocol.IFrameParser;
16import fishlamp.application.protocol.IFrameParserFactory;
17import fishlamp.application.protocol.InputStreamParser;
18import gnu.io.CommPort;
19import gnu.io.CommPortIdentifier;
20import gnu.io.NoSuchPortException;
21import gnu.io.PortInUseException;
22import gnu.io.SerialPort;
23import gnu.io.UnsupportedCommOperationException;
24
25/**
26 * @author balage
27 *
28 */
29public class FishlampComm implements IFrameParserFactory{
30
31	private OutputStream out;
32
33	private final Set<IFrameListener> listeners = new HashSet<IFrameListener>();
34	
35	public void addListener(IFrameListener listener){
36		listeners.add(listener);
37	}
38	
39	public FishlampComm(String port) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException, TooManyListenersException {
40		 CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port);
41		 CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
42         
43         if ( commPort instanceof SerialPort )
44         {
45             SerialPort serialPort = (SerialPort) commPort;
46             serialPort.setSerialPortParams(
47            		 9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
48             
49             final InputStream in = serialPort.getInputStream();
50             out = serialPort.getOutputStream();
51                            
52             InputStreamParser parser = new InputStreamParser(this, in);
53             parser.addListener(new IFrameListener() {
54				
55				@Override
56				public void frameArrived(Object frame) {
57						//System.out.println("Frame: "+frame);
58						for(IFrameListener fl : listeners){
59							fl.frameArrived(frame);
60						}
61				}
62			});
63             Thread t = new Thread(parser);
64             t.setDaemon(true);
65             t.start();
66
67         }
68         else
69         {
70             
71         }
72	}
73
74	public synchronized void sendFrame(IFrame frame) throws IOException{
75		short[] data = frame.toBytes();
76		for(short d : data){
77			out.write(d);
78		}
79	}
80	
81	@Override
82	public IFrameParser<?> startParsing(short firstbyte) {
83		switch(firstbyte){
84		case 0xFF: return new TimeFrameParser(); 
85		}
86		return null;
87	}
88	
89}