天天看点

Java中基于Rxtx的串口操作

欢迎大家访问我的博客:地址

感谢:java串口通信

一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。

本文是基于RXTX(提供串口和并口通信)开源类库对串口进行操作的。

使用准备(windows,rxtx-2.2)

1.将RXTXcomm.jar放到%jre_home%\lib\ext\下,如:D:\Java\jdk1.6.0_06\jre\lib\ext,或者项目1.右键->2.Preperties(首选项)->3.Java Build Path->4.Libraries->5.Add External JARs引入

2.把 rxtxSerial.dll放入到java.library.path中,如:D:\Java\jdk1.6.0_06\jre\bin,或C:\windows\system32

RXTX

RXTX是一个提供串口和并口通信的开源java类库,由该项目发布的文件均遵循LGPL协议。

RXTX项目提供了Windows,Linux,Mac os X,Solaris操作系统下的兼容javax.comm串口通讯包API的实现,为其他开发人员在此类系统下开发串口应用提供了相当的方便。

RXTX的使用上与sun提供的comm.jar基本相同,编程时最明显的不同是要包含的包名由javax.comm.*改成了gnu.io.*

RxtxAPI 的核心是抽象的CommPort类(用于描述一个被底层系统支持的端口的抽象类,它包含一些高层的IO控制方法,这些方法对于所有不同的通讯端口来说是通用的)及其两个子类:SerialPort类和ParallePort类。其中,SerialPort类是用于串口通信的类,ParallePort类是用于并行口通信的类。CommPort类还提供了常规的通信模式和方法,例如:getInputStream( )方法和getOutputStream( )方法,专用于与端口上的设备进行通信。

然而,这些类的构造方法都被有意的设置为非公有的(non-public)。所以,不能直接构造对象,而是先通过静态的CommPortIdentifer.getPortIdentifiers()获得端口列表,再从这个端口列表中选择所需要的端口,并调用CommPortIdentifer对象的Open( )方法,这样,就能得到一个CommPort对象。当然,还要将这个CommPort对象的类型转换为某个非抽象的子类,表明是特定的通讯设备,该子类可以是SerialPort类和ParallePort类中的一个。下面将分别对CommPortIdentifier类,串口类SerialPort进行详细的介绍。

接口

CommDriver可负载设备(the loadable device)驱动程序接口的一部分

CommPortOwnershipListener传递各种通讯端口的所有权事件

ParallelPortEventListener传递并行端口事件

SerialPortEventListener传递串行端口事件

CommPort通讯端口

CommPortIdentifier通讯端口管理

ParallelPort并行通讯端口

ParallelPortEvent并行端口事件

SerialPortRS-232串行通讯端口

SerialPortEvent 串行端口事件

异常类

NoSuchPortException当驱动程序不能找到指定端口时抛出

PortInUseException当碰到指定端口正在使用中时抛出

UnsupportedCommOperationException驱动程序不允许指定操作时抛出

CommPortIdentifier类

这个类主要用于对通信端口进行管理和设置,是对端口进行访问控制的核心类,主要包括以下方法:

addPortName(String,int, CommDriver) 添加端口名到端口列表里

addPortOwnershipListener(CommPortOwnershipListener)添加端口拥有的监听器

removePortOwnershipListener(CommPortOwnershipListener)移除端口拥有的监听器

getCurrentOwner()获取当前占有端口的对象或应用程序

getName()获取端口名称

getPortIdentifier(CommPort)获取指定打开的端口的CommPortIdentifier类型对象

getPortIdentifier(String)获取以参数命名的端口的CommPortIdentifier类型对象

getPortIdentifiers()获取系统中的端口列表

getPortType()获取端口的类型

isCurrentlyOwned()判断当前端口是否被占用

open(FileDescriptor)用文件描述的类型打开端口

open(String,int) 打开端口,两个参数:程序名称,延迟时间(毫秒数)

SerialPort类

这个类用于描述一个RS-232串行通信端口的底层接口,它定义了串口通信所需的最小功能集。通过它,用户可以直接对串口进行读、写及设置工作。

SerialPort类中关于串口参数的静态成员变量说明:

DATABITS_5 数据位为5

DATABITS_6 数据位为6

DATABITS_7 数据位为7

DATABITS_8 数据位为8

PARITY_NONE 空格检验

PARITY_ODD 奇检验

PARITY_EVEN 偶检验

PARITY_MARK 标记检验

PARITY_SPACE 无检验

STOPBITS_1 停止位为1

STOPBITS_2 停止位为2

STOPBITS_1_5 停止位为1.5

SerialPort类中关于串口参数的方法说明:

getBaudRate()得到波特率

getParity()得到检验类型

getDataBits()得到数据位数

getStopBits()得到停止位数

