`

Communications API

    博客分类:
  • J2SE
阅读更多
----声明,此文章是直接复制别人的东东,以便自己记录,并非原创。

     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.*;  
  /**  
    *   Title:  
  
    *   Description:  
  
    *   Copyright:   Copyright   (c)   2003
  
    *   Company:  
  
    *   @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)   {}  
          }  
      }  
    }  
  }  


------------------------javax.comm包配置管理
http://www.exam8.com/computer/djks/dj2/Java/ziliao/201008/1524526.html
分享到:
评论

相关推荐

    Multicore Communications API (MCAPI) Specification V2.015

    MCA官网上下的《Multicore Communication API 2.015 (MCAPI®) Specification》是当前(2012.11)最新版本.

    Java Communications API 3.0u1 - Linux

    The Java Communications API is a Java extension that provides access to a variety of communication protocols and interfaces for communication between applications and devices. Java Communications API ...

    Java Communications API 3.0u1 - Solaris SPARC

    The Java Communications API is a Java extension that provides access to a variety of communication protocols and interfaces for communication between applications and devices. Java Communications API...

    Java Communications API 3.0u1 - Solaris x86

    The Java Communications API is a Java extension that provides access to a variety of communication protocols and interfaces for communication between applications and devices. Java Communications API...

    Unified Communications API Map for 2007

    UC开发所有的API有8个之多,但是没有全用的,这个文档描述每个API的用处

    Unified Communications Managed API version 1.0

    provides technical information about the Unified Communications Managed API version 1.0 SDK. It is intended for application developers who are interested in creating and deploying SIP-based ...

    Java串口编程API包

    SUN的官方API(comm2.0.jar) 里面包含win32com.dll ,comm.jar ,javax.comm.properties

    java串口驱动,comm是专门为Java读取串口信息的而编写的API

    Java,comm是专门为Java读取串口信息的而编写的API,这个既可以读取到串口的信息,也可以进行相关数据的写入到对应的串口中。这个一般刚刚开始,使用简单的代码,进行读取的时候,可能会什么都没有发现,这个是应该,...

    sun 串口编程包 comm api

    sun 公司开发的Java Communications API ,在 xp 系统下面测试通过。适合需要用串口通讯的编程

    java-Comm-API.rar_comm

    java Communications API

    浅谈Java串行端口技术协议

    五、安装JAVA COMMUNICATIONS API 3 六、通讯前的准备 4 七、COMM API基础 4 1、枚举出系统所有的RS232端口 5 2、打开端口 6 2.1、CommPortIdentifier类和CommPort类有什么区别呢? 7 3、关闭端口 7 3.1、轮询方式...

    bc-bm-nodejs-api-explorer:此示例演示如何使用Business Communications API创建和管理Business Messages品牌,代理和位置

    商业通讯:API Explorer 此示例演示如何使用创建和管理Business Messages品牌,代理商和位置。 此应用程序假定您已经使用。 该示例已设置为在Google App Engine或本地运行。 有关更多详细说明,请参阅Google App ...

    microsoft-graph-comms-samples:Microsoft Graph Communications示例

    Microsoft Graph Communications API和示例 Microsoft Graph Communications API使开发人员能够以编程方式与Microsoft的Communications Platform(也为Microsoft Teams提供动力)进行交互,以创造令人惊叹的体验和...

    Sample Code for GSSAPI/Kerberos v5 SASL Mechanism

    Note that the Java Communications API is no longer officially supported by Oracle, and the last available version is 3.0u1. However, there are alternative libraries and frameworks available for ...

    统一通信托管API4.0核心的安装,故障提示解决办法

    有的朋友在部署有些应用服务...此计算机要求安装 Microsoft 统一通信托管 API 4.0 核心运行时 64 位。 着其实就是服务器系统又个组件没有安装,只需要去官网把它现在下来安装即可。 这里有个现成的教程,实测安装成功。

    java_rs232.rar_java rs2_java rs2_java rs232_rs232_rs232 java

    用JAVA写的RS232通讯软体,请记得开发环境需安装Java Communications API,才可执行

    java串口通信

    Java提供了 CommunicationAPI(包含于javax.comm包中)用于通过...Communications API,是标准的Java的扩展部分,它在JavaAPI中是没有附带的。因此,必须先在SUN公司网站的Java站点(www.java.sun.com)上下载这个扩展类库

    java实现串口

    Java提供了 CommunicationAPI(包含于javax.com包中)用于...Communications API,是标准的Java的扩展部分,它在JavaAPI中是没有附带的。因此,必须先在SUN公司网站的Java站点 (www.java.sun.com)上下载这个扩展类库。

    java串口通信控件

    Communications API,是标准的Java的扩展部分,它在JavaAPI中是没有附带的。因此,必须先在SUN公司网站的Java站点(www.java.sun.com)上下载这个扩展类库。 详见参考文档:java串口通信及例子程序.doc

    apiDebug.v.1.0.14.crx

    It might be used for testing other HTTP communications too. Support interface debugging (POST, GET), support JSON, XML and other parameters. If you want to save data to you own database, use the API ...

Global site tag (gtag.js) - Google Analytics