Advertisement
Guest User

Untitled

a guest
Aug 18th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import gnu.io.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5.  
  6. public class TwoWaySerialComm
  7. {
  8.     public void arduinPort(){
  9.     CommPort arduino = getArduinoPort();
  10.     try {
  11.         arduino.getOutputStream().write(1);
  12.     } catch (IOException e) {
  13.         // TODO Auto-generated catch block
  14.         e.printStackTrace();
  15.     }
  16.     }
  17.     public CommPort getArduinoPort() {
  18.         @SuppressWarnings("rawtypes")
  19.         Enumeration ports = CommPortIdentifier.getPortIdentifiers();
  20.         while(ports.hasMoreElements()) {
  21.             CommPortIdentifier identifier = (CommPortIdentifier) ports.nextElement();
  22.             if(isArduino(identifier)) {
  23.                 try {
  24.                     return identifier.open(getClass().getName(), 2000);
  25.                 } catch (PortInUseException e) {
  26.                     // TODO Auto-generated catch block
  27.                     e.printStackTrace();
  28.                 } // 2 second timeout
  29.             }
  30.         }
  31.         return null;
  32.     }
  33.  
  34.     public boolean isArduino(CommPortIdentifier identifier) {
  35.        
  36.         if (identifier.getName()== "COM3"){
  37.             return true;
  38.         }
  39.         else{
  40.             return false;
  41.         }
  42.         // if you know the name of the port ahead of time you can
  43.         // compare it here with identifier.getName(), otherwise
  44.         // you can interface with the user like the Arduino IDE's
  45.         // serial monitor
  46.    
  47. }
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement