RXTX

RXTX是个提供串口和并口通信的开源java类库,由该项目发布的文档均遵循LGPL协议。该项目的主页位于http://users.frii.com/jarvi/rxtx/index.html。
RXTX项目提供了Windows,Linux,Mac os X,Solaris操作系统下的兼容javax.comm 串口通讯包API的实现,为其他研发人员在此类系统下研发串口应用提供了相当的方便。
针对x86体系结构的Linux操作系统平台,RXTX的部署包括下面几个文档:
* RXTXcomm.jar RXTX自己的javax.comm实现
* librxtxSerial.so 由RXTXcomm.jar调用的底层串口库文档
* librxtxParallel.so 由RXTXcomm.jar调用的底层并口库文档
* javax.comm.properties RXTX驱动的类配置文档,内容是Driver=gnu.io.RXTXCommDriver
三.RXTX的配置方法及部分 源代码(Linux环境)
为了使我们的程式使用RXTX作为 串口通讯的底层API,需要配置他的环境。仍然以Linux系统平台为例:
1.复制librxtxSerial.so,librxtxParallel.so到$JAVA_HOME/lib/$(ARCH)/
2.复制RXTXcomm.jar到$JAVA_HOME/ext/,或在应用程式启动的CLASSPATH中包含RXTXcomm.jar
3.定义驱动类后将javax.comm.properties放在应用程式的根目录下
RXTX的使用上和sun提供的comm.jar基本相同,编程时最明显的不同是要包含的包名由javax.comm.*改成了gnu.io.*。下面是我们环境监测系统中封装的一个232串口驱动类部分 源代码,使用RXTX作为 串口通讯类库。
Windows系统配置如下:
1. 将RXTXcomm.jar放到<jre_home>\lib\ext\下
2. 把rxtxParallel.dll,rxtxSerial.dll这两个放到你java的当前目录下(选windows下的)
3. 例子仍然采用原来SUN官方<ReadSimple.java>实例(change all references from 'javax.comm' to 'gnu.io' )
4. 参考网址:http://users.frii.com/jarvi/rxtx/download.html
=============================
以下为RXTX的简单应用代码,仅参考。
import java.io.*;
import java.util.*;
import gnu.io.*;
public class RFIDReader implements Runnable,SerialPortEventListener{
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args){
init();
}
public static void init(){
portList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements()){
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
if(portId.getName().equals("COM1")){
RFIDReader reader=new RFIDReader();
System.out.println("COM1 start!");
}
}
}
}
public RFIDReader(){
try{
serialPort=(SerialPort)portId.open("Sunder",2000);
}catch(PortInUseException e){System.out.println(e);}
try{
inputStream=serialPort.getInputStream();
}catch(IOException e){System.out.println(e);}
try{
serialPort.addEventListener(this);
}catch(TooManyListenersException e){System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
try{
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}catch(UnsupportedCommOperationException e){System.out.println(e);}
readThread=new Thread(this);
readThread.start();
}
public void run(){
try{
Thread.sleep(20000);
}catch(InterruptedException e){System.out.println(e);}
}
public void serialEvent(SerialPortEvent event){
switch(event.getEventType()){
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer=new byte[20];
try{
while(inputStream.available()>0){
int numBytes=inputStream.read(readBuffer);
}
System.out.println(new String(readBuffer));
}catch(IOException e){System.out.println(e);}
break;
}
}
}
=============
开源万岁~!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值