View Javadoc
1   package com.starphoenixmedia.candle_pos.scale;
2   
3   import com.starphoenixmedia.candle_pos.util.ScaleActionListener;
4   import gnu.io.NRSerialPort;
5   import java.io.IOException;
6   import java.io.InputStream;
7   
8   /**
9    *
10   * @author jlhawkwell
11   */
12  public class ScaleReader extends Thread implements Runnable
13  {
14  	private final NRSerialPort sp;
15  	private boolean exit = true;
16  	private ScaleActionListener sal;
17  
18  	private ScaleData sd;
19  
20  	public ScaleReader(NRSerialPort serial) { sp = serial; }
21  
22  	public void setActionListener(ScaleActionListener listen)
23  	{
24  		sal = listen;
25  	}
26  
27  	public void halt() { synchronized(this) { exit = false; } }
28  
29  	@Override public void run() { run(false); }
30  	public void run(boolean display)
31  	{
32  		sd = new ScaleData();
33  		InputStream is = sp.getInputStream();
34  
35  		byte[] bt = new byte[]{0,0,0, 0,0,0,0,0, 0};
36  		int a;
37  		try
38  		{
39  			while ( exit )
40  			{
41  				if ( is.available() >= 6 )
42  				{
43  					a = is.read();
44  					if ( a == 0x02 )
45  					{
46  						is.read(bt);
47  						sd.setRaw(bt);
48  						if ( sal != null ) { sal.actionPerformed(new ScaleAction(sd)); }
49  						if ( display )
50  						{
51  							System.out.print("\nData: ");
52  							System.out.print(sd.getReading());
53  							System.out.print("\t");
54  							System.out.print(sd.getUnit().getString());
55  							System.out.print("\t");
56  							System.out.println(sd.getMode().getString());
57  						}
58  					}
59  				}
60  			}
61  			sp.disconnect();
62  			sp.removeEventListener();
63  		}
64  		catch ( IOException ex ) { } // */
65  	}
66  
67  	public ScaleData getScaleData() { return sd; }
68  }