package com.yuan;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.TooManyListenersException;
public class SerialComm implements SerialPortEventListener, Runnable {
public final static String PORT_OWER = "MonitorApp";
private boolean isOpen;
private boolean isStart;
private boolean isSave;
private boolean isPrint;
private Thread readThread;
private String portName;
private String portAddress;
private CommPortIdentifier portId;
private SerialPort serialPort;
private DataInputStream inputStream;
private OutputStream outputStream;
private SimpleDateFormat formatter;
private String dataProtocol;
private Object readEWriteLock = new Object();
public SerialComm(){
this.isOpen = false;
this.isStart = false;
this.isSave = true;
this.isPrint = false;
formatter = new SimpleDateFormat("[yyyy-MM-dd hh:mm:ss,SSS]");
portName = "COM1";
portAddress = "LOCAL";
dataProtocol = "Gooseli";
}
public void init(String port,String protocol ) throws Exception{
this.portName = port;
this.portAddress = portName;
this.dataProtocol = protocol;
init();
}
public void init(String port,String address,String protocol) throws Exception{
this.portName = port;
this.portAddress = address;
this.dataProtocol = protocol;
}
public void init()throws IOException,Exception{
if(isOpen){
close();
}
try{
portId = CommPortIdentifier.getPortIdentifier(portName);
serialPort = (SerialPort)portId.open(PORT_OWER,2000);
inputStream = new DataInputStream(serialPort.getInputStream());
outputStream = serialPort.getOutputStream();
isOpen = true;
}catch(NoSuchPortException ex){
throw new Exception(ex.toString());
}
}
public void start()throws Exception{
if(!isOpen){
throw new Exception(portName + " has not bean opened!");
}
try{
readThread = new Thread(this);
readThread.start();
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(this);
isStart = true;
} catch( TooManyListenersException ex){
throw new Exception(ex.toString());
}
}
private void close() {
stop();
if(isOpen){
try{
inputStream.close();
outputStream.close();
serialPort.close();
isOpen = false;
}catch(IOException ex){
}
}
}
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();
/*byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
System.out.println(inputStream.available());
int numBytes = inputStream.read(readBuffer);
System.out.println(numBytes);
}
data = new String(readBuffer).trim();
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {
e.fillInStackTrace();
}*/
break;
default:break;
}
}
private void readComm() {
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
System.out.println(inputStream.available());
int numBytes = inputStream.read(readBuffer);
}
//data = new String(readBuffer).trim();
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {
e.fillInStackTrace();
}
/*StringBuffer readBuffer = new StringBuffer();
String scannedInput = "";
Date currentTime = null;
String TimeStamp = "";
int c ;
char a;
try {
InputStreamReader fis = new InputStreamReader(inputStream,"utf-8");
while((c=fis.read())!=-1){
readBuffer.append((char)c);
}
scannedInput = readBuffer.toString().trim();
currentTime = new Date();
TimeStamp = formatter.format(currentTime);
} catch(IOException ex ){
ex.printStackTrace();
} catch(Exception exx){
exx.printStackTrace();
}*/
}
public void run() {
/*while(true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String at = "test";
writeComm(at);
isPrint = true;
}*/
/*String at = "at^hcmgr=1\r";
String strTemp = at+(char)Integer.parseInt("la",16) + "z";
writeComm(strTemp);*/
}
private void writeComm(String outString) {
synchronized(readEWriteLock){
try{
outputStream.write(outString.getBytes());
} catch(IOException ex){
ex.printStackTrace();
}
}
}
public void stop(){
if(isStart){
serialPort.notifyOnDataAvailable(false);
serialPort.removeEventListener();
isStart =false;
}
}
public static void main(String[] args){
SerialComm serialComm = new SerialComm();
try {
serialComm.init("COM2","Air");
serialComm.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
附件libs.rar为响应jar和需要放在jre里面的dll文件
分享到:
相关推荐
这份代码是我在网上看到的,配置好你的comm,我在串口通信助手、串口模拟软件、eclipse上运行成功!
利用GUI图形化界面实现JAVA串口通信
java串口通信实例,经过测试使用Java实用源码整理learns
Java串口通信的实例,可实现打开串口、关闭串口、发送数据及接收数据(十六进制)可定时传输及数据库传输数据功能
java串口通信实例,仅供大家学习参考。Java串口,本实例大致工作流程是:将以下的所有java类都放在一个包下面,run as ---java application ,打开的是一个图形化界面,最上面的是一个输入文本框(也就是你要发射的...
Android Studio下的串口通信实例项目,已通过测试。可实现对于串口的设置、打开、发送、接收等操作
实现串口通信 提供所有需要的依赖包,可直接运行有UI设置
Java串口通信详解收藏1 串口通信简介... 1 1.1 常见的Java串口包... 1 1.2 串口包的安装(Windows下)... 1 2 串口API概览... 2 2.1 javax.comm.CommPort2 2.2 javax.comm.CommPortIdentifier3 2.3 javax.comm...
IT_计算机]Java串口通信详解(相互学习)
1.1 常见的Java串口包... 1 1.2 串口包的安装(Windows下)... 1 2 串口API概览... 2 2.1 javax.comm.CommPort2 2.2 javax.comm.CommPortIdentifier3 2.3 javax.comm.SerialPort3 2.4 串口API实例... 3 2.4.1...
JAVA串口通信实例,简明扼要。易于理解易于掌握。
java串口通讯及串口调试助手
主要介绍了java 串口通信详细及简单实例的相关资料,在开发硬件与软件结合的时候,就会用到串口,需要的朋友可以参考下
JAVA串口通讯 modbus
主要介绍了Java实现的串口通信功能,结合实例形式分析了java串口通信的具体操作步骤与相关注意事项,需要的朋友可以参考下
java com串口组件调用实例jajava com串口组件调用实例va com串口组件调用实例
所有源码,资料均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、...