放飞你的梦想--翱翔我的天空 | 会员登陆 | 繁體中文 | 站点地图 | 站长博客
 | 首页 | 文章中心 | IT 前沿 | 清怡画风 | 视频教程 | 资源下载 | 精彩图库 | 博客文赏 | 天空论坛 | 访客留言 | 音乐版 | 
    本站全新推出IT 前沿频道欢迎大家访问 地址 http://it.tkbbs.com  [风雪残士  2005年12月9日]            本站推出新浪VIVI收藏夹服务,欢迎使用  [风雪残士  2005年10月18日]        
您现在的位置: 翱翔翼站 >> 文章中心 >> 编程开发 >> 编程语言 >> java编程 >> 文章正文 今天是:
利用Java实现串口全双工通讯 【字体:
作 者:佚名 文章来源:网络 更新:2006-6-29 点击:






本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程

一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程。

本类库主要包括:SerialBean.java(与其他应用程序的接口),SerialBuffer.java(用来保存从串口所接收数据的缓冲区),ReadSerial.java(从串口读取数据的程序)。另外本类库还提供了一个例程SerialExample.java作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。

1.SerialBean

SerialBean是本类库与其他应用程序的接口。该类库中定义了SerialBean的构造方法以及初始化串口,从串口读取数据,往串口写入数据以及关闭串口的函数。具体介绍如下:

publicSerialBean(intPortID)

本函数构造一个指向特定串口的SerialBean,该串口由参数PortID所指定。PortID=1表示COM1,PortID=2表示COM2,由此类推。

publicintInitialize()

本函数初始化所指定的串口并返回初始化结果。如果初始化成功返回1,否则返回-1。初始化的结果是该串口被SerialBean独占性使用,其参数被设置为9600,N,8,1。如果串口被成功初始化,则打开一个进程读取从串口传入的数据并将其保存在缓冲区中。

publicStringReadPort(intLength)

本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。

publicvoidWritePort(StringMsg)

本函数向串口发送一个字符串。参数Msg是需要发送的字符串。

publicvoidClosePort()

本函数停止串口检测进程并关闭串口。

SerialBean的源代码如下:

packageserial;

importjava.io.*;

importjava.util.*;

importjavax.comm.*;

/**

*

*Thisbeanprovidessomebasicfunctionstoimplementfulldulplex

*informationexchangethroughthesrialport.

*

*/

publicclassSerialBean

{

staticStringPortName;

CommPortIdentifierportId;

SerialPortserialPort;

staticOutputStreamout;

staticInputStreamin;

SerialBufferSB;

ReadSerialRT;

/**

*

*Constructor

*

*@paramPortIDtheIDoftheserialtobeused.1forCOM1,

*2forCOM2,etc.

*

*/

publicSerialBean(intPortID)

{

PortName="COM"+PortID;

}

/**

*

*Thisfunctioninitializetheserialportforcommunication.Itstartsa

*threadwhichconsistentlymonitorstheserialport.Anysignalcaptured

*fromtheserialportisstoredintoabufferarea.

*

*/

publicintInitialize()

{

intInitSuccess=1;

intInitFail=-1;

try

{

portId=CommPortIdentifier.getPortIdentifier(PortName);

try

{

serialPort=(SerialPort)

portId.open("Serial_Communication",2000);

}catch(PortInUseExceptione)

{

returnInitFail;

}

//UseInputStreamintoreadfromtheserialport,andOutputStream

//outtowritetotheserialport.

try

{

in=serialPort.getInputStream();

out=serialPort.getOutputStream();

}catch(IOExceptione)

{

returnInitFail;

}

//Initializethecommunicationparametersto9600,8,1,none.

try

{

serialPort.setSerialPortParams(9600,

SerialPort.DATABITS_8,

SerialPort.STOPBITS_1,

SerialPort.PARITY_NONE);

}catch(UnsupportedCommOperationExceptione)

{

returnInitFail;

}

}catch(NoSuchPortExceptione)

{

returnInitFail;

}

//whensuccessfullyopentheserialport,createanewserialbuffer,

//thencreateathreadthatconsistentlyacceptsincomingsignalsfrom

//theserialport.Incomingsignalsarestoredintheserialbuffer.

SB=newSerialBuffer();

RT=newReadSerial(SB,in);

RT.start();

//returnsuccessinformation

returnInitSuccess;

}

/**

*

*Thisfunctionreturnsastringwithacertainlengthfromtheincoming

*messages.

*

*@paramLengthThelengthofthestringtobereturned.

*

*/

publicStringReadPort(intLength)

{

StringMsg;

Msg=SB.GetMsg(Length);

returnMsg;

}

/**

*

*Thisfunctionsendsamessagethroughtheserialport.

*

*@paramMsgThestringtobesent.

*

*/

publicvoidWritePort(StringMsg)

{

intc;

try

{

for(inti=0;i<Msg.length();i++)

out.write(Msg.charAt(i));

}catch(IOExceptione){}

}

/**

*

*Thisfunctionclosestheserialportinuse.

*

*/

publicvoidClosePort()

{

RT.stop();

serialPort.close();

}

}

2.SerialBuffer

SerialBuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲区中读取数据所需要的函数。

publicsynchronizedStringGetMsg(intLength)

本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。

publicsynchronizedvoidPutChar(intc)

[1] [2] 下一页  

文章录入:风雪残士    责任编辑:风雪残士 
  • 上一篇文章:

  • 下一篇文章: 没有了
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    相关文章
    没有相关文章
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    最新文章 TOP10
    最新热门 TOP10
    最新推荐TOP10
    翱翔翼站拥有本站所有版权! Copyright © 2005 - 2008 5-IT.COM
    本站维护 :风雪残士

    浙ICP备05039908号
    努力打造国内最全的电脑技术资料库