gnomezgrave

Serial Port reader using Java

Aug 10th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.OutputStream;
  4. import gnu.io.CommPortIdentifier;
  5. import gnu.io.SerialPort;
  6. import gnu.io.SerialPortEvent;
  7. import gnu.io.SerialPortEventListener;
  8. import java.util.Enumeration;
  9. import java.util.Observable;
  10. import java.util.Observer;
  11.  
  12. public class GSRReader extends Observable implements SerialPortEventListener {
  13.  
  14.     SerialPort serialPort;
  15.     /** The port we're normally going to use. */
  16.     private static final String PORT_NAMES[] = {
  17.         "COM7" // Windows
  18.     };
  19.     /**
  20.      * A BufferedReader which will be fed by a InputStreamReader
  21.      * converting the bytes into characters
  22.      * making the displayed results codepage independent
  23.      */
  24.     private BufferedReader input;
  25.     /** The output stream to the port */
  26.     private OutputStream output;
  27.     /** Milliseconds to block while waiting for port open */
  28.     private static final int TIME_OUT = 2000;
  29.     /** Default bits per second for COM port. */
  30.     private static final int DATA_RATE = 9600;
  31.  
  32.     public void initialize() {
  33.         // the next line is for Raspberry Pi and
  34.         // gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
  35.         System.setProperty("gnu.io.rxtx.SerialPorts", "COM7");
  36.  
  37.         CommPortIdentifier portId = null;
  38.         Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
  39.  
  40.         //First, Find an instance of serial port as set in PORT_NAMES.
  41.         while (portEnum.hasMoreElements()) {
  42.             CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
  43.             for (String portName : PORT_NAMES) {
  44.                 if (currPortId.getName().equals(portName)) {
  45.                     portId = currPortId;
  46.                     break;
  47.                 }
  48.             }
  49.         }
  50.         if (portId == null) {
  51.             System.out.println("Could not find COM port.");
  52.             return;
  53.         }
  54.  
  55.         try {
  56.             // open serial port, and use class name for the appName.
  57.             serialPort = (SerialPort) portId.open(this.getClass().getName(),
  58.                     TIME_OUT);
  59.  
  60.             // set port parameters
  61.             serialPort.setSerialPortParams(DATA_RATE,
  62.                     SerialPort.DATABITS_8,
  63.                     SerialPort.STOPBITS_1,
  64.                     SerialPort.PARITY_NONE);
  65.  
  66.             // open the streams
  67.             input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
  68.             output = serialPort.getOutputStream();
  69.  
  70.             // add event listeners
  71.             serialPort.addEventListener(this);
  72.             serialPort.notifyOnDataAvailable(true);
  73.         } catch (Exception e) {
  74.             System.err.println(e.toString());
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * This should be called when you stop using the port.
  80.      * This will prevent port locking on platforms like Linux.
  81.      */
  82.     public synchronized void close() {
  83.         if (serialPort != null) {
  84.             serialPort.removeEventListener();
  85.             serialPort.close();
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Handle an event on the serial port. Read the data and print it.
  91.      */
  92.     public synchronized void serialEvent(SerialPortEvent oEvent) {
  93.         if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
  94.             try {
  95.                 String inputLine = input.readLine();
  96.                 // System.out.println(inputLine);
  97.                 try {
  98.                     final double val = Double.parseDouble(inputLine);
  99.                     Thread.sleep(100);
  100.                     setChanged();
  101.                     notifyObservers(val);
  102.  
  103.                 } catch (Exception e) {
  104.                 }
  105.             } catch (Exception e) {
  106.                 System.err.println(e.toString());
  107.             }
  108.         }
  109.         // Ignore all the other eventTypes, but you should consider the other ones.
  110.     }
  111.  
  112.     public static void main(String[] args) throws Exception {
  113.     }
  114. }
  115.  
  116. class MyObserver implements Observer {
  117.  
  118.     private final LineChart demo; // JPanel extending JFreeChart
  119.  
  120.     public MyObserver(LineChart demo) {
  121.         this.demo = demo;
  122.     }
  123.  
  124.     @Override
  125.     public void update(Observable o, Object arg) {
  126.         if (o instanceof GSRReader) {
  127.             System.out.println("Observer " + arg);
  128.             demo.drawChart(Double.parseDouble(arg.toString()));
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment