JAVA 串口编程

一、环境配置

(1)解压复制文件

解压javacomm20-win32.zip

把win32com.dll复制到<JAVA_HOME>/jre/bin 和<JDK>/bin目录下

把comm.jar复制到<JAVA_HOME>/jre/lib/ext和<JDK>/lib目录下

把javax.comm.properties复制到<JAVA_HOME>/jre/lib和<JDK>/lib目录下

(2)设置环境变量

CLASSPATH=<JAVA_HOME>/jre /lib/ext /comm.jar;%classpath%

(3)在Eclipse的工程中加入jar包

“工程右键-> properties-> java build path->libraries->add external jar”去找到<JDK>/lib中放置的comm.jar的位置。

二、API概述

接口

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

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

ParallelPortEventListener 传递并行端口事件

SerialPortEventListener 传递串行端口事件

CommPort 通讯端口

CommPortIdentifier通讯端口管理

ParallelPort 并行通讯端口

ParallelPortEvent 并行端口事件

SerialPort RS-232串行通讯端口

SerialPortEvent 异常类

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

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

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

(1)CommPort类

描述被底层系统支持的端口的抽象类。包含一些高层的IO控制方法,这些方法对于所有不同的通讯端口来说是通用的。SerialPort(串口) 和ParallelPort(并口)都是它的子类。

(2)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) 打开端口,两个参数:程序名称,延迟时间(毫秒数)

(3)SerialPort

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

A:串口参数的函数

getBaudRate() 得到波特率 getParity() 得到检验类型

getDataBits() 得到数据位数 getStopBits() 得到停止位数

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

close() 关闭串口

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

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

B:事件及事件方法

isCD() 是否有载波

isCTS() 是否清除发送

isDSR() 数据是否准备就绪

isDTR() 数据终端是否准备就绪

isRI() 是否响铃侦测

isRTS() 是否要求发送

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

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

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

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

setRTS(boolean) 设置或清除RTS位

setDTR(boolean) 设置或清除DTR位

notifyOnBreakInterrupt(boolean) 设置中断事件

notifyOnCarrierDetect(boolean) 设置载波监听事件

notifyOnCTS(boolean) 设置清除发送事件

notifyOnDataAvailable(boolean) 设置串口有数据的事件

notifyOnDSR(boolean) 设置数据备妥事件

notifyOnFramingError(boolean) 设置发生错误事件

notifyOnOutputEmpty(boolean) 设置发送缓冲区为空事件

notifyOnParityError(boolean) 设置发生奇偶检验错误事件

notifyOnRingIndicator(boolean) 设置响铃侦测事件

C:串口参数的静态成员变量

成员变量 说明 成员变量 说明 成员变量 说明

DATABITS_5 数据位为5 STOPBITS_2 停止位为2 PARITY_ODD 奇检验

DATABITS_6 数据位为6 STOPBITS_1 停止位为1 PARITY_MARK 标记检验

DATABITS_7 数据位为7 STOPBITS_1_5 停止为1.5 PARITY_NONE 空格检验

DATABITS_8 数据位为8 PARITY_EVEN 偶检验 PARITY_SPACE 无检验

三、实例

(1)打开、关闭串口

首先使用CommPortIdentifier中的方法,获取可用的端口,并且选择一个端口打开作为通信端口。

A:枚举可用端口

1void listPortChoices()
2{
3CommPortIdentifier portId;
4Enumeration en = CommPortIdentifier.getPortIdentifiers();
5while (en.hasMoreElements())
6{
7portId = (CommPortIdentifier) en.nextElement();
8if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
9System.out.println(portId.getName());
10}
11portChoice.select(parameters.getPortName());
12}
13 
14 
B:打开指定的端口
1CommPortIdentifier portId;
2try
3{
4//生成CommPortIdentifier类的实例
5portId = CommPortIdentifier.getPortIdentifier("COM4");
6}
7catch (NoSuchPortException e)
8{
9e.printStackTrace();
10}
11try
12{
13//打开串口COM4,设置超时时限为3000毫秒
14serialPort = (SerialPort) portId.open("testApp", 3000);
15}
16catch (PortInUseException e)
17{
18e.printStackTrace();
19}
20 
21 
C:关闭端口

使用完的端口,必须记得将其关闭,否则其它的程序将无法使用该端口,CommPortIdentifier类只提供了开启端口的方法,而要关闭端口,则要调用CommPort类的close()方法。

serialPort.close()

(2)设置串口参数

1try {
2try {
3serialPort.setSerialPortParams(9600, // 波特率
4SerialPort.DATABITS_8,// 数据位数
5SerialPort.STOPBITS_1, // 停止位
6SerialPort.PARITY_NONE);// 奇偶位
7}
8catch (UnsupportedCommOperationException e)
9{
10e.printStackTrace();
11}
12 

(3)串口的读、写

A:向串口写数据
1OutputStream outputStream;
2try
3{
4outputStream = serialPort.getOutputStream();
5}
6catch (IOException e)
7{
8e.printStackTrace();
9}
10bt = new byte[] { (byte) 0x55, (byte) 0xAA, (byte) 0xF1 };
11try
12{
13outputStream.write(bt);
14outputStream.flush();
15outputStream.close();
16}
17catch (IOException e)
18{
19e.printStackTrace();
20}
21 
B:读取串口中的数据

读操作,需继承SerialPortEventListener接口。为SerialPort添加监听Listener。实现该接口的serialEvent (SerialPortEvent event)方法。

1public class SerialRead implements SerialPortEventListener{
2
3InputStream inputStream;
4byte[] rBuffer = new byte[38];
5SerialRead(SerialPort serialPort)
6{
7try {
8serialPort.addEventListener((SerialPortEventListener) this);
9this.serialPort.notifyOnDataAvailable(true);
10
11} catch (TooManyListenersException e) {
12}
13
14try {
15if (serialPort != null)
16inputStream = serialPort.getInputStream();
17} catch (IOException e) {
18}
19}
20
21public void serialEvent(SerialPortEvent event)
22{
23if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE)
24{
25int count = 0;
26try
27{
28while ((ch = inputStream.read()) != -1)
29{
30bt[count++]=(byte)ch;
31}
32}
33catch (IOException e)
34{
35e.printStackTrace();
36}
37}
38}
39 

四、实例分析

同API一起下载的还有一个examples文件,里面有个最简单的读、写程序,对其进行注释,以增进了理。

(1)读串口

1import java.io.*;
2import java.util.*;
3import javax.comm.*;
4 
5public class SimpleRead implements Runnable, SerialPortEventListener {
6 
7static CommPortIdentifier portId;
8static Enumeration portList;//枚举类
9 
10InputStream inputStream;
11SerialPort serialPort;
12Thread readThread;
13 
14public static void main(String[] args) {
15 
16portList = CommPortIdentifier.getPortIdentifiers();/*不带参数的getPortIdentifiers方法获得一个枚举对象,该对象又包含了系统中管理每个端口的CommPortIdentifier对象。注意这里的端口不仅仅是指串口,也包括并口。这个方法还可以带参数。getPortIdentifiers(CommPort)获得与已经被应用程序打开的端口相对应的CommPortIdentifier对象。 getPortIdentifier(String portName)获取指定端口名(比如“COM1”)的CommPortIdentifier对象。*/
17 
18while (portList.hasMoreElements()) {
19portId = (CommPortIdentifier) portList.nextElement();
20if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)/*getPortType方法返回端口类型*/ {
21// if (portId.getName().equals("COM1"))/* 找Windows下的第一个串口*/ {
22if (portId.getName().equals("/dev/term/a"))/*找Unix-like系统下的第一个串口*/ {
23SimpleRead reader = new SimpleRead();
24}
25}
26}
27}
28 
29public SimpleRead() {
30try {
31serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);/* open方法打开通讯端口,获得一个CommPort对象。它使程序独占端口。如果端口正被其他应用程序占用,将使用 CommPortOwnershipListener事件机制,传递一个PORT_OWNERSHIP_REQUESTED事件。每个端口都关联一个 InputStream 何一个OutputStream。如果端口是用open方法打开的,那么任何的getInputStream都将返回相同的数据流对象,除非有close 被调用。有两个参数,第一个为应用程序名;第二个参数是在端口打开时阻塞等待的毫秒数。*/
32} catch (PortInUseException e) {}
33try {
34inputStream = serialPort.getInputStream();/*获取端口的输入流对象*/
35} catch (IOException e) {}
36try {
37serialPort.addEventListener(this);/*注册一个SerialPortEventListener事件来监听串口事件*/
38} catch (TooManyListenersException e) {}
39 
40serialPort.notifyOnDataAvailable(true);/*数据可用*/
41 
42try {
43serialPort.setSerialPortParams(9600,
44SerialPort.DATABITS_8,
45SerialPort.STOPBITS_1,
46SerialPort.PARITY_NONE);/*设置串口初始化参数,依次是波特率,数据位,停止位和校验*/
47} catch (UnsupportedCommOperationException e) {}
48 
49readThread = new Thread(this);
50readThread.start();
51}
52 
53public void run() {
54try {
55Thread.sleep(20000);
56} catch (InterruptedException e) {}
57}
58 
59//串口事件
60public void serialEvent(SerialPortEvent event) {
61 
62switch(event.getEventType()) {
63case SerialPortEvent.BI:/*Break interrupt,通讯中断*/
64case SerialPortEvent.OE:/*Overrun error,溢位错误*/
65case SerialPortEvent.FE:/*Framing error,传帧错误*/
66case SerialPortEvent.PE:/*Parity error,校验错误*/
67case SerialPortEvent.CD:/*Carrier detect,载波检测*/
68case SerialPortEvent.CTS:/*Clear to send,清除发送*/
69case SerialPortEvent.DSR:/*Data set ready,数据设备就绪*/
70case SerialPortEvent.RI:/*Ring indicator,响铃指示*/
71case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空*/
72break;
73 
74case SerialPortEvent.DATA_AVAILABLE:/*Data available at the serial port,端口有可用数据。读到缓冲数组,输出到终端*/
75byte[] readBuffer = new byte[20];
76 
77try {
78while (inputStream.available() > 0) {
79int numBytes = inputStream.read(readBuffer);
80}
81System.out.print(new String(readBuffer));
82} catch (IOException e) {}
83break;
84}
85}
86}
87 
(2)写串口
1import java.io.*;
2import java.util.*;
3import javax.comm.*;
4 
5public class SimpleWrite {
6static Enumeration portList;
7static CommPortIdentifier portId;
8static String messageString = "Hello, world!/n";
9static SerialPort serialPort;
10static OutputStream outputStream;
11 
12public static void main(String[] args) {
13portList = CommPortIdentifier.getPortIdentifiers();
14 
15while (portList.hasMoreElements()) {
16portId = (CommPortIdentifier) portList.nextElement();
17if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
18// if (portId.getName().equals("COM1")) {
19if (portId.getName().equals("/dev/term/a")) {
20try {
21serialPort = (SerialPort)
22portId.open("SimpleWriteApp", 2000);
23} catch (PortInUseException e) {}
24try {
25outputStream = serialPort.getOutputStream();
26} catch (IOException e) {}
27try {
28serialPort.setSerialPortParams(9600,
29SerialPort.DATABITS_8,
30SerialPort.STOPBITS_1,
31SerialPort.PARITY_NONE);
32} catch (UnsupportedCommOperationException e) {}
33try {
34outputStream.write(messageString.getBytes());
35} catch (IOException e) {}
36}
37}
38}
39}
40}
41 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值