基本信息
源码名称:java 串口调试助手 源码
源码大小:1.59M
文件格式:.zip
开发语言:Java
更新时间:2018-05-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 4 元×
微信扫码支付:4 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
简单的串口调试助手
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.TooManyListenersException;
public class Comm implements SerialPortEventListener, Runnable {
private InputStream is = null;
private OutputStream os =null;
private byte[] chars = new byte[64];
private SerialPort port = null;
public ArrayList listPortChoices(){
ArrayList list = new ArrayList();
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
while (en.hasMoreElements()) {
portId = (CommPortIdentifier)en.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
list.add(portId.getName());
}
}
return list;
}
public boolean openPort(String portName) throws TooManyListenersException{
boolean bl = false;
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);//获得串口管理对象
port = (SerialPort) portId.open(portName,3000);//打开并获得串口对象
port.setSerialPortParams(9600,8,1,0);//配置串口
is = port.getInputStream();//获得串口输入流
os = port.getOutputStream();//获得串口输出流
bl = true;
} catch (NoSuchPortException e) {
e.printStackTrace();
} catch (PortInUseException e) {
e.printStackTrace();
System.out.println("该串口已被其他程序占用");
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bl;
}
public boolean checkPort(){
if(port == null){
return true;
}else{
return false;
}
}
public void closePort(){
if (port != null) {
try {
os.close();
is.close();
} catch (IOException e) {
System.err.println(e);
}
}
port.removeEventListener();
port.close();
port = null;
}
public void write(byte[] out,int len){
try{
for(int i=0;i<len;i ){
os.write(out[i]);
os.flush();
}
return;
}catch(IOException e1){
System.out.println(e1);
return;
}catch(ArrayIndexOutOfBoundsException e2){
System.out.println(e2);
}
}
public void Read() throws TooManyListenersException{
try{
is = new BufferedInputStream(port.getInputStream());
}catch(IOException e){
throw new RuntimeException();
}
try{
port.addEventListener(this);
}catch(TooManyListenersException e){
throw new RuntimeException(e.getMessage());
}
port.notifyOnDataAvailable(true);
if(true){
Thread thread = new Thread(this);
thread.start();
}
}
@Override
public void serialEvent(SerialPortEvent event) {
// TODO Auto-generated method stub
switch (event.getEventType())
{
case SerialPortEvent.BI:/*Break interrupt,通讯中断*/
case SerialPortEvent.OE:/*Overrun error,溢位错误*/
case SerialPortEvent.FE:/*Framing error,传帧错误*/
case SerialPortEvent.PE:/*Parity error,校验错误*/
case SerialPortEvent.CD:/*Carrier detect,载波检测*/
case SerialPortEvent.CTS:/*Clear to send,清除发送*/
case SerialPortEvent.DSR:/*Data set ready,数据设备就绪*/
case SerialPortEvent.RI:/*Ring indicator,响铃指示*/
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空*/
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readbuffer = new byte[1024];
String readstr = null;
String s2 = null;
try{
while(is.available()>0){
is.read(readbuffer);
readstr = new String(readbuffer).trim();
}
s2 = new String(readbuffer).trim();
}catch(IOException e){
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
String temp = null;
int len = 0;
try {
len = is.read(chars);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
if(checkPort()){
while(len!=-1){
temp = new String(chars,0,len);
System.out.println(temp);
}
}else{
}
}
}
}
简单的串口调试助手
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.TooManyListenersException;
public class Comm implements SerialPortEventListener, Runnable {
private InputStream is = null;
private OutputStream os =null;
private byte[] chars = new byte[64];
private SerialPort port = null;
public ArrayList listPortChoices(){
ArrayList list = new ArrayList();
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
while (en.hasMoreElements()) {
portId = (CommPortIdentifier)en.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
list.add(portId.getName());
}
}
return list;
}
public boolean openPort(String portName) throws TooManyListenersException{
boolean bl = false;
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);//获得串口管理对象
port = (SerialPort) portId.open(portName,3000);//打开并获得串口对象
port.setSerialPortParams(9600,8,1,0);//配置串口
is = port.getInputStream();//获得串口输入流
os = port.getOutputStream();//获得串口输出流
bl = true;
} catch (NoSuchPortException e) {
e.printStackTrace();
} catch (PortInUseException e) {
e.printStackTrace();
System.out.println("该串口已被其他程序占用");
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bl;
}
public boolean checkPort(){
if(port == null){
return true;
}else{
return false;
}
}
public void closePort(){
if (port != null) {
try {
os.close();
is.close();
} catch (IOException e) {
System.err.println(e);
}
}
port.removeEventListener();
port.close();
port = null;
}
public void write(byte[] out,int len){
try{
for(int i=0;i<len;i ){
os.write(out[i]);
os.flush();
}
return;
}catch(IOException e1){
System.out.println(e1);
return;
}catch(ArrayIndexOutOfBoundsException e2){
System.out.println(e2);
}
}
public void Read() throws TooManyListenersException{
try{
is = new BufferedInputStream(port.getInputStream());
}catch(IOException e){
throw new RuntimeException();
}
try{
port.addEventListener(this);
}catch(TooManyListenersException e){
throw new RuntimeException(e.getMessage());
}
port.notifyOnDataAvailable(true);
if(true){
Thread thread = new Thread(this);
thread.start();
}
}
@Override
public void serialEvent(SerialPortEvent event) {
// TODO Auto-generated method stub
switch (event.getEventType())
{
case SerialPortEvent.BI:/*Break interrupt,通讯中断*/
case SerialPortEvent.OE:/*Overrun error,溢位错误*/
case SerialPortEvent.FE:/*Framing error,传帧错误*/
case SerialPortEvent.PE:/*Parity error,校验错误*/
case SerialPortEvent.CD:/*Carrier detect,载波检测*/
case SerialPortEvent.CTS:/*Clear to send,清除发送*/
case SerialPortEvent.DSR:/*Data set ready,数据设备就绪*/
case SerialPortEvent.RI:/*Ring indicator,响铃指示*/
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空*/
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readbuffer = new byte[1024];
String readstr = null;
String s2 = null;
try{
while(is.available()>0){
is.read(readbuffer);
readstr = new String(readbuffer).trim();
}
s2 = new String(readbuffer).trim();
}catch(IOException e){
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
String temp = null;
int len = 0;
try {
len = is.read(chars);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
if(checkPort()){
while(len!=-1){
temp = new String(chars,0,len);
System.out.println(temp);
}
}else{
}
}
}
}