setSerialPortParams(int,int, int, int) 设置串口参数依次为(波特率,数据位,停止位,奇偶检验)

SerialPort类中关于事件的静态成员变量说明:

BI Break interrupt 通讯中断

FE Framing error 帧错误

CD Carrier detect 载波侦听

OE Overrun error 溢位错误

CTS Clear to send 清除发送

PE Parity error 奇偶检验错误

DSR Data set ready 数据设备准备好

RI Ring indicator 响铃侦测

DATA_AVAILABLE 串口中的可用数据

OUTPUT_BUFFER_EMPTY 输出缓冲区已清空

SerialPort类中关于事件的方法说明:

isCD()是否有载波

isCTS()是否清除以传送

isDSR()数据是否备妥

isDTR()是否数据端备妥

isRI()是否响铃侦测

isRTS()是否要求传送

addEventListener(SerialPortEventListener)向SerialPort对象中添加串口事件监听器

removeEventListener()移除SerialPort对象中的串口事件监听器

notifyOnBreakInterrupt(boolean)设置中断事件true有效,false无效

notifyOnCarrierDetect(boolean)设置载波监听事件true有效,false无效

notifyOnCTS(boolean)设置清除发送事件true有效,false无效

notifyOnDataAvailable(boolean)设置串口有数据的事件true有效,false无效

notifyOnDSR(boolean)设置数据备妥事件true有效,false无效

notifyOnFramingError(boolean)设置发生错误事件true有效,false无效

notifyOnOutputEmpty(boolean)设置发送缓冲区为空事件true有效,false无效

notifyOnParityError(boolean)设置发生奇偶检验错误事件true有效,false无效

notifyOnRingIndicator(boolean)设置响铃侦测事件true有效,false无效

getEventType()得到发生的事件类型返回值为int型

sendBreak(int)设置中断过程的时间,参数为毫秒值

setRTS(boolean)设置或清除RTS位

setDTR(boolean)设置或清除DTR位

SerialPort中的其他常用方法说明:

close()关闭串口

getOutputStream()得到OutputStream类型的输出流

getInputStream()得到InputStream类型的输入流

串口API实例

public class ListPort {
	/**
	 * @Description:列出所有可用串口
	 * @author:Lu
	 * @date:2015-8-29 上午11:34:04
	 */
	public static void listPorts(){
		HashSet<CommPortIdentifier> portSet = getAvailableSerialPorts();
		for(CommPortIdentifier comm : portSet){
			System.out.println(comm.getName()  +  " - " +  getPortTypeName(comm.getPortType()) );
		}
	}
	/**
	 * @Description:列出所有通信端口
	 * @author:Lu
	 * @date:2015-8-29 下午2:06:17
	 */
	@SuppressWarnings("unchecked")
	public static void listCommPorts(){
		CommPortIdentifier.getPortIdentifiers();
		/*不带参数的getPortIdentifiers方法可以获得一个枚举对象,该对象包含了
		 系统中每个端口的CommPortIdentifier对象。注意这里的端口不仅仅是指串口,也包括并口。
		 这个方法还可以带参数,getPortIdentifiers(CommPort)获得已经被应用程序打开的端口
		 相对应的CommPortIdentifier对象。getPortIdentifier(String portName)
		 获取指定端口名(比如“COM1”)的CommPortIdentifier对象。*/
		java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier
				.getPortIdentifiers();
		while (portEnum.hasMoreElements()) {
			CommPortIdentifier portIdentifier = portEnum.nextElement();
			System.out.println(portIdentifier.getName() + " - "
					+ getPortTypeName(portIdentifier.getPortType()));
		}
	}
	/**
	 * @Description:获取通信端口类型名称
	 * @author:Lu
	 * @date:2015-8-29 上午11:35:32
	 */
	public static String getPortTypeName(int portType) {
		switch (portType) {
		case CommPortIdentifier.PORT_I2C:
			return "I2C";
		case CommPortIdentifier.PORT_PARALLEL: // 并口
			return "Parallel";
		case CommPortIdentifier.PORT_RAW:
			return "Raw";
		case CommPortIdentifier.PORT_RS485: // RS485端口
			return "RS485";
		case CommPortIdentifier.PORT_SERIAL: // 串口
			return "Serial";
		default:
			return "unknown type";
		}
	}
	/**
	 * @Description:获取所有可用的串口集合
	 * @author:Lu
	 * @date:2015-8-29 上午11:37:54
	 */
	@SuppressWarnings("unchecked")
	public static HashSet<CommPortIdentifier> getAvailableSerialPorts() {
		HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>();
		Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
		while (portList.hasMoreElements()) {
			CommPortIdentifier com = (CommPortIdentifier) portList
					.nextElement();
			switch (com.getPortType()) {
			case CommPortIdentifier.PORT_SERIAL:
				try {
					//open:(应用程序名【随意命名】,阻塞时等待的毫秒数)
					/*open方法打开通讯端口,获得一个CommPort对象,它使程序独占端口。
					如果端口正被其他应用程序占用,将使用CommPortOwnershipListener事件机制
					传递一个PORT_OWNERSHIP_REQUESTED事件。
					每个端口都关联一个InputStream和一个OutputStream,如果端口是用
					open方法打开的,那么任何的getInputStream都将返回相同的数据流对象,除非
					有close被调用。
					*/
					CommPort thePort = com.open(Object.class.getSimpleName(), 50);
					thePort.close();
					h.add(com);
				} catch (PortInUseException e) {
					//不可用串口
					System.out.println("Port, " + com.getName()
							+ ", is in use.");
				} catch (Exception e) {
					System.err.println("Failed to open port " + com.getName());
					e.printStackTrace();
				}
			}
		}
		return h;
	}
	public static void main(String[] args) {
		/**
		 * 可列出当前系统所有可用的串口名称,本机输出COM1 - Serial, COM2 - Serial.....COM6 - Serial
		 */
		listPorts();
		listCommPorts();
	}
}
           

