45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:Communications API知识简介

Communications API知识简介

2016-09-03 14:12:33 来源:www.45fan.com 【

Communications API知识简介

Communications API 的核心是抽象的CommPort类及其两个子类:SerialPort类和ParallePort类。其中,SerialPort类是用于串口通信的类,ParallePort类是用于并行口通信的类。CommPort类还提供了常规的通信模式和方法,例如: getInputStream( )方法和getOutputStream( )方法,专用于与端口上的设备进行通信。
然而,这些类的构造方法都被有意的设置为非公有的(non-public )。所以,不能直接构造对象,而是先通过静态的CommPortIdentifer.getPortIdentifiers( )获得端口列表;再从这个端口列表中选择所需要的端口,并调用CommPortIdentifer对象的Open( )方法,这样,就能得到一个CommPort对象。当然,还要将这个CommPort对象的类型转换为某个非抽象的子类,表明是特定的通讯设备。该子类可以是SerialPort类和ParallePort类中的一个。下面将分别对CommPort 类,CommPortIdentifier类,串口类SerialPort进行详细的介绍。
1.2 CommPortIdentifier类
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) 打开端口,两个参数:程序名称,延迟时间(毫秒数)

1.3 SerialPort类
SerialPort关于串口参数的静态成员变量
成员变量 说明 成员变量 说明 成员变量 说明
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 无检验
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类型的输入流

这是小弟的毕业设计中串口通信部分内容。提出与大家分享。
实现串口通信
实现串口通信的通用方法主要有两种:一种就是事件驱动的方式。当端口准备好读写数据时,CommunicationsAPI就会通知应用程序。另一种是基于线程的方式。每个”方向”上都有自己的控制流程。在此采用了事件驱动的方法。一下为实现串口通信的类SerialBean类.
设置和打开串行口的步骤是这样的:
1. 获得名字和相应的CommPortIdentifier对象。
2. 调用CommPortIdentifier对象的Open方法;将CommPort对象赋给SerialPort对象,这里可能发生PortInUseException异常,必须捕获它,后显示异常信息”串口已被使用”。
3. 调用SerialPort对象的getOutputStream(),getInputStream()方法获得OutputStream类型的输出流和InputStream类型的输入流,可能发生IOException异常, 应捕获它,后显示异常信息”创建流失败”对话框通知用户 。
4. 调用SerialPort对象的setSerialPortParams( )方法设置串行通信的参数(例如:设置波特率、奇偶检验、停止位、数据位), 可能发生UnsupportedCommOperationException异常,捕获它显示异常信息对话框“串口参数设置失败”。

事件驱动方式的进行读操作如下:
1. 建立一个内部类commListener,实现SerialPortEventListener接口
2. 调用SerialPort对象的addEventListener(commListener) 方法将commListener添加事件监听器中
3. 调用SerialPort对象的notifyOnDataAvailable(true)方法,设置端口有数据事件有效,是应用程序能监听串口否是有数据产生,有就调用事件处理函数自定义的serialEvent(SerialPortEvent event)处理,将数据接收。如下为:serialEvent的流程图。
串口通信的原程序如下:
package sketchtry;
import java.io.*;
import javax.comm.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class SerialBean {
private SketchFrame frame;
protected SerialPort serialPort;
private static String PortName;
private static OutputStream out;
private static InputStream in;
private CommPortIdentifier portId;
private BufferedInputStream reader;
private String ReadString;
private int numBytes=0;
public SerialBean(SketchFrame frame,int PortID) {
this.frame=frame;
PortName="COM"+PortID;
}
public boolean Initialize(){
boolean InitSuccess=true;
boolean InitFail=false;
try{
portId=CommPortIdentifier.getPortIdentifier(PortName);
try{
serialPort=(SerialPort)portId.open("Serial_Communication", 2000);
}catch(PortInUseException e){
/*frame.theApp.getMessageDlg().MessageLabel.setText(PortName+"串口已被占用");
frame.theApp.getMessageDlg().show();*/
return InitFail;
}
try{
in=serialPort.getInputStream();
reader=new BufferedInputStream(in);
out=serialPort.getOutputStream();
}catch (IOException e){
/*frame.theApp.getMessageDlg().MessageLabel.setText("创建流失败");
frame.theApp.getMessageDlg().show();*/
return InitFail;
}
try{
serialPort.setSerialPortParams(4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}catch(UnsupportedCommOperationException e){
/*frame.theApp.getMessageDlg().MessageLabel.setText("串口设置参数失败");
frame.theApp.getMessageDlg().show();*/
return InitFail;
}
}catch (NoSuchPortException e){
return InitFail;
}
return InitSuccess;
}
public void ReadPort(){
try{
serialPort.addEventListener(new commListener());
}catch(Exception e){}
serialPort.notifyOnDataAvailable(true);
}
public void StopRead(){
try{
serialPort.removeEventListener();
}catch(Exception e){}
serialPort.notifyOnDataAvailable(false);
}
public void WritePort(String Msg){
try{
for(int i=0;i<Msg.length();i++)
out.write(Msg.charAt(i));
}catch(IOException e){
}
}
public void ClosePort(){
serialPort.close();
}

public class commListener implements SerialPortEventListener{
public void serialEvent(SerialPortEvent event) {
byte[] readBuffer=new byte[100];
if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE){
try {
while (in.available() > 0) {
numBytes = reader.read(readBuffer);
}
ReadString+= new String(readBuffer,0,numBytes);
//处理自己的字符串
}catch (IOException e) {}
}
}
}
}

 

本文地址:http://www.45fan.com/a/question/71781.html
Tags: 简介 API Communications
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部