zoukankan      html  css  js  c++  java
  • 基于java的串口通讯(附带实例+说明文档+测试工具)

    在步入正题前,发个牢骚。

    前天总公司的一个技术经理下达一个任务,实现java程序与串口的通信。半天做出来了(见附件),经理看了,在电话里说是直接从网上下载的,颇为不屑。

    说实话,当时真TM火大!虽然现在我还没有毕业,但是我也敢说我有实战经验!

    我承认是下载了网上的一个例子,也从java的官方网站中copy了一些他们的代码,但是,绝对没有全部的copy,很多都是这几年实践下来的经验,还有文档说明也是自己实践出来的,关于串口测试的过程也是全程截图。

    我敢说,现在项目中,不可能全部是自己的东西,如果什么都想要自己的,那还做什么飞机。创新也要从别人的基础上建立。

    当然,这只是小弟的个人见解。



    好吧,步入正题。


    完整的代码+工具打包下载:点击打开链接


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

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


    串口通信类如下:

    package org.serial;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Enumeration;
    import java.util.TooManyListenersException;
    
    import javax.comm.CommPortIdentifier;
    import javax.comm.PortInUseException;
    import javax.comm.SerialPort;
    import javax.comm.SerialPortEvent;
    import javax.comm.SerialPortEventListener;
    
    /**
     * @项目名称 :illegalsms
     * @文件名称 :SerialPort.java
     * @所在包 :org.serial
     * @功能描述 :
     *	串口类
     * @创建者 :集成显卡	1053214511@qq.com
     * @创建日期 :2012-9-13
     * @修改记录 :
     */
    public class DSerialPort implements Runnable, SerialPortEventListener {
    
    	private String appName = "串口通讯测试[集成显卡2012]";
    	private int timeout = 2000;//open 端口时的等待时间
    	private int threadTime = 0;
    	
    	private CommPortIdentifier commPort;
    	private SerialPort serialPort;
    	private InputStream inputStream;
    	private OutputStream outputStream;
    	
    	/**
    	 * @方法名称 :listPort
    	 * @功能描述 :列出所有可用的串口
    	 * @返回值类型 :void
    	 */
    	@SuppressWarnings("rawtypes")
    	public void listPort(){
    		CommPortIdentifier cpid;
    		Enumeration en = CommPortIdentifier.getPortIdentifiers();
    		
    		System.out.println("now to list all Port of this PC:" +en);
    		
    		while(en.hasMoreElements()){
    			cpid = (CommPortIdentifier)en.nextElement();
    			if(cpid.getPortType() == CommPortIdentifier.PORT_SERIAL){
    				System.out.println(cpid.getName() + ", " + cpid.getCurrentOwner());
    			}
    		}
    	}
    	
    	
    	/**
    	 * @方法名称 :selectPort
    	 * @功能描述 :选择一个端口,比如:COM1
    	 * @返回值类型 :void
    	 *	@param portName
    	 */
    	@SuppressWarnings("rawtypes")
    	public void selectPort(String portName){
    		
    		this.commPort = null;
    		CommPortIdentifier cpid;
    		Enumeration en = CommPortIdentifier.getPortIdentifiers();
    		
    		while(en.hasMoreElements()){
    			cpid = (CommPortIdentifier)en.nextElement();
    			if(cpid.getPortType() == CommPortIdentifier.PORT_SERIAL
    					&& cpid.getName().equals(portName)){
    				this.commPort = cpid;
    				break;
    			}
    		}
    		
    		openPort();
    	}
    	
    	/**
    	 * @方法名称 :openPort
    	 * @功能描述 :打开SerialPort
    	 * @返回值类型 :void
    	 */
    	private void openPort(){
    		if(commPort == null)
    			log(String.format("无法找到名字为'%1$s'的串口!", commPort.getName()));
    		else{
    			log("端口选择成功,当前端口:"+commPort.getName()+",现在实例化 SerialPort:");
    			
    			try{
    				serialPort = (SerialPort)commPort.open(appName, timeout);
    				log("实例 SerialPort 成功!");
    			}catch(PortInUseException e){
    				throw new RuntimeException(String.format("端口'%1$s'正在使用中!", 
    						commPort.getName()));
    			}
    		}
    	}
    	
    	/**
    	 * @方法名称 :checkPort
    	 * @功能描述 :检查端口是否正确连接
    	 * @返回值类型 :void
    	 */
    	private void checkPort(){
    		if(commPort == null)
    			throw new RuntimeException("没有选择端口,请使用 " +
    					"selectPort(String portName) 方法选择端口");
    		
    		if(serialPort == null){
    			throw new RuntimeException("SerialPort 对象无效!");
    		}
    	}
    	
    	/**
    	 * @方法名称 :write
    	 * @功能描述 :向端口发送数据,请在调用此方法前 先选择端口,并确定SerialPort正常打开!
    	 * @返回值类型 :void
    	 *	@param message
    	 */
    	public void write(String message) {
    		checkPort();
    		
    		try{
    			outputStream = new BufferedOutputStream(serialPort.getOutputStream());
    		}catch(IOException e){
    			throw new RuntimeException("获取端口的OutputStream出错:"+e.getMessage());
    		}
    		
    		try{
    			outputStream.write(message.getBytes());
    			log("信息发送成功!");
    		}catch(IOException e){
    			throw new RuntimeException("向端口发送信息时出错:"+e.getMessage());
    		}finally{
    			try{
    				outputStream.close();
    			}catch(Exception e){
    			}
    		}
    	}
    	
    	/**
    	 * @方法名称 :startRead
    	 * @功能描述 :开始监听从端口中接收的数据
    	 * @返回值类型 :void
    	 *	@param time  监听程序的存活时间,单位为秒,0 则是一直监听
    	 */
    	public void startRead(int time){
    		checkPort();
    		
    		try{
    			inputStream = new BufferedInputStream(serialPort.getInputStream());
    		}catch(IOException e){
    			throw new RuntimeException("获取端口的InputStream出错:"+e.getMessage());
    		}
    		
    		try{
    			serialPort.addEventListener(this);
    		}catch(TooManyListenersException e){
    			throw new RuntimeException(e.getMessage());
    		}
    		
    		serialPort.notifyOnDataAvailable(true);
    		
    		log(String.format("开始监听来自'%1$s'的数据--------------", commPort.getName()));
    		if(time > 0){
    			this.threadTime = time*1000;
    			Thread t = new Thread(this);
    			t.start();
    			log(String.format("监听程序将在%1$d秒后关闭。。。。", threadTime));
    		}
    	}
    	
    	
    	/**
    	 * @方法名称 :close
    	 * @功能描述 :关闭 SerialPort
    	 * @返回值类型 :void
    	 */
    	public void close(){
    		serialPort.close();
    		serialPort = null;
    		commPort = null;
    	}
    	
    	
    	public void log(String msg){
    		System.out.println(appName+" --> "+msg);
    	}
    
    
    	/**
    	 * 数据接收的监听处理函数
    	 */
    	@Override
    	public void serialEvent(SerialPortEvent arg0) {
    		switch(arg0.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[1024];
                String readStr="";
                String s2 = "";
                
                try {
                    
                	while (inputStream.available() > 0) {
                        inputStream.read(readBuffer);
                        readStr += new String(readBuffer).trim();
                    }
                    
                	s2 = new String(readBuffer).trim();
                	
    	            log("接收到端口返回数据(长度为"+readStr.length()+"):"+readStr);
    	            log(s2);
                } catch (IOException e) {
                }
    		}
    	}
    
    
    	@Override
    	public void run() {
    		try{
    			Thread.sleep(threadTime);
    			serialPort.close();
    			log(String.format("端口''监听关闭了!", commPort.getName()));
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    }


    运行测试:

    public static void main(String[] args) {
    		
    		DSerialPort sp = new DSerialPort();
    		
    		sp.listPort();
    		
    		sp.selectPort(PORT_NAME);
    		sp.write("210.36.16.166");
    		sp.write("2");
    		sp.startRead(120);
    	}

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


    效果如下:

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

    这个是和COM2 连通的

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


    2.运行java的main程序:


    COM3中收到了信息:


    从COM3中得到数据:




    完整的代码+工具打包下载:点击打开链接


  • 相关阅读:
    利用本地浏览器远程服务器上的jupyter notebook
    解决IIS服务器Web访问提示输入密码
    IIS 配置
    override new 关键字的区别
    ASP.NET的网站的设计与优化
    山东人!
    远程连接SQL Server 2000服务器的解决方案
    对软件的新认识
    一个程序员成长的六个阶段
    优秀程序员应当具备的品质
  • 原文地址:https://www.cnblogs.com/nerve/p/3185439.html
Copyright © 2011-2022 走看看