导航

Java 串口通讯

Posted on 2013-07-22 09:37  寒宵飞飞  阅读(618)  评论(0)    收藏  举报

         关于串口通讯,可参考http://blog.csdn.net/kabini/article/details/1601324

一、软件准备

1、虚拟串口工具: 

    工具名称:vspdconfig

    用途:虚拟出一对串口,例如com1-com2,并将两个串口连接起来,如果往其中一个串口写数据,另一个串口可以将数据读取出来

2、串口调试工具

    工具名称:CommAssistant.exe

    用途:可以往指定串口写入/读取数据

3、Java串口通讯类

     工具名称:rxtx-2.1-7-bins-r2

     使用方式:

       1)将rxtxSerial.dll拷贝到%java_home%/jre/bin,%java_home%为jdk安装目录,例如我的是D:\Program Files\Java\jdk\jre\bin
       2)将RXTXcomm.jar加入java工程

 

二、示例:

 SerialPortCommunication:

package GPS;
import gnu.io.*;
import java.io.*;
import java.util.*;

public class SerialPortCommunication 
{
    public static void main(String[] args) 
    {
        CommPortIdentifier portID = null;
        SerialPort port=null;
        InputStream in=null;
        
        try 
        {     
            //获取电脑所有串口信息
            getPorts();
            
            //CommPortIdentifier取得
            portID = CommPortIdentifier.getPortIdentifier("COM1");
          
            //打开串口
            port = (SerialPort)portID.open("portApp", 5000);
            
            //设置串口属性
            port.setSerialPortParams(19200,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
             port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
             
             //得到输入流
             in = port.getInputStream();
             //得到输出流
             //OutputStream out = port.getOutputStream();
             
             int CurrentPackageCount = 0; //本次发送的数据包长度
            byte[] CurrentPackage = null; //本次发送的数据包信息
            String CurrentPackageText; //本次发送的数据包文本信息
            while(true)
            {
                CurrentPackageCount = 0;
                while (CurrentPackageCount == 0) //如果Count不为0,说明有新的数据包进来
                {
                     CurrentPackageCount = in.available();
                }
                CurrentPackage = new byte[CurrentPackageCount];
                in.read(CurrentPackage, 0, CurrentPackageCount);
                CurrentPackageText  =   new String(CurrentPackage, "GB2312");
                //输出当前获取的信息
                System.out.println(CurrentPackageText); 
            }
        } 
        catch(Exception e)
        {
             e.printStackTrace();
             try   
             {
                 //关闭当前port
                 port.close();
                 in.close();
             }
             catch(Exception e1)
             {
                 
             }
        }    
    }
    
    //获取本机所有的port
    public static void getPorts()
    {
        CommPortIdentifier portId;  
        Enumeration en = CommPortIdentifier.getPortIdentifiers();  
        // iterate through the ports.  
        while (en.hasMoreElements()) {  
        portId = (CommPortIdentifier) en.nextElement();  
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {  
           System.out.println("port:"+portId.getName());  
        }  
        }  
    }
}

     运行SerialPortCommunication,就会监听com1端口获取的信息,利用CommAssistant.exe往com2发送信息,SerialPortCommunication就会将com1获取的信息打印出来。