java基础学习总结——java串口通信

来源:互联网 发布:淘宝专营店怎么申请 编辑:程序博客网 时间:2024/05/18 21:41

现在一般的电脑都没有串口端口的了,所以还是用虚拟的串口来做测试吧。

我们用 VSPD(Virtual Serial Port Driver) 这个软件建立两个虚拟串口,COM2和COM3,名字随便起,VSPD对虚拟串口的序号没有限制,理论上可以创建无数个。


串口通信类如下:

[java] view plain copy
  1. package org.serial;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.util.Enumeration;  
  9. import java.util.TooManyListenersException;  
  10.   
  11. import javax.comm.CommPortIdentifier;  
  12. import javax.comm.PortInUseException;  
  13. import javax.comm.SerialPort;  
  14. import javax.comm.SerialPortEvent;  
  15. import javax.comm.SerialPortEventListener;  
  16.   
  17. /** 
  18.  * @项目名称 :illegalsms 
  19.  * @文件名称 :SerialPort.java 
  20.  * @所在包 :org.serial 
  21.  * @功能描述 : 
  22.  *  串口类 
  23.  * @创建者 :集成显卡   1053214511@qq.com 
  24.  * @创建日期 :2012-9-13 
  25.  * @修改记录 : 
  26.  */  
  27. public class DSerialPort implements Runnable, SerialPortEventListener {  
  28.   
  29.     private String appName = "串口通讯测试[集成显卡2012]";  
  30.     private int timeout = 2000;//open 端口时的等待时间  
  31.     private int threadTime = 0;  
  32.       
  33.     private CommPortIdentifier commPort;  
  34.     private SerialPort serialPort;  
  35.     private InputStream inputStream;  
  36.     private OutputStream outputStream;  
  37.       
  38.     /** 
  39.      * @方法名称 :listPort 
  40.      * @功能描述 :列出所有可用的串口 
  41.      * @返回值类型 :void 
  42.      */  
  43.     @SuppressWarnings("rawtypes")  
  44.     public void listPort(){  
  45.         CommPortIdentifier cpid;  
  46.         Enumeration en = CommPortIdentifier.getPortIdentifiers();  
  47.           
  48.         System.out.println("now to list all Port of this PC:" +en);  
  49.           
  50.         while(en.hasMoreElements()){  
  51.             cpid = (CommPortIdentifier)en.nextElement();  
  52.             if(cpid.getPortType() == CommPortIdentifier.PORT_SERIAL){  
  53.                 System.out.println(cpid.getName() + ", " + cpid.getCurrentOwner());  
  54.             }  
  55.         }  
  56.     }  
  57.       
  58.       
  59.     /** 
  60.      * @方法名称 :selectPort 
  61.      * @功能描述 :选择一个端口,比如:COM1 
  62.      * @返回值类型 :void 
  63.      *  @param portName 
  64.      */  
  65.     @SuppressWarnings("rawtypes")  
  66.     public void selectPort(String portName){  
  67.           
  68.         this.commPort = null;  
  69.         CommPortIdentifier cpid;  
  70.         Enumeration en = CommPortIdentifier.getPortIdentifiers();  
  71.           
  72.         while(en.hasMoreElements()){  
  73.             cpid = (CommPortIdentifier)en.nextElement();  
  74.             if(cpid.getPortType() == CommPortIdentifier.PORT_SERIAL  
  75.                     && cpid.getName().equals(portName)){  
  76.                 this.commPort = cpid;  
  77.                 break;  
  78.             }  
  79.         }  
  80.           
  81.         openPort();  
  82.     }  
  83.       
  84.     /** 
  85.      * @方法名称 :openPort 
  86.      * @功能描述 :打开SerialPort 
  87.      * @返回值类型 :void 
  88.      */  
  89.     private void openPort(){  
  90.         if(commPort == null)  
  91.             log(String.format("无法找到名字为'%1$s'的串口!", commPort.getName()));  
  92.         else{  
  93.             log("端口选择成功,当前端口:"+commPort.getName()+",现在实例化 SerialPort:");  
  94.               
  95.             try{  
  96.                 serialPort = (SerialPort)commPort.open(appName, timeout);  
  97.                 log("实例 SerialPort 成功!");  
  98.             }catch(PortInUseException e){  
  99.                 throw new RuntimeException(String.format("端口'%1$s'正在使用中!",   
  100.                         commPort.getName()));  
  101.             }  
  102.         }  
  103.     }  
  104.       
  105.     /** 
  106.      * @方法名称 :checkPort 
  107.      * @功能描述 :检查端口是否正确连接 
  108.      * @返回值类型 :void 
  109.      */  
  110.     private void checkPort(){  
  111.         if(commPort == null)  
  112.             throw new RuntimeException("没有选择端口,请使用 " +  
  113.                     "selectPort(String portName) 方法选择端口");  
  114.           
  115.         if(serialPort == null){  
  116.             throw new RuntimeException("SerialPort 对象无效!");  
  117.         }  
  118.     }  
  119.       
  120.     /** 
  121.      * @方法名称 :write 
  122.      * @功能描述 :向端口发送数据,请在调用此方法前 先选择端口,并确定SerialPort正常打开! 
  123.      * @返回值类型 :void 
  124.      *  @param message 
  125.      */  
  126.     public void write(String message) {  
  127.         checkPort();  
  128.           
  129.         try{  
  130.             outputStream = new BufferedOutputStream(serialPort.getOutputStream());  
  131.         }catch(IOException e){  
  132.             throw new RuntimeException("获取端口的OutputStream出错:"+e.getMessage());  
  133.         }  
  134.           
  135.         try{  
  136.             outputStream.write(message.getBytes());  
  137.             log("信息发送成功!");  
  138.         }catch(IOException e){  
  139.             throw new RuntimeException("向端口发送信息时出错:"+e.getMessage());  
  140.         }finally{  
  141.             try{  
  142.                 outputStream.close();  
  143.             }catch(Exception e){  
  144.             }  
  145.         }  
  146.     }  
  147.       
  148.     /** 
  149.      * @方法名称 :startRead 
  150.      * @功能描述 :开始监听从端口中接收的数据 
  151.      * @返回值类型 :void 
  152.      *  @param time  监听程序的存活时间,单位为秒,0 则是一直监听 
  153.      */  
  154.     public void startRead(int time){  
  155.         checkPort();  
  156.           
  157.         try{  
  158.             inputStream = new BufferedInputStream(serialPort.getInputStream());  
  159.         }catch(IOException e){  
  160.             throw new RuntimeException("获取端口的InputStream出错:"+e.getMessage());  
  161.         }  
  162.           
  163.         try{  
  164.             serialPort.addEventListener(this);  
  165.         }catch(TooManyListenersException e){  
  166.             throw new RuntimeException(e.getMessage());  
  167.         }  
  168.           
  169.         serialPort.notifyOnDataAvailable(true);  
  170.           
  171.         log(String.format("开始监听来自'%1$s'的数据--------------", commPort.getName()));  
  172.         if(time > 0){  
  173.             this.threadTime = time*1000;  
  174.             Thread t = new Thread(this);  
  175.             t.start();  
  176.             log(String.format("监听程序将在%1$d秒后关闭。。。。", threadTime));  
  177.         }  
  178.     }  
  179.       
  180.       
  181.     /** 
  182.      * @方法名称 :close 
  183.      * @功能描述 :关闭 SerialPort 
  184.      * @返回值类型 :void 
  185.      */  
  186.     public void close(){  
  187.         serialPort.close();  
  188.         serialPort = null;  
  189.         commPort = null;  
  190.     }  
  191.       
  192.       
  193.     public void log(String msg){  
  194.         System.out.println(appName+" --> "+msg);  
  195.     }  
  196.   
  197.   
  198.     /** 
  199.      * 数据接收的监听处理函数 
  200.      */  
  201.     @Override  
  202.     public void serialEvent(SerialPortEvent arg0) {  
  203.         switch(arg0.getEventType()){  
  204.         case SerialPortEvent.BI:/*Break interrupt,通讯中断*/   
  205.         case SerialPortEvent.OE:/*Overrun error,溢位错误*/   
  206.         case SerialPortEvent.FE:/*Framing error,传帧错误*/  
  207.         case SerialPortEvent.PE:/*Parity error,校验错误*/  
  208.         case SerialPortEvent.CD:/*Carrier detect,载波检测*/  
  209.         case SerialPortEvent.CTS:/*Clear to send,清除发送*/   
  210.         case SerialPortEvent.DSR:/*Data set ready,数据设备就绪*/   
  211.         case SerialPortEvent.RI:/*Ring indicator,响铃指示*/  
  212.         case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空*/   
  213.             break;  
  214.         case SerialPortEvent.DATA_AVAILABLE:/*Data available at the serial port,端口有可用数据。读到缓冲数组,输出到终端*/  
  215.             byte[] readBuffer = new byte[1024];  
  216.             String readStr="";  
  217.             String s2 = "";  
  218.               
  219.             try {  
  220.                   
  221.                 while (inputStream.available() > 0) {  
  222.                     inputStream.read(readBuffer);  
  223.                     readStr += new String(readBuffer).trim();  
  224.                 }  
  225.                   
  226.                 s2 = new String(readBuffer).trim();  
  227.                   
  228.                 log("接收到端口返回数据(长度为"+readStr.length()+"):"+readStr);  
  229.                 log(s2);  
  230.             } catch (IOException e) {  
  231.             }  
  232.         }  
  233.     }  
  234.   
  235.   
  236.     @Override  
  237.     public void run() {  
  238.         try{  
  239.             Thread.sleep(threadTime);  
  240.             serialPort.close();  
  241.             log(String.format("端口''监听关闭了!", commPort.getName()));  
  242.         }catch(Exception e){  
  243.             e.printStackTrace();  
  244.         }  
  245.     }  
  246. }  


