在做过一年多的RXTX操作串口项目有现在把一些平时遇到的问题在这里写写: 
RXTX是一个开源包,主要是在COMM开源包中做扩张,以前的COMM包只能在WINDOWS下面对串口或并口做操作,扩充后的RXTX可以在LINUX和MAC下对串口和并口做操作。  现在跨平台: 

在RXTX网站下载JAR包和动态库 
http://users.frii.com/jarvi/rxtx/download.html 

下载后配置环境 

Windows 

拷贝RXTXcomm.jar 文件到 \jre\lib\ext 目录下 
拷贝rxtxSerial.dll文件到 \jre\bin目录下 

Mac OS X (x86 and ppc) (there is an Installer with the source) 

MAC下面我自己没有配置环境成功,后来找一个MAC下RXTX的安装把环境配置好的。 
http://www.jdivelog.org/how-to/mac-os-x/下载安装环境配置文件RXTX_Tiger.pkg 

Linux (only x86, x86_64, ia64 here but more in the ToyBox) 

拷贝RXTXcomm.jar 文件到 /jre/lib/ext 目录下 
拷贝librxtxSerial.so 文件到 /jre/lib/[machine type] (i386 for instance)目录下 
并将拷贝文件释放权限给所有用户 

Solaris (sparc only so far) 

拷贝RXTXcomm.jar 文件到 /jre/lib/ext 目录下 
拷贝librxtxSerial.so 文件到 /jre/lib/[machine type]目录下 
并将拷贝文件释放权限给所有用户 

环境搭建好后开始写代码实现 


