JAVA 串口编程 (三)
四、实例分析
同API一起下载的还有一个examples文件,里面有个最简单的读、写程序,对其进行注释,以增进了理。
(1)读串口
1 | import java.io.*; |
2 | import java.util.*; |
3 | import javax.comm.*; |
4 | |
5 | public class SimpleRead implements Runnable, SerialPortEventListener { |
6 | |
7 | static CommPortIdentifier portId; |
8 | static Enumeration portList;//枚举类 |
9 | |
10 | InputStream inputStream; |
11 | SerialPort serialPort; |
12 | Thread readThread; |
13 | |
14 | public static void main(String[] args) { |
15 | |
16 | portList = CommPortIdentifier.getPortIdentifiers();/*不带参数的getPortIdentifiers方法获得一个枚举对象,该对象又包含了系统中管理每个端口的CommPortIdentifier对象。注意这里的端口不仅仅是指串口,也包括并口。这个方法还可以带参数。getPortIdentifiers(CommPort)获得与已经被应用程序打开的端口相对应的CommPortIdentifier对象。 getPortIdentifier(String portName)获取指定端口名(比如“COM1”)的CommPortIdentifier对象。*/ |
17 | |
18 | while (portList.hasMoreElements()) { |
19 | portId = (CommPortIdentifier) portList.nextElement(); |
20 | if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)/*getPortType方法返回端口类型*/ { |
21 | // if (portId.getName().equals("COM1"))/* 找Windows下的第一个串口*/ { |
22 | if (portId.getName().equals("/dev/term/a"))/*找Unix-like系统下的第一个串口*/ { |
23 | SimpleRead reader = new SimpleRead(); |
24 | } |
25 | } |
26 | } |
27 | } |
28 | |
29 | public SimpleRead() { |
30 | try { |
31 | serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);/* open方法打开通讯端口,获得一个CommPort对象。它使程序独占端口。如果端口正被其他应用程序占用,将使用 CommPortOwnershipListener事件机制,传递一个PORT_OWNERSHIP_REQUESTED事件。每个端口都关联一个 InputStream 何一个OutputStream。如果端口是用open方法打开的,那么任何的getInputStream都将返回相同的数据流对象,除非有close 被调用。有两个参数,第一个为应用程序名;第二个参数是在端口打开时阻塞等待的毫秒数。*/ |
32 | } catch (PortInUseException e) {} |
33 | try { |
34 | inputStream = serialPort.getInputStream();/*获取端口的输入流对象*/ |
35 | } catch (IOException e) {} |
36 | try { |
37 | serialPort.addEventListener(this);/*注册一个SerialPortEventListener事件来监听串口事件*/ |
38 | } catch (TooManyListenersException e) {} |
39 | |
40 | serialPort.notifyOnDataAvailable(true);/*数据可用*/ |
41 | |
42 | try { |
43 | serialPort.setSerialPortParams(9600, |
44 | SerialPort.DATABITS_8, |
45 | SerialPort.STOPBITS_1, |
46 | SerialPort.PARITY_NONE);/*设置串口初始化参数,依次是波特率,数据位,停止位和校验*/ |
47 | } catch (UnsupportedCommOperationException e) {} |
48 | |
49 | readThread = new Thread(this); |
50 | readThread.start(); |
51 | } |
52 | |
53 | public void run() { |
54 | try { |
55 | Thread.sleep(20000); |
56 | } catch (InterruptedException e) {} |
57 | } |
58 | |
59 | //串口事件 |
60 | public void serialEvent(SerialPortEvent event) { |
61 | |
62 | switch(event.getEventType()) { |
63 | case SerialPortEvent.BI:/*Break interrupt,通讯中断*/ |
64 | case SerialPortEvent.OE:/*Overrun error,溢位错误*/ |
65 | case SerialPortEvent.FE:/*Framing error,传帧错误*/ |
66 | case SerialPortEvent.PE:/*Parity error,校验错误*/ |
67 | case SerialPortEvent.CD:/*Carrier detect,载波检测*/ |
68 | case SerialPortEvent.CTS:/*Clear to send,清除发送*/ |
69 | case SerialPortEvent.DSR:/*Data set ready,数据设备就绪*/ |
70 | case SerialPortEvent.RI:/*Ring indicator,响铃指示*/ |
71 | case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空*/ |
72 | break; |
73 | |
74 | case SerialPortEvent.DATA_AVAILABLE:/*Data available at the serial port,端口有可用数据。读到缓冲数组,输出到终端*/ |
75 | byte[] readBuffer = new byte[20]; |
76 | |
77 | try { |
78 | while (inputStream.available() > 0) { |
79 | int numBytes = inputStream.read(readBuffer); |
80 | } |
81 | System.out.print(new String(readBuffer)); |
82 | } catch (IOException e) {} |
83 | break; |
84 | } |
85 | } |
86 | } |
87 |
(2)写串口
1 | import java.io.*; |
2 | import java.util.*; |
3 | import javax.comm.*; |
4 | |
5 | public class SimpleWrite { |
6 | static Enumeration portList; |
7 | static CommPortIdentifier portId; |
8 | static String messageString = "Hello, world!/n"; |
9 | static SerialPort serialPort; |
10 | static OutputStream outputStream; |
11 | |
12 | public static void main(String[] args) { |
13 | portList = CommPortIdentifier.getPortIdentifiers(); |
14 | |
15 | while (portList.hasMoreElements()) { |
16 | portId = (CommPortIdentifier) portList.nextElement(); |
17 | if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { |
18 | // if (portId.getName().equals("COM1")) { |
19 | if (portId.getName().equals("/dev/term/a")) { |
20 | try { |
21 | serialPort = (SerialPort) |
22 | portId.open("SimpleWriteApp", 2000); |
23 | } catch (PortInUseException e) {} |
24 | try { |
25 | outputStream = serialPort.getOutputStream(); |
26 | } catch (IOException e) {} |
27 | try { |
28 | serialPort.setSerialPortParams(9600, |
29 | SerialPort.DATABITS_8, |
30 | SerialPort.STOPBITS_1, |
31 | SerialPort.PARITY_NONE); |
32 | } catch (UnsupportedCommOperationException e) {} |
33 | try { |
34 | outputStream.write(messageString.getBytes()); |
35 | } catch (IOException e) {} |
36 | } |
37 | } |
38 | } |
39 | } |
40 | } |
41 |
JAVA 串口编程 (三)相关推荐
- java rxtx version_RXTX实现JAVA串口编程
给大家分项下用RXTX库实现JAVA串口编程. 一 准备工作 1.1 下载资源文件 首先下载RXTX库对应的资源文件.下载地址 http://fizzed.com/oss/rxtx-for-java ...
- RXTX实现JAVA串口编程
给大家分项下用RXTX库实现JAVA串口编程. 一 准备工作 1.1 下载资源文件 首先下载RXTX库对应的资源文件.下载地址 http://fizzed.com/oss/rxtx-for-java ...
- Linux 串口编程三 使用termios与API进行串口程序开发
在 termios 结构体以及内部终端控制标志中,并非所有的参数对于实际的物理串口都是有效的,在使用过程中也不需要对于所有标志的作用都有所理解.事实上,快速掌握一项技术的核心点也是一种学习能力.对于使 ...
- 进阶笔记——java并发编程三特性与volatile
欢迎关注专栏:Java架构技术进阶.里面有大量batj面试题集锦,还有各种技术分享,如有好文章也欢迎投稿哦.微信公众号:慕容千语的架构笔记.欢迎关注一起进步. 前言 前面讲过使用synchronize ...
- java串口编程-读取称重仪表中净重
一.需求说明 将仪表和计算机串口相连,计算机通过软件向仪表发送指令,然后仪表返回结果,在计算机软件界面上显示. 二.实现过程 1.查看仪表说明书 仪表型号为XK3190-A9,主要是查看相关参数(波特 ...
- Java——网络编程三要素
* A:计算机网络* 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统.网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统.* ...
- java闭锁_【Java并发编程三】闭锁
1.什么是闭锁? 闭锁(latch)是一种Synchronizer(Synchronizer:是一个对象,它根据本身的状态调节线程的控制流.常见类型的Synchronizer包括信号量.关卡和闭锁). ...
- java并发编程(三十五)——公平与非公平锁实战
前言 在 java并发编程(十六)--锁的七大分类及特点 一文中我们对锁有各个维度的分类,其中有一个维度是公平/非公平,本文我们来探讨下公平与非公平锁. 公平|非公平 首先,我们来看下什么是公平锁和非 ...
- java小编程----三数之和
package com.henu.lmd;import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; ...
最新文章
- 为什么jdk源码推荐ThreadLocal使用static
- arraylist从大到小排序_初学Python最简易入门之十四排序算法10对字典排序
- python考试pass or fail_python-pytest学习(十二)-标记失败xfail
- VirtualKD + VMWare双机调试(失败)
- 【通俗易懂】层次聚类算法的原理及实现
- Sort HDU5884(二分+多叉哈夫曼树)
- druid seata 配置_架构设计 | 基于Seata中间件,微服务模式下事务管理
- Android笔记 simpleAdapter demo
- Java 学习笔记(121208)
- Oracle 存储大文本
- 使用SQL Coalesce函数查询数据
- 网页制作的基本知识(标签)
- java检查文件的编码格式
- 《紫川》之远东战火 十二卷
- 华为手机wifi不显示连接到服务器,华为手机中无法连接WIFI处理方法
- 元气骑士怎么用电脑玩 元气骑士手游模拟器教程
- Android 11 正式版发布
- python 实现图片转卡通图
- IEduChina2019国际学校展暨国际教育论坛温暖深圳
- html中怎么做扇形菜单,纯CSS3实现扇形动画菜单(简化版)实例源码