使用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 ...
随机推荐
- 第7月第27天 c++11 boost
1. #include <boost/asio.hpp> #include <cstdlib> #include <memory> namespace asio = ...
- HTTP::UserAgent注意问题
例用 HTTP::Request 设置头信息时, 比如 add-content , 第二次再执行 add-content 时, content 内容会追加, 并不会重新添加. 当下次再 add-con ...
- Debian安装Nvidia最简单方法
电脑配置: Dell本本 i7+gtx1050+8g 安装bumblebee: sudo apt install bumblebee-nvidia primus 以上会自动安装nvidia驱动. bu ...
- windows下使用pip安装python模块lxml
pip install lxml 1 1 会有如下问题: 结果一路解决下去,解决了一个坑还是有一个坑,遂放弃,查找有没有别的解决办法. 亲测使用wheel+pip可以成功安装lxml! wheel本 ...
- 『记录』Android参考资料
欢迎留言推荐好的教程.资料.博客及作者等. 『记录』Android参考资料 1.前期环境 Android Studio使用Git Android Studio快捷键总结 Android Studio及 ...
- Flask源码解析:Flask应用执行流程及原理
WSGI WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述服务器端如何与web应用程序通信的 ...
- 基于 Apache 在本地配置多个虚拟主机
如何使用 Apache 在本地配置出多个虚拟主机呢?而且使用不同的“域名”来访问本地不同的站点呢? 一般情况下,咱们都使用 localhost 来访问本机上的服务器,在我们的 C:/WINDOWS/s ...
- testng运行失败,继续执行
1.重写断言类 public class Verify { public static StringBuffer verificationErrors= new StringBuffer();; pu ...
- axios笔记
参考:http://www.cnblogs.com/Upton/p/6180512.html https://cloud.tencent.com/developer/article/1098141 ...
- linux下安装redis及其中遇到的问题的解决方法
1.将下载好的压缩包放到/usr/local目录下# tar xzf redis-3.0.2.tar.gz # cd redis-3.0.2 # make 提示错误 make: cc: Command ...