Advertisement
Guest User

RXTX SerialConnection.java

a guest
May 30th, 2012
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.util.ArrayList;
  5.  
  6. import gnu.io.CommPort;
  7. import gnu.io.CommPortIdentifier;
  8. import gnu.io.NoSuchPortException;
  9. import gnu.io.PortInUseException;
  10. import gnu.io.SerialPort;
  11. import gnu.io.UnsupportedCommOperationException;
  12.  
  13.  
  14. public class SerialConnection {
  15.     CommPortIdentifier portIdentifier;
  16.     OutputStream portOut;
  17.     InputStream portIn;
  18.     SerialPort serialPort;
  19.     String port;
  20.     private final Object stopReadMutex =  new Object();
  21.     private final Object stopWriteMutex =  new Object();
  22.     public boolean stopRead = false;
  23.     public boolean stopWrite = false;
  24.  
  25.     public SerialConnection(String portName){
  26.         this(portName, 2400, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD);
  27.     }
  28.  
  29.     public SerialConnection(String portName, int baudRate, int databits, int stopbits, int parity){
  30.         port = portName;
  31.         try {
  32.             portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
  33.         } catch (NoSuchPortException e) {
  34.             e.printStackTrace();
  35.         }
  36.         if ( portIdentifier.isCurrentlyOwned() )
  37.         {
  38.             System.out.println("Error: Port is currently in use");
  39.         }
  40.         else
  41.         {
  42.             CommPort commPort = null;
  43.             try {
  44.                 commPort = portIdentifier.open(this.getClass().getName(),2000);
  45.             } catch (PortInUseException e) {
  46.                 e.printStackTrace();
  47.             }
  48.  
  49.             if ( commPort instanceof SerialPort )
  50.             {
  51.                 serialPort = (SerialPort) commPort;
  52.                 serialPort.setDTR(true);
  53.                 serialPort.setRTS(true);
  54.                 InputStream in = null;
  55.                 OutputStream out = null;
  56.                 try {
  57.                     serialPort.setSerialPortParams(baudRate,databits,stopbits,parity);
  58.                     in = serialPort.getInputStream();
  59.                     out = serialPort.getOutputStream();
  60.                 } catch (UnsupportedCommOperationException e) {
  61.                     e.printStackTrace();
  62.                 } catch (IOException e) {
  63.                     e.printStackTrace();
  64.                 }
  65.  
  66.  
  67.  
  68.                 (new Thread(new SerialReader(in))).start();
  69.                 (new Thread(new SerialWriter(out))).start();
  70.  
  71.                 portIn = in;
  72.                 portOut = out;
  73.  
  74.             }
  75.             else
  76.             {
  77.                 System.out.println("Error: Only serial ports are handled by this example.");
  78.             }
  79.         }
  80.     }
  81.  
  82.     public void write(String s){
  83.         try {
  84.             System.out.println(s);
  85.             s = s+"\r\n";
  86.             portOut.write(s.getBytes());
  87.         } catch (IOException e) {
  88.             e.printStackTrace();
  89.         }
  90.     }
  91.  
  92.     /** */
  93.     public class SerialReader implements Runnable
  94.     {
  95.         InputStream in;
  96.  
  97.         public SerialReader ( InputStream in )
  98.         {
  99.             synchronized(stopReadMutex)
  100.             {stopRead = false;}
  101.             this.in = in;
  102.         }
  103.  
  104.         public void run ()
  105.         {
  106.             byte[] buffer = new byte[1024];
  107.             int len = -1;
  108.             try
  109.             {
  110.                 while ( ( len = this.in.read(buffer)) > -1 )
  111.                 {
  112.                     if(stopRead){
  113.                         System.out.println("StoppedReading");
  114.                         break;
  115.                     }
  116.                     System.out.print(new String(buffer,0,len));
  117.                 }
  118.             }
  119.             catch ( IOException e )
  120.             {
  121.                 e.printStackTrace();
  122.             }            
  123.         }
  124.     }
  125.  
  126.     /** */
  127.     public class SerialWriter implements Runnable
  128.     {
  129.         OutputStream out;
  130.  
  131.         public SerialWriter ( OutputStream out )
  132.         {
  133.             this.out = out;
  134.             synchronized(stopWriteMutex)
  135.             {stopWrite = false;}
  136.         }
  137.  
  138.         public void run ()
  139.         {
  140.             while(true){
  141.                 if(stopWrite){
  142.                     System.out.println("Stopped writing");
  143.                     break;
  144.                 }
  145.             }
  146.         }
  147.     }
  148.  
  149.     static void listPorts()
  150.     {
  151.         @SuppressWarnings("unchecked")
  152.         java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
  153.         while ( portEnum.hasMoreElements() )
  154.         {
  155.             CommPortIdentifier portIdentifier = portEnum.nextElement();
  156.             System.out.println(portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType())  + " - " + portIdentifier.isCurrentlyOwned());
  157.         }        
  158.     }
  159.  
  160.     static String[] portsArray()
  161.     {
  162.         @SuppressWarnings("unchecked")
  163.         java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
  164.         ArrayList<String> portsAL = new ArrayList<String>();
  165.         while ( portEnum.hasMoreElements() )
  166.         {
  167.             CommPortIdentifier portIdentifier = portEnum.nextElement();
  168.             portsAL.add(portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType()));
  169.         }        
  170.         return portsAL.toArray(new String[0]);
  171.     }
  172.  
  173.     static String getPortTypeName ( int portType )
  174.     {
  175.         switch ( portType )
  176.         {
  177.         case CommPortIdentifier.PORT_I2C:
  178.             return "I2C";
  179.         case CommPortIdentifier.PORT_PARALLEL:
  180.             return "Parallel";
  181.         case CommPortIdentifier.PORT_RAW:
  182.             return "Raw";
  183.         case CommPortIdentifier.PORT_RS485:
  184.             return "RS485";
  185.         case CommPortIdentifier.PORT_SERIAL:
  186.             return "Serial";
  187.         default:
  188.             return "unknown type";
  189.         }
  190.     }
  191.  
  192.     public void disconnect(){
  193.         try {
  194.             synchronized(stopReadMutex)
  195.             {stopRead = true;}
  196.             synchronized(stopWriteMutex)
  197.             {stopWrite = true;}
  198.             portOut.close();
  199.             portIn.close();
  200.             serialPort.close();
  201.  
  202.         } catch (IOException e) {
  203.             e.printStackTrace();
  204.         }
  205.  
  206.  
  207.     }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement