java 全双工串口,Java实现全双工串口通信
依据 《利用Java实现串口全双工通讯》蒋清野 更新。
更新1:修正其源代码返回的字符数比发送的字符数滞后的BUG
更新2:修正javax.comm的BUG。此BUG表现为 java BlackBox 总是返回 No serial ports found!
注:更新处标注 update by LZA
环境:Win7 + JDK1.7 + Java Communications API 2.0 (comm.jar)
ReadSerial.java
//package serial;
import java.io.*;
/**
*
* This class reads message from the specific serial port and save the message
* to the serial buffer.
*
*/
public class ReadSerial extends Thread {
private SerialBuffer ComBuffer;
private InputStream ComPort;
/**
*
* Constructor
*
* @param SB
* The buffer to save the incoming messages.
* @param Port
* The InputStream from the specific serial port.
*
*/
public ReadSerial(SerialBuffer SB, InputStream Port) {
ComBuffer = SB;
ComPort = Port;
}
public void run() {
int c;
try {
while (true) {
c = ComPort.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {
}
}
}
SerialBean.java
//package serial;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* This bean provides some basic functions to implement full dulplex information
* exchange through the srial port.
*
*/
public class SerialBean {
static String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
static OutputStream out;
static InputStream in;
SerialBuffer SB;
ReadSerial RT;
/**
*
* Constructor
*
* @param PortID
* the ID of the serial to be used. 1 for COM1, 2 for COM2, etc.
*
*/
public SerialBean(int PortID) {
PortName = "COM" + PortID;
}
/**
*
* This function initialize the serial port for communication. It startss a
* thread which consistently monitors the serial port. Any signal capturred
* from the serial port is stored into a buffer area.
*
*/
public int Initialize() {
int InitSuccess = 1;
int InitFail = -1;
try {
portId = CommPortIdentifier.getPortIdentifier(PortName);
try {
serialPort = (SerialPort) portId.open("Serial_Communication",
2000);
} catch (PortInUseException e) {
return InitFail;
}
// Use InputStream in to read from the serial port, and OutputStream
// out to write to the serial port.
try {
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e) {
return InitFail;
}
// Initialize the communication parameters to 9600, 8, 1, none.
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
return InitFail;
}
} catch (NoSuchPortException e) {
return InitFail;
}
// when successfully open the serial port, create a new serial buffer,
// then create a thread that consistently accepts incoming signals from
// the serial port. Incoming signals are stored in the serial buffer.
SB = new SerialBuffer();
RT = new ReadSerial(SB, in);
RT.start();
// return success information
return InitSuccess;
}
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length
* The length of the string to be returned.
*
*/
public String ReadPort(int Length) {
String Msg;
Msg = SB.GetMsg(Length);
return Msg;
}
/**
*
* This function sends a message through the serial port.
*
* @param Msg
* The string to be sent.
*
*/
public void WritePort(String Msg) {
int c;
try {
for (int i = 0; i < Msg.length(); i++)
out.write(Msg.charAt(i));
} catch (IOException e) {
}
}
/**
*
* This function closes the serial port in use.
*
*/
public void ClosePort() {
RT.stop();
serialPort.close();
}
}
SerialBuffer.java
//package serial;
/**
*
* This class implements the buffer area to store incoming data from the serial
* port.
*
*/
public class SerialBuffer {
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
private int LengthNeeded = 1;
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length
* The length of the string to be returned.
*
*/
public synchronized String GetMsg(int Length) {
LengthNeeded = Length;
notifyAll();
if (LengthNeeded > Content.length()) {
available = false;
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
CurrentMsg = Content.substring(0, LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();
return CurrentMsg;
}
/**
*
* This function stores a character captured from the serial port to the
* buffer area.
*
* @param t
* The char value of the character to be stored.
*
*/
public synchronized void PutChar(int c) {
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (LengthNeeded <= Content.length()) // update by LZA
{
available = true;
}
notifyAll();
}
}
SerialExample.java
import java.io.*;
import java.util.*;
import javax.comm.*;
import com.sun.comm.*;
class SerialExample {
public static void main(String[] args) {
// update by LZA
Win32Driver w32Driver = new Win32Driver();
// 这两行修正javax.comm的BUG
w32Driver.initialize();
// 这才正常初始化,才会正常找出所有port
// TO DO: Add your JAVA codes here
SerialBean SB = new SerialBean(1);
String Msg;
SB.Initialize();
for (int i = 5; i <= 10; i++) {
Msg = SB.ReadPort(i);
SB.WritePort("Reply: " + Msg);
}
SB.ClosePort();
}
}
java 全双工串口,Java实现全双工串口通信相关推荐
- java 蓝牙读取数据格式,单片机与安卓手机通过蓝牙串口模块利用JSON数据格式通信实例...
原标题:单片机与安卓手机通过蓝牙串口模块利用JSON数据格式通信实例 JSON 指的是 Java 对象表示法(Java Object Notation),JSON 是轻量级的文本数据交换格式,JSON ...
- Linux/windows com串口 java 接收数据 并解析 web程序
1.首先应公司要求再 com 口本来使用 .net 由于 .net 适用 linux 太麻烦 改为java 准备工作 准备 RXTXconmm.jar(版本很重要) 因为版本问题我搞了一天. 主要讲述 ...
- js串口 Java web串口 调用客户端的串口 串口上云 硬件上云
最新版本已经投入实际运行使用,demo版禁止未授权时投入商业使用 用Java串口开发,发现部署到服务器后连接不到本地的串口,蒙 于是准备用纯js调串口 发现兼容性不咋地 经过许多思考 我突然发现我会c ...
- 嵌入式--串口、RS232、RS485通信
一.串口通信 1.UART指的是通用异步收发器,是一种通用的串行.异步通信总线该总线有两条数据线,一条接收一条发送,可以实现全双工的发送和接收,在嵌入式开发过程中比较常用.异步通信没有使用同一个时钟, ...
- codesys 串口通讯实例_串口通信RS232的基本接法,原来这么简单,今天终于弄明白了...
目前较为常用的串口有9针串口(DB9)和25针串口(DB25),通信距离较近时(<12m),可以用电缆线直接连接标准RS232端口(RS422,RS485较远),若距离较远,需附加调制解调器(M ...
- 变频器怎么设置_如何利用串口调试软件与变频器通信?
西门子博途的全套视频教程下载! 西门子S7-1500内部培训PPT下载! 每一个做工控的都必须要会利用串口调试软件来和我们的外围设备来做通讯测试.今天我们就来学习怎么用串口调试软件控制台达的这款变 ...
- 串口通信模块3:串口通信编程基础(读写、关闭)
上一节总结了如何打开串口并讨论了如何配置串口,本节是在上一节的基础上,进一步讨论串口编程的基础--如何进行文件读写?如何关闭串口? 1. 读写串口 串口的读写操作和文件的读写操作是一样的,也是通过Re ...
- 串口通信模块2:串口通信编程基础(打开、配置)
有两种方式可以操作串口:同步操作方式和异步操作方式(即重叠操作方式).同步操作时,API函数会阻塞直到操作完成以后才能返回(在多线程方式中,虽然不会阻塞主线程,但是仍然会阻塞监听线程):而异步操作方式 ...
- java socket编程聊天室_Java Socket通信之聊天室功能
Java Socket通信之聊天室功能 发布时间:2020-10-17 14:36:00 来源:脚本之家 阅读:73 作者:LY_624 本文实例为大家分享了Java Socket聊天室功能的具体代码 ...
- Java Socket实现客户端服务端之间的通信
Java Socket Java Socket编程用于在不同JRE上运行的应用程序之间的通信. Java Socket编程可以是面向连接的或无连接的. Socket和ServerSocket类用于面向 ...
最新文章
- 趣谈网络协议笔记-二(第十七讲)
- 2010年基于Linux的10大技术趋势
- laravel框架总结(一) -- 请求和响应
- 牙齿间隙变大怎么办_牙齿之间的间隙越来越大怎么办?
- 从有理数到实数和数的连续体
- 纯ASP结合VML生成完美图-柱图
- 简述一下索引的匹配原则_【进阶之路】索引中一些易忽视的点
- DVWA 黑客攻防演练(七)Weak Session IDs
- vs2017 js cordova + dotnet core 开发app
- 用计算机模仿真实系统的技术叫,计算机模拟技术.pdf
- python setup_简述python setup.py install的过程
- 计算机科学与技术班级,学院计算机科学与技术学院专业班级计算机科学与技术.doc...
- Java读写csv文件操作
- Hyrax: Doubly-efficient zkSNARKs without trusted setup学习笔记
- Phalcon PHP 中文,Phalcon 入门
- kubernetes节点NotReady
- 怎样开发每天赚100万的微信小游戏?
- OpenCV+OCR文字识别
- 生活随记 - 春节快递延误的正确处理方式
- @EqualsAndHashCode(callSuper = true/false) 作用
热门文章
- 在html上运行asp,ASP在网页设计的作用
- 微软“玻璃硬盘”问世:2毫米杯垫大小可存储75.8G数据,1000年不坏!
- python怎么提取文件内容_python怎么提取出文件里的指定内容
- 说说react-custom-scrollbars插件在react hooks版本中的使用
- 如何调整计算机显卡,教你n卡发挥最大性能,电脑怎么设置独立显卡-
- 最新码支付源码+微信/支付宝/qq/秒挂支付/uid+三网监控+易支付H5接口 +聚合免签系统
- 克隆硬盘后进不去系统_硬盘克隆后重启找不到操作系统所在分区问题解决
- python 时间序列异常值_干货 :时间序列异常检测
- endnote找不到国标_实验差距惊人!揭秘雅迪高于新国标的品质标准测试
- Linux 创建用户角色并添加ssh登录权限