运行测试:

[java] view plain copy
  1. public static void main(String[] args) {  
  2.           
  3.         DSerialPort sp = new DSerialPort();  
  4.           
  5.         sp.listPort();  
  6.           
  7.         sp.selectPort(PORT_NAME);  
  8.         sp.write("210.36.16.166");  
  9.         sp.write("2");  
  10.         sp.startRead(120);  
  11.     }  

上面代码中的 PORT_NAME="COM2";


效果如下:

1.用 串口测试工具(附件中有) 打开COM3

这个是和COM2 连通的

如果在java程序中调用COM2,发送数据,那么COM3会收到,在COM3 中发送的数据,java可以监听到。


2.运行java的main程序:


COM3中收到了信息:


从COM3中得到数据:












java串口通信API说明
java串口通信 

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

  1.1Communications 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类型的输入流


四:实例

一:下载 
    需要到其官方主页http://java.sun.com/products/javacomm/下载这个API,目前的最新版本是3.0。不过可惜的是,Sun目前没有推出此API在Windows平台下的3.0版本,主页上列出的三个版本,分别是运行在x86和Sparc结构下的Solaris系统,以及x86下的Linux系统。要下载Windows版本只能去寻找较老的版本了。我所找到的2个网址是http://llk.media.mit.edu/projects/cricket/software/javaSerial.zip(两个文件夹里面有所需的3个文件),http://mdubuc.freeshell.org/Jolt/javacomm20-win32.zip和(完整的2.0版本,还有examples)。 
