zoukankan      html  css  js  c++  java
  • Java串口编程

    最近由于项目的需要,需要用到java串口和windows端java程序的通讯,笔者也是刚刚接触串口这一模块,在网上搜索了很多的串口编程实例之类的,几乎前篇一律吧,但是串口通讯之前的配置是非常重要的,如果配置没有成功,编程也显得没有意义。串口编程主要有两种接口,第一种是利用sun提供的comm.jar包,这种方式比较古老了,这个包也没有更新。第二种就是RXTX模式,这种模式其实和comm.jar包的模式几乎是一样的。下面就记下我学习和使用此模块的记录。RXTX资源包,网上有很多,但要注意的是看你的电脑配置,如果是win64位系统的,则要下载RXTX 64位的资源包,否则会出错(这里最主要的区别就是rxtxSerial.dll文件不一样)。

    (1)编程环境的搭建和配置

    本文是基于RXTX(提供串口和并口通信)开源类库对串口进行操作的。使用准备(这里的JAVA_HOME是我的jdk的安装路径)

    1.将RXTXcomm.jar放到%JAVA_HOME%jrelibext下,如:E:jdkJava1.8jdk1.8.0_45jrelibext,或者项目1.右键->2.Preperties(首选项)->3.Java Build Path->4.Libraries->5.Add External JARs引入(建议使用后面一种,前面一种有时候在项目中读取不出来,在项目打包的过程中如果没有将jre打包进去的话,项目移植到别的设备上时候是不会成功的)

    2.把 rxtxSerial.dll放入到%JAVA_HOME%jrein中,如:E:jdkJava1.8jdk1.8.0_45jrein,或C:windowssystem32

    (2)RXTX介绍

    RXTX是一个提供串口和并口通信的开源java类库,由该项目发布的文件均遵循LGPL协议。

    RXTX项目提供了Windows,Linux,Mac os X,Solaris操作系统下的兼容javax.comm串口通讯包API的实现,为其他开发人员在此类系统下开发串口应用提供了相当的方便。

    RXTX的使用上与sun提供的comm.jar基本相同,编程时最明显的不同是要包含的包名由javax.comm.*改成了gnu.io.*

    RxtxAPI 的核心是抽象的CommPort类(用于描述一个被底层系统支持的端口的抽象类,它包含一些高层的IO控制方法,这些方法对于所有不同的通讯端口来说是通用的)及其两个子类:SerialPort类和ParallePort类。其中,SerialPort类是用于串口通信的类,ParallePort类是用于并行口通信的类。CommPort类还提供了常规的通信模式和方法,例如:getInputStream( )方法和getOutputStream( )方法,专用于与端口上的设备进行通信。

    然而,这些类的构造方法都被有意的设置为非公有的(non-public)。所以,不能直接构造对象,而是先通过静态的CommPortIdentifer.getPortIdentifiers()获得端口列表,再从这个端口列表中选择所需要的端口,并调用CommPortIdentifer对象的Open( )方法,这样,就能得到一个CommPort对象。当然,还要将这个CommPort对象的类型转换为某个非抽象的子类,表明是特定的通讯设备,该子类可以是SerialPort类和ParallePort类中的一个。下面将分别对CommPortIdentifier类,串口类SerialPort进行详细的介绍。

    接口

    CommDriver可负载设备(the loadable device)驱动程序接口的一部分

    CommPortOwnershipListener传递各种通讯端口的所有权事件

    ParallelPortEventListener传递并行端口事件

    SerialPortEventListener传递串行端口事件

    CommPort通讯端口

    CommPortIdentifier通讯端口管理

    ParallelPort并行通讯端口

    ParallelPortEvent并行端口事件

    SerialPortRS-232串行通讯端口

    SerialPortEvent 串行端口事件

    异常类

    NoSuchPortException当驱动程序不能找到指定端口时抛出

    PortInUseException当碰到指定端口正在使用中时抛出

    UnsupportedCommOperationException驱动程序不允许指定操作时抛出

    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) 打开端口,两个参数:程序名称,延迟时间(毫秒数)

    SerialPort类

    这个类用于描述一个RS-232串行通信端口的底层接口,它定义了串口通信所需的最小功能集。通过它,用户可以直接对串口进行读、写及设置工作。

    SerialPort类中关于串口参数的静态成员变量说明:

    DATABITS_5 数据位为5

    DATABITS_6 数据位为6

    DATABITS_7 数据位为7

    DATABITS_8 数据位为8

    PARITY_NONE 空格检验

    PARITY_ODD 奇检验

    PARITY_EVEN 偶检验

    PARITY_MARK 标记检验

    PARITY_SPACE 无检验

    STOPBITS_1 停止位为1

    STOPBITS_2 停止位为2

    STOPBITS_1_5 停止位为1.5

     

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

    (3)列出串口以及其它通信端口类以及方法,下面贴出demo

    import java.util.Enumeration;
    import java.util.HashSet;
    
    import gnu.io.CommPort;
    import gnu.io.CommPortIdentifier;
    import gnu.io.PortInUseException;
    
    public class ListPort {
    	/**
    	 * @Description:列出所有可用串口
    	 * @author:dengchaoqun
    	 * @date:2015-8-29 上午11:34:04
    	 */
    	public static void listPorts() {
    		HashSet<CommPortIdentifier> portSet = getAvailableSerialPorts();
    		for (CommPortIdentifier comm : portSet) {
    			System.out.println(comm.getName() + " - " + getPortTypeName(comm.getPortType()));
    		}
    	}
    
    	/**
    	 * @Description:列出所有通信端口
    	 * @author:dengchaqun
    	 * @date:2015-8-29 下午2:06:17
    	 */
    	@SuppressWarnings("unchecked")
    	public static void listCommPorts() {
    		CommPortIdentifier.getPortIdentifiers();
    		/*
    		 * 不带参数的getPortIdentifiers方法可以获得一个枚举对象,该对象包含了
    		 * 系统中每个端口的CommPortIdentifier对象。注意这里的端口不仅仅是指串口,也包括并口。
    		 * 这个方法还可以带参数,getPortIdentifiers(CommPort)获得已经被应用程序打开的端口
    		 * 相对应的CommPortIdentifier对象。getPortIdentifier(String portName)
    		 * 获取指定端口名(比如“COM1”)的CommPortIdentifier对象。
    		 */
    		java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
    		while (portEnum.hasMoreElements()) {
    			CommPortIdentifier portIdentifier = portEnum.nextElement();
    			System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()));
    		}
    	}
    
    	/**
    	 * @Description:获取通信端口类型名称
    	 * @author:Lu
    	 * @date:2015-8-29 上午11:35:32
    	 */
    	public static String getPortTypeName(int portType) {
    		switch (portType) {
    		case CommPortIdentifier.PORT_I2C:
    			return "I2C";
    		case CommPortIdentifier.PORT_PARALLEL: // 并口
    			return "Parallel";
    		case CommPortIdentifier.PORT_RAW:
    			return "Raw";
    		case CommPortIdentifier.PORT_RS485: // RS485端口
    			return "RS485";
    		case CommPortIdentifier.PORT_SERIAL: // 串口
    			return "Serial";
    		default:
    			return "unknown type";
    		}
    	}
    
    	/**
    	 * @Description:获取所有可用的串口集合
    	 * @author:dengchaoqun
    	 * @date:2015-8-29 上午11:37:54
    	 */
    	@SuppressWarnings("unchecked")
    	public static HashSet<CommPortIdentifier> getAvailableSerialPorts() {
    		HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>();
    		Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
    		while (portList.hasMoreElements()) {
    			CommPortIdentifier com = (CommPortIdentifier) portList.nextElement();
    			switch (com.getPortType()) {
    			case CommPortIdentifier.PORT_SERIAL:
    				try {
    					// open:(应用程序名【随意命名】,阻塞时等待的毫秒数)
    					/*
    					 * open方法打开通讯端口,获得一个CommPort对象,它使程序独占端口。
    					 * 如果端口正被其他应用程序占用,将使用CommPortOwnershipListener事件机制
    					 * 传递一个PORT_OWNERSHIP_REQUESTED事件。
    					 * 每个端口都关联一个InputStream和一个OutputStream,如果端口是用
    					 * open方法打开的,那么任何的getInputStream都将返回相同的数据流对象,除非 有close被调用。
    					 */
    					CommPort thePort = com.open(Object.class.getSimpleName(), 50);
    					thePort.close();
    					h.add(com);
    				} catch (PortInUseException e) {
    					// 不可用串口
    					System.out.println("Port, " + com.getName() + ", is in use.");
    				} catch (Exception e) {
    					System.err.println("Failed to open port " + com.getName());
    					e.printStackTrace();
    				}
    			}
    		}
    		return h;
    	}
    
    	public static void main(String[] args) {
    		/**
    		 * 可列出当前系统所有可用的串口名称,本机输出COM1 - Serial, COM2 - Serial.....COM6 - Serial
    		 */
    		listPorts();
    		listCommPorts();
    	}
    }
    

      

    (4)调试串口输入输出demo

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Date;
    import java.util.Enumeration;
    import java.util.TooManyListenersException;
    
    import gnu.io.CommPortIdentifier;
    import gnu.io.PortInUseException;
    import gnu.io.SerialPort;
    import gnu.io.SerialPortEvent;
    import gnu.io.SerialPortEventListener;
    import gnu.io.UnsupportedCommOperationException;
    
    public class SerialPortTest1 implements Runnable, SerialPortEventListener {
    	// 检测系统中可用的通讯端口类
    	private CommPortIdentifier portId;
    	// 枚举类型
    	private Enumeration<CommPortIdentifier> portList;
    
    	// RS232串口
    	private SerialPort serialPort;
    
    	// 输入输出流
    	private InputStream inputStream;
    	private OutputStream outputStream;
    
    	// 保存串口返回信息
    	private String test = "";
    
    	// 单例创建
    	private static SerialPortTest1 uniqueInstance = new SerialPortTest1();
    
    	// 初始化串口
    	@SuppressWarnings("unchecked")
    	public void init() {
    		// 获取系统中所有的通讯端口
    		portList = CommPortIdentifier.getPortIdentifiers();
    		// 循环通讯端口
    		while (portList.hasMoreElements()) {
    			portId = portList.nextElement();
    			// 判断是否是串口
    			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
    				// 比较串口名称是否是"COM3"
    				if ("COM2".equals(portId.getName())) {
    					System.out.println("找到串口COM2");
    					// 打开串口
    					try {
    						// open:(应用程序名【随意命名】,阻塞时等待的毫秒数)
    						serialPort = (SerialPort) portId.open(Object.class.getSimpleName(), 2000);
    						System.out.println("获取到串口对象,COM2");
    						//实例化输入流
    						inputStream = serialPort.getInputStream();
    						// 设置串口监听
    						serialPort.addEventListener(this);
    						// 设置串口数据时间有效(可监听)
    						serialPort.notifyOnDataAvailable(true);
    						// 设置串口通讯参数
    						// 波特率,数据位,停止位和校验方式
    						// 波特率2400,偶校验
    						serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, //
    								SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    
    					} catch (PortInUseException e) {
    						e.printStackTrace();
    					} catch (TooManyListenersException e) {
    						e.printStackTrace();
    					} catch (UnsupportedCommOperationException e) {
    						e.printStackTrace();
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    
    				}
    			}
    		}
    	}
    
    	// 实现接口SerialPortEventListener中的方法 读取从串口中接收的数据
    	@Override
    	public void serialEvent(SerialPortEvent event) {
    		switch (event.getEventType()) {
    		case SerialPortEvent.BI: // 通讯中断
    		case SerialPortEvent.OE: // 溢位错误
    		case SerialPortEvent.FE: // 帧错误
    		case SerialPortEvent.PE: // 奇偶校验错误
    		case SerialPortEvent.CD: // 载波检测
    		case SerialPortEvent.CTS: // 清除发送
    		case SerialPortEvent.DSR: // 数据设备准备好
    		case SerialPortEvent.RI: // 响铃侦测
    		case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // 输出缓冲区已清空
    			break;
    		case SerialPortEvent.DATA_AVAILABLE: // 有数据到达
    			readComm();
    			break;
    		default:
    			break;
    		}
    	}
    
    	// 读取串口返回信息
    	public void readComm() {
    		byte[] readBuffer = new byte[1024];
    		try {
    			inputStream = serialPort.getInputStream();
    			// 从线路上读取数据流
    			int len = 0;
    			while ((len = inputStream.read(readBuffer)) != -1) {
    				System.out.println("实时反馈:" + new String(readBuffer, 0, len).trim() + new Date());
    				test += new String(readBuffer, 0, len).trim();
    				break;
    			}
    			System.out.println(test + " ");
    			//closeSerialPort();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	public void closeSerialPort() {
    		uniqueInstance.serialPort.close();
    	}
    	
    	//向串口发送数据
    	public void sendMsg(){
    		String information = "AT
    ";
    		try {
    			//实例化输出流
    			outputStream = serialPort.getOutputStream();
    		} catch (IOException e1) {
    			e1.printStackTrace();
    		}
    		try {
    			outputStream.write(information.getBytes());
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	@Override
    	public void run() {
    		init();
    		sendMsg();
    		
    	}
    }
    

    (5)测试上面输入输出demo

    public class TestDemo {
    
    	public static void main(String[] args) {
    
    		Thread thread=new Thread(new SerialPortTest1());
    		thread.start();
    	}
    
    }
    

    串口测试的效果如图所示

    (6)后面的就是将此demo应用到项目中去,实际情况,根据自己的需求来定

  • 相关阅读:
    win10下查看进程,杀死进程
    Pycharm,debug调试时怎样带参数
    struts2,登录功能模块实现
    struts2处理.do后缀的请求
    struts2 修改action的后缀
    j2ee中如何拦截jsp页面?
    4个好用的JS联动选择插件
    css position:absolute 如何居中对齐
    使用jquery插件报错:TypeError:$.browser is undefined的解决方法
    phpcms v9后台多表查询分页代码
  • 原文地址:https://www.cnblogs.com/deng-c-q/p/5407450.html
Copyright © 2011-2022 走看看