测试代码  

/**
 * 串口参数的配置 串口一般有如下参数可以在该串口打开以前进行配置:
 * 包括波特率,输入/输出流控制,数据位数,停止位和奇偶校验。
 */
// 注:串口操作类一定要继承SerialPortEventListener 
public class SerialPortTest1 implements SerialPortEventListener {
	// 检测系统中可用的通讯端口类 
	private CommPortIdentifier portId;
	// 枚举类型
	private Enumeration<CommPortIdentifier> portList;

	// RS232串口
	private SerialPort serialPort;

	// 输入输出流
	private InputStream inputStream;
	private OutputStream outputStream;

	// 保存串口返回信息
	private String test = "";

	// 单例创建
	private static SerialPortTest1 uniqueInstance = new SerialPortTest1();

	// 初始化串口
	@SuppressWarnings("unchecked")
	public void init() {
		// 获取系统中所有的通讯端口
		portList = CommPortIdentifier.getPortIdentifiers();
		// 循环通讯端口
		while (portList.hasMoreElements()) {
			portId = portList.nextElement();
			// 判断是否是串口
			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				// 比较串口名称是否是"COM1"
				if ("COM1".equals(portId.getName())) {
					System.out.println("找到串口COM1");
					// 打开串口
					try {
						// open:(应用程序名【随意命名】,阻塞时等待的毫秒数)
						serialPort = (SerialPort) portId.open(Object.class.getSimpleName(), 2000);
						System.out.println("获取到串口对象,COM1");
						// 设置串口监听
						serialPort.addEventListener(this);
						// 设置串口数据时间有效(可监听)
						serialPort.notifyOnDataAvailable(true);
						// 设置串口通讯参数 
						// 波特率,数据位,停止位和校验方式
						// 波特率2400,偶校验 
						serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,//
								SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
						test = "";
						outputStream = serialPort.getOutputStream();

					} catch (PortInUseException e) {
						e.printStackTrace();
					} catch (TooManyListenersException e) {
						e.printStackTrace();
					} catch (UnsupportedCommOperationException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}

				}
			}
		}
	}

	// 实现接口SerialPortEventListener中的方法 读取从串口中接收的数据
	@Override
	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:	//有数据到达
			readComm();
			break;
		default:
			break;
		}
	}

	// 读取串口返回信息
	public void readComm() {
		byte[] readBuffer = new byte[1024];
		try {
			inputStream = serialPort.getInputStream();
			// 从线路上读取数据流
			int len = 0;
			while ((len = inputStream.read(readBuffer)) != -1) {
				System.out.println("实时反馈:" + new String(readBuffer, 0, len).trim() + new Date());
				test += new String(readBuffer, 0, len).trim();
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 关闭串口
	public void closeSerialPort() {
		if (serialPort != null) {
			serialPort.notifyOnDataAvailable(false);
			serialPort.removeEventListener();
			if (inputStream != null) {
				try {
					inputStream.close();
					inputStream = null;
				}
				catch (IOException e) {}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
					outputStream = null;
				}
				catch (IOException e) {}
			}
			serialPort.close();
			serialPort = null;
		}
	}

	public static void main(String[] args) {
		SerialPortTest1 sp = new SerialPortTest1();
		sp.init();
		System.out.println("输出" + sp.test);
		sp.closeSerialPort();
	}
}
           

之后,用虚拟串口工具VSPD创建两个串口,COM1和COM2,运行java程序,打开串口调试助手,从COM2向COM1发送数据,java程序就可以在控制台输出信息了。