Java代码  

  1. import java.io.*;   

  2. import java.text.SimpleDateFormat;   

  3. import java.util.Date;   

  4. import java.util.TooManyListenersException;   

  5.   

  6. import gnu.io.CommPortIdentifier;   

  7. import gnu.io.NoSuchPortException;   

  8. import gnu.io.PortInUseException;   

  9. import gnu.io.SerialPort;   

  10. import gnu.io.SerialPortEvent;   

  11. import gnu.io.SerialPortEventListener;   

  12.   

  13. public class SerialComm implements SerialPortEventListener, Runnable   

  14. {   

  15.     public final static String PORT_OWER = "MonitorApp";   

  16.   

  17.     private boolean isOpen;   

  18.   

  19.     private boolean isStart;   

  20.   

  21.     private boolean isSave;   

  22.   

  23.     private boolean isPrint;   

  24.   

  25.     private Thread readThread;   

  26.   

  27.     private String portName;   

  28.   

  29.     private String portAddress;   

  30.   

  31.     private CommPortIdentifier portId;   

  32.   

  33.     private SerialPort serialPort;   

  34.   

  35.     private DataInputStream inputStream;   

  36.   

  37.     private OutputStream outputStream;   

  38.   

  39.     private SimpleDateFormat formatter;   

  40.   

  41.     // prase data with process   

  42.     private String dataProtocol;   

  43.   

  44.     private Object readWriteLock = new Object();   

  45.   

  46.   

  47.     public SerialComm() {   

  48.         isOpen = false;   

  49.         isStart = false;   

  50.         isSave = true;   

  51.         isPrint = false;   

  52.         formatter = new SimpleDateFormat("[yyyy-MM-dd hh:mm:ss,SSS]");   

  53.   

  54.         portName = "COM1";   

  55.         portAddress = "LOCAL";   

  56.         dataProtocol = "Gooseli";   

  57.     }   

  58.   

  59.     public void init(String port, String protocol) throws Exception   

  60.     {   

  61.         portName = port;   

  62.         portAddress = portName;   

  63.         dataProtocol = protocol;   

  64.   

  65.         init();   

  66.     }   

  67.   

  68.     public void init(String port, String address, String protocol) throws Exception   

  69.     {   

  70.         portName = port;   

  71.         portAddress = address;   

  72.         dataProtocol = protocol;   

  73.   

  74.         init();   

  75.     }   

  76.   

  77.     public void init() throws IOException, Exception, Exception   

  78.     {   

  79.         if (isOpen)   

  80.         {   

  81.             close();   

  82.         }   

  83.   

  84.         try  

  85.         {   

  86.             //传送串口名创建CommPortIdentifier对象服务。   

  87.             portId = CommPortIdentifier.getPortIdentifier(portName);   

  88.   

  89.             //使用portId对象服务打开串口,并获得串口对象   

  90.             serialPort = (SerialPort) portId.open(PORT_OWER, 2000);   

  91.   

  92.             //通过串口对象获得读串口流对象   

  93.             inputStream = new DataInputStream(serialPort.getInputStream());   

  94.   

  95.             //通过串口对象获得写串口流对象   

  96.             outputStream = serialPort.getOutputStream();   

  97.   

  98.             isOpen = true;   

  99.         } catch (NoSuchPortException ex)   

  100.         {   

  101.             throw new Exception(ex.toString());   

  102.         } catch (PortInUseException ex)   

  103.         {   

  104.             throw new Exception(ex.toString());   

  105.         }   

  106.     }   

  107.   

  108.     public void start() throws Exception   

  109.     {   

  110.         if (!isOpen)   

  111.         {   

  112.             throw new Exception(portName + " has not been opened.");   

  113.         }   

  114.   

  115.         try  

  116.         {   

  117.             //创建对象线程   

  118.             readThread = new Thread(this);   

  119.             readThread.start();   

  120.   

  121.             //设置串口数据时间有效   

  122.             serialPort.notifyOnDataAvailable(true);   

  123.   

  124.             //增加监听   

  125.             serialPort.addEventListener(this);   

  126.   

  127.             isStart = true;   

  128.   

  129.         } catch (TooManyListenersException ex)   

  130.         {   

  131.             throw new Exception(ex.toString());   

  132.         }   

  133.     }   

  134.   

  135.     public void run()   

  136.     {   

  137.         String at = "at^hcmgr=1\r";   

  138.   

  139.         String strTemp = at + (char) Integer.parseInt("1a"16) + "z";   

  140.   

  141.         writeComm(strTemp);   

  142.         isPrint = true;   

  143.     }   

  144.   

  145.     public void stop()   

  146.     {   

  147.         if (isStart)   

  148.         {   

  149.             serialPort.notifyOnDataAvailable(false);   

  150.             serialPort.removeEventListener();   

  151.   

  152.             isStart = false;   

  153.         }   

  154.     }   

  155.   

  156.     public void close()   

  157.     {   

  158.         stop();   

  159.   

  160.         if (isOpen)   

  161.         {   

  162.             try  

  163.             {   

  164.                 inputStream.close();   

  165.                 outputStream.close();   

  166.                 serialPort.close();   

  167.   

  168.                 isOpen = false;   

  169.             } catch (IOException ex)   

  170.             {   

  171.             }   

  172.         }   

  173.     }   

  174.   

  175.     //如果串口有数据上报则主动调用此方法   

  176.     public void serialEvent(SerialPortEvent event)   

  177.     {   

  178.         switch (event.getEventType())   

  179.         {   

  180.         case SerialPortEvent.BI:   

  181.         case SerialPortEvent.OE:   

  182.         case SerialPortEvent.FE:   

  183.         case SerialPortEvent.PE:   

  184.         case SerialPortEvent.CD:   

  185.         case SerialPortEvent.CTS:   

  186.         case SerialPortEvent.DSR:   

  187.         case SerialPortEvent.RI:   

  188.         case SerialPortEvent.OUTPUT_BUFFER_EMPTY:   

  189.             break;   

  190.         case SerialPortEvent.DATA_AVAILABLE:   

  191.             readComm();   

  192.             break;   

  193.         default:   

  194.             break;   

  195.         }   

  196.     }   

  197.   

  198.     public void readComm()   

  199.     {   

  200.         StringBuffer readBuffer = new StringBuffer();   

  201.         String scannedInput = "";   

  202.         Date currentTime = null;   

  203.         String TimeStamp = "";   

  204.         int c;   

  205.         char a;   

  206.         try  

  207.         {   

  208.             InputStreamReader fis = new InputStreamReader(inputStream, "utf-8");   

  209.             while ((c = fis.read()) != -1)   

  210.             {   

  211.                 readBuffer.append((char) c);   

  212.             }   

  213.             scannedInput = readBuffer.toString().trim();   

  214.             currentTime = new Date();   

  215.   

  216.             TimeStamp = formatter.format(currentTime);   

  217.   

  218.         } catch (IOException ex)   

  219.         {   

  220.             ex.printStackTrace();   

  221.   

  222.         } catch (Exception ex)   

  223.         {   

  224.   

  225.             ex.printStackTrace();   

  226.         }   

  227.   

  228.     }   

  229.   

  230.     public void writeComm(String outString)   

  231.     {   

  232.         synchronized (readWriteLock)   

  233.         {   

  234.             try  

  235.             {   

  236.                 outputStream.write(outString.getBytes());   

  237.             } catch (IOException ex)   

  238.             {   

  239.   

  240.             }   

  241.         }   

  242.     }   

  243.   

  244.     public static void main(String[] args)   

  245.     {   

  246.         SerialComm serialcomm = new SerialComm();   

  247.   

  248.         try  

  249.         {   

  250.             serialcomm.init("COM3""Air");// windows下测试端口   

  251.                

  252.             // serialcomm.init("/dev/ttyUSB0", "Air");//linux下测试端口   

  253.             serialcomm.start();   

  254.         } catch (Exception ex)   

  255.         {   

  256.         }   

  257.     }   

  258.   

  259. }