二:安装 
    这里的所谓安装就是把三个重要的文件放到指定的目录下。 
    将下载的文件解压缩后,在/javacomm20-win32/commapi目录下有必需的三个文件comm.jar,javax.comm. properties和win32comm.dll。将文件comm.jar拷贝到%JAVA_HOME%/jre/lib/ext;文件javax.comm. properties拷贝到%JAVA_HOME%/jre/lib; 文件win32comm.dll拷贝到%JAVA_HOME%/bin。注意%JAVA_HOME%是jdk的路径,而非jre。



首先看最简单的读、写程序。 
读串口的例程

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    //枚举类
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();
/*不带参数的getPortIdentifiers方法获得一个枚举对象,该对象又包含了系统中管理每个端口的CommPortIdentifier对象。
注意这里的端口不仅仅是指串口,也包括并口。这个方法还可以带参数。getPortIdentifiers(CommPort)获得与已经被应 
用程序打开的端口相对应的CommPortIdentifier对象。getPortIdentifier(String portName)获取指定端口名(比如“COM1”)
的CommPortIdentifier对象。*/ 
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            /*getPortType方法返回端口类型*/ 
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            /* 找Windows下的第一个串口*/
                if (portId.getName().equals("COM1")) {
            /*找Unix-like系统下的第一个串口*/
                //if (portId.getName().equals("/dev/term/a")) {
                    SimpleRead reader = new SimpleRead();
                }
            }
        }
    }

    public SimpleRead() {
        try {
/* open方法打开通讯端口,获得一个CommPort对象。它使程序独占端口。如果端口正被其他应用程序占用,将使用 
CommPortOwnershipListener事件机制,传递一个PORT_OWNERSHIP_REQUESTED事件。每个端口都关联一个 
InputStream和一个OutputStream。如果端口是用open方法打开的,那么任何的getInputStream都将返回 
相同的数据流对象,除非有close被调用。有两个参数,第一个为应用程序名;第二个参数是在端口打开 
时阻塞等待的毫秒数。 */ 
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {}
        try {
            /*获取端口的输入流对象*/ 
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {}
try {
        /*注册一个SerialPortEventListener事件来监听串口事件*/ 
            serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
        /*数据可用*/ 
        serialPort.notifyOnDataAvailable(true);
        try {
       /*设置串口初始化参数,依次是波特率,数据位,停止位和校验*/ 
            serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {}
        readThread = new Thread(this);
        readThread.start();
    }

    public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {}
    }
    //串口事件 
    public void serialEvent(SerialPortEvent event) {
        switch(event.getEventType()) {
        case SerialPortEvent.BI:/*Break interrupt,通讯中断*/ 
        case SerialPortEvent.OE:/*Overrun error,溢位错误*/ 
        case SerialPortEvent.FE:/*Framing error,传帧错误*/
        case SerialPortEvent.PE:/*Parity error,校验错误*/
        case SerialPortEvent.CD:/*Carrier detect,载波检测*/
        case SerialPortEvent.CTS:/*Clear to send,清除发送*/ 
        case SerialPortEvent.DSR:/*Data set ready,数据设备就绪*/ 
        case SerialPortEvent.RI:/*Ring indicator,响铃指示*/
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空*/ 
            break;
        case SerialPortEvent.DATA_AVAILABLE:/*Data available at the serial port,端口有可用数据。读到缓冲数组,输出到终端*/ 
            byte[] readBuffer = new byte[20];
            char[] readChar = new char[20];
            String readStr="";
            int numBytes=0;
            try {
                while (inputStream.available() > 0) {
                     numBytes= inputStream.read(readBuffer);
                }
               for(int iii=0;iii<numBytes;iii++){   
                 readStr=readStr + Byte.toString(readBuffer[iii]);                  
               }                      
             System.out.println(readStr.length());
             System.out.println(readStr);
            } catch (IOException e) {}                  
            break;            
        }
    }
}

写串口的例程 
import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleWrite {
    static Enumeration portList;
    static CommPortIdentifier portId;
    static String messageString = "Hello, world!/n";
    static SerialPort serialPort;
    static OutputStream outputStream;

    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM1")) {
                //if (portId.getName().equals("/dev/term/a")) {
                    try {
                        serialPort = (SerialPort)
                            portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try {
                        serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try {
                        outputStream.write(messageString.getBytes());
                    } catch (IOException e) {}
                }
            }
        }
    }
}

    上面两个例程都经过了简化,在打开端口,并且传输结束后没有关闭数据流和串口。在例程中我们看 
到CommPortIdentifier提供了打开通讯端口的方法open,但却没有相应关闭端口的方法,关闭端口需要调 
用javax.comm.CommPort类的close()。CommPort是这个包中的一个高级抽象,它定义了端口可作的各种事 
情:获取I/O数据流对象,控制缓冲区大小,调整输入的处理。





0 0
原创粉丝点击