使用Java进行串口SerialPort通讯
1.准备工作
2.JAVA程序的操作
- package com.cams.CaMSMobileService.SerialPort;
- import gnu.io.CommPort;
- import gnu.io.CommPortIdentifier;
- import gnu.io.NoSuchPortException;
- import gnu.io.PortInUseException;
- import gnu.io.SerialPort;
- import gnu.io.SerialPortEventListener;
- import gnu.io.UnsupportedCommOperationException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.TooManyListenersException;
- import com.cams.CaMSMobileService.SerialPort.exception.NoSuchPort;
- import com.cams.CaMSMobileService.SerialPort.exception.NotASerialPort;
- import com.cams.CaMSMobileService.SerialPort.exception.PortInUse;
- import com.cams.CaMSMobileService.SerialPort.exception.ReadDataFromSerialPortFailure;
- import com.cams.CaMSMobileService.SerialPort.exception.SendDataToSerialPortFailure;
- import com.cams.CaMSMobileService.SerialPort.exception.SerialPortInputStreamCloseFailure;
- import com.cams.CaMSMobileService.SerialPort.exception.SerialPortOutputStreamCloseFailure;
- import com.cams.CaMSMobileService.SerialPort.exception.SerialPortParameterFailure;
- import com.cams.CaMSMobileService.SerialPort.exception.TooManyListeners;
- public class SerialPortManager {
- /**
- * 查找所有可用端口
- *
- * @return 可用端口名称列表
- */
- @SuppressWarnings("unchecked")
- public static final ArrayList<String> findPort() {
- // 获得当前所有可用串口
- Enumeration<CommPortIdentifier> portList = CommPortIdentifier
- .getPortIdentifiers();
- ArrayList<String> portNameList = new ArrayList<String>();
- // 将可用串口名添加到List并返回该List
- while (portList.hasMoreElements()) {
- String portName = portList.nextElement().getName();
- portNameList.add(portName);
- }
- return portNameList;
- }
- /**
- * 打开串口
- *
- * @param portName
- * 端口名称
- * @param baudrate
- * 波特率
- * @return 串口对象
- * @throws SerialPortParameterFailure
- * 设置串口参数失败
- * @throws NotASerialPort
- * 端口指向设备不是串口类型
- * @throws NoSuchPort
- * 没有该端口对应的串口设备
- * @throws PortInUse
- * 端口已被占用
- */
- public static final SerialPort openPort(String portName, int baudrate)
- throws SerialPortParameterFailure, NotASerialPort, NoSuchPort,
- PortInUse {
- try {
- // 通过端口名识别端口
- CommPortIdentifier portIdentifier = CommPortIdentifier
- .getPortIdentifier(portName);
- // 打开端口,设置端口名与timeout(打开操作的超时时间)
- CommPort commPort = portIdentifier.open(portName, 2000);
- // 判断是不是串口
- if (commPort instanceof SerialPort) {
- SerialPort serialPort = (SerialPort) commPort;
- try {
- // 设置串口的波特率等参数
- serialPort.setSerialPortParams(baudrate,
- SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
- SerialPort.PARITY_NONE);
- } catch (UnsupportedCommOperationException e) {
- throw new SerialPortParameterFailure();
- }
- return serialPort;
- } else {
- // 不是串口
- throw new NotASerialPort();
- }
- } catch (NoSuchPortException e1) {
- throw new NoSuchPort();
- } catch (PortInUseException e2) {
- throw new PortInUse();
- }
- }
- /**
- * 关闭串口
- *
- * @param serialport
- * 待关闭的串口对象
- */
- public static void closePort(SerialPort serialPort) {
- if (serialPort != null) {
- serialPort.close();
- serialPort = null;
- }
- }
- /**
- * 向串口发送数据
- *
- * @param serialPort
- * 串口对象
- * @param order
- * 待发送数据
- * @throws SendDataToSerialPortFailure
- * 向串口发送数据失败
- * @throws SerialPortOutputStreamCloseFailure
- * 关闭串口对象的输出流出错
- */
- public static void sendToPort(SerialPort serialPort, byte[] order)
- throws SendDataToSerialPortFailure,
- SerialPortOutputStreamCloseFailure {
- OutputStream out = null;
- try {
- out = serialPort.getOutputStream();
- out.write(order);
- out.flush();
- } catch (IOException e) {
- throw new SendDataToSerialPortFailure();
- } finally {
- try {
- if (out != null) {
- out.close();
- out = null;
- }
- } catch (IOException e) {
- throw new SerialPortOutputStreamCloseFailure();
- }
- }
- }
- /**
- * 从串口读取数据
- *
- * @param serialPort
- * 当前已建立连接的SerialPort对象
- * @return 读取到的数据
- * @throws ReadDataFromSerialPortFailure
- * 从串口读取数据时出错
- * @throws SerialPortInputStreamCloseFailure
- * 关闭串口对象输入流出错
- */
- public static byte[] readFromPort(SerialPort serialPort)
- throws ReadDataFromSerialPortFailure,
- SerialPortInputStreamCloseFailure {
- InputStream in = null;
- byte[] bytes = null;
- try {
- in = serialPort.getInputStream();
- // 获取buffer里的数据长度
- int bufflenth = in.available();
- while (bufflenth != 0) {
- // 初始化byte数组为buffer中数据的长度
- bytes = new byte[bufflenth];
- in.read(bytes);
- bufflenth = in.available();
- }
- } catch (IOException e) {
- throw new ReadDataFromSerialPortFailure();
- } finally {
- try {
- if (in != null) {
- in.close();
- in = null;
- }
- } catch (IOException e) {
- throw new SerialPortInputStreamCloseFailure();
- }
- }
- return bytes;
- }
- /**
- * 添加监听器
- *
- * @param port
- * 串口对象
- * @param listener
- * 串口监听器
- * @throws TooManyListeners
- * 监听类对象过多
- */
- public static void addListener(SerialPort port,
- SerialPortEventListener listener) throws TooManyListeners {
- try {
- // 给串口添加监听器
- port.addEventListener(listener);
- // 设置当有数据到达时唤醒监听接收线程
- port.notifyOnDataAvailable(true);
- // 设置当通信中断时唤醒中断线程
- port.notifyOnBreakInterrupt(true);
- } catch (TooManyListenersException e) {
- throw new TooManyListeners();
- }
- }
- }
使用Java进行串口SerialPort通讯的更多相关文章
- java实现串口通讯
一. 准备工作 1. 点击此下载java串口通讯相关工具 2. 将RXTXcomm.jar放到 %JAVA_HOME%\jre\lib\ext\ 目录下,工程中引入该jar包 3. 将rxtxSe ...
- java读写串口数据
本博文参考自https://www.cnblogs.com/Dreamer-1/p/5523046.html 最近接触到了串口及其读写,在此记录java进行串口读写的过程. 1.导入串口支持包 需要下 ...
- Java实现串口通信的小样例
用Java实现串口通信(windows系统下),须要用到sun提供的串口包 javacomm20-win32.zip.当中要用到三个文件,配置例如以下: 1.comm.jar放置到 JAVA_HOME ...
- 用Java通过串口发送手机短信
用Java通过串口发短信其实很简单,因为有现成的类库供我们使用.有底层的类库,也有封装好一点的类库,下面我介绍一下在 Win32 平台下发送短信的方法. 如果你想用更底层的类库开发功能更强大的应用程序 ...
- 自制单片机之十七……PC与单片机RS-232串口的通讯和控制
这次我们来试着一步步的去掌握PC与单片机通过RS-232进行通讯和控制. 先说说我硬件的情况.我用的PC是个二手的IBM240小本本,十寸屏,赛扬400,机子很老了.但也有它的优点:1.串口,并口,P ...
- Java编写串口程序
用Java编写串口程序一般都会用到这个 http://fizzed.com/oss/rxtx-for-java 根据电脑的情况下载 解压以后有安装文档 For a JDK installation: ...
- java读取串口-mfz-rxtx-2.2-20081207-win-x86
1.下载jar包 RXTXcomm.jar 2.实现代码 package main; import java.awt.*; import java.awt.event.*; import java.i ...
- gRPC java 客户端,服务器端通讯使用json格式
使用 protobuf 作为通讯内容序列化的简单例子请看:http://www.cnblogs.com/ghj1976/p/5458176.html . 本文是使用 json 做为内容序列化的简单例子 ...
- c# 串口SerialPort
创建SerialPortFun类 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
随机推荐
- decimal模块
简介 decimal意思为十进制,这个模块提供了十进制浮点运算支持. 常用方法 1.可以传递给Decimal整型或者字符串参数,但不能是浮点数据,因为浮点数据本身就不准确. 2.要从浮点数据转换为De ...
- Number of Airplanes in the Sky
Given an interval list which are flying and landing time of the flight. How many airplanes are on th ...
- 学习mysql replication gitd
- linux backtrace()详细使用说明,分析Segmentation fault【转】
转自:http://velep.com/archives/1032.html 在此之前,开发eCos应用程序时,经常碰到程序挂掉后,串口打印输出一大串让人看不懂的数据.今天才明白,原来这些数据是程序挂 ...
- springmvc上下文与springcontext上下文的关系
内容摘自:springmvc与spring上下文的关系 原理区别 一直不清楚springmvc-servlet.xml配置与spring.xml两个配置文件出现的上下文关系.今天找到一上面的文章,倒是 ...
- java 内部类 工厂方法
用内部类实现工厂模式 :优先使用类而不是接口,如果你的设计中需要某个接口,你必须了解它,否则不到迫不得已,不要将其放到你的类中 //: innerclasses/Factories.java impo ...
- java 多重继承
接口不仅仅只是一种更纯粹形式的抽象类,它的目标比这更高,因为接口是根本没有任何具体实现的--也就是说,没有任何与接口相关的存储,因此也就无法阻止多个接口的组合, 在导出类中,不强制要求必须有一个抽象的 ...
- EFCore CodeFirst 适配数据库
EF6中可以直接根据代码模型生成数据库Database.SetInitializer即可 在EFCore中如何实现呢? 这项功能放在了DatabaseFacade对象中,传入数据库上下文对象实例化到一 ...
- 一步一步学习IdentityServer3 (3)
在上一篇中配置一个基础的idrserver服务端 这篇文章将对服务端做一些变化,这里我先贴一下上一章中的代码 证书: static class Certificate { public static ...
- 只想写一个真正能用的django mock
调参数的过程,百转千回. 还好,搞得差不多了. 确实,方便写测试用例, 也是一个开发水平高低的衡量~~~:( 为了测试这个mock,不得不改下代码~~ 还要不断的将Model里允许Null的参数写完, ...