Advertisement
Guest User

serial class

a guest
Aug 11th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package testlist;
  2.  
  3.  
  4. /**
  5.  *
  6.  * @author Atulmaharaj
  7.  */
  8. import java.io.BufferedReader;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStream;
  11. import gnu.io.CommPortIdentifier;
  12. import gnu.io.SerialPort;
  13. import gnu.io.SerialPortEvent;
  14. import gnu.io.SerialPortEventListener;
  15. import java.util.Enumeration;
  16. public class SerialClass implements SerialPortEventListener {
  17.  
  18.  public SerialPort serialPort;
  19.  /** The port we're normally going to use. */
  20.  private static final String PORT_NAMES[] = {
  21.  "/dev/tty.usbserial-A9007UX1", // Mac OS X
  22.  "/dev/ttyUSB0", // Linux
  23.  "COM3", // Windows
  24.  };
  25.  
  26. public static BufferedReader input;
  27. public static OutputStream output;
  28.  /** Milliseconds to block while waiting for port open */
  29.  public static final int TIME_OUT = 2000;
  30.  /** Default bits per second for COM port. */
  31.  public static final int DATA_RATE = 9600;
  32.  
  33. public void initialize() {
  34.  CommPortIdentifier portId = null;
  35.  Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
  36.  
  37. //First, Find an instance of serial port as set in PORT_NAMES.
  38.  while (portEnum.hasMoreElements()) {
  39.  CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
  40.  for (String portName : PORT_NAMES) {
  41.  if (currPortId.getName().equals(portName)) {
  42.  portId = currPortId;
  43.  break;
  44.  }
  45.  }
  46.  }
  47.  if (portId == null) {
  48.  System.out.println("Could not find COM port.");
  49.  return;
  50.  }
  51.  
  52. try {
  53.  // open serial port, and use class name for the appName.
  54.  serialPort = (SerialPort) portId.open(this.getClass().getName(),
  55.  TIME_OUT);
  56.  
  57. // set port parameters
  58.  serialPort.setSerialPortParams(DATA_RATE,
  59.  SerialPort.DATABITS_8,
  60.  SerialPort.STOPBITS_1,
  61.  SerialPort.PARITY_NONE);
  62.  
  63. // open the streams
  64.  input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
  65.  output = serialPort.getOutputStream();
  66.  char ch = 1;
  67.  output.write(ch);
  68.  
  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. public synchronized void close() {
  79.  if (serialPort != null) {
  80.  serialPort.removeEventListener();
  81.  serialPort.close();
  82.  }
  83.  }
  84.  
  85. public synchronized void serialEvent(SerialPortEvent oEvent) {
  86.  if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
  87.  try {
  88.  String inputLine=input.readLine();
  89.  System.out.println(inputLine);
  90.  } catch (Exception e) {
  91.  System.err.println(e.toString());
  92.  }
  93.  }
  94.  
  95.  }
  96.  
  97.  public static synchronized void writeData(String data) {
  98.  //System.out.println("Sent: " + data);
  99.  try {
  100.  output.write(data.getBytes());
  101.  } catch (Exception e) {
  102.  System.out.println("could not write to port");
  103.  }
  104.  }
  105.  
  106. public static void main(String[] args) throws Exception {
  107.  SerialClass main = new SerialClass();
  108.  main.initialize();
  109.  Thread t=new Thread() {
  110.  public void run() {
  111.  //the following line will keep this app alive for 1000 seconds,
  112.  //waiting for events to occur and responding to them (printing incoming messages to console).
  113.  try {Thread.sleep(1500);
  114.  writeData("2");} catch (InterruptedException ie) {}
  115.  }
  116.  };
  117.  t.start();
  118.  System.out.println("Started");
  119.  }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement