如何关闭串口

config_man 2010-03-16 10:02:37
这几天一直在做java串口通信方面的程序。
遇到了些问题,碰到最多的问题是:
1、串口被占用【虽然程序写了关闭方法,但是没有效果】
2、发送指令给串口后,串口会有返回值,在读取返回值时会出现问题【调试时在用输入流读取返回值时,第一次没问题,第二次在count = inputstream.read()处出现问题,调试的那种箭头会消失的无影无踪】。

代码如下:

package com.test;

import java.io.*;
import java.util.*;
import javax.comm.*;

/**
* @author zcj
* 写串口的例程
*/

public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;

//static String messageString = "aa00E10000000055";
static String messageString = "aa00db0101000055";//测试红外
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;

/**
* 方法过程如下:
* 通过调用CommPortIdentifier端口标识对象的getPortIdentifiers()方法获得CommPortIdentifier的枚举对象Enumeration
* 然后迭代此枚举,获得具体的某一个CommPortIdentifier对象,如果此对象的端口与系统内定的端口相等,并且此对象的名称为COM1,
* 那么首先获得用此端口标识对象打开通讯端口,获得端口对象,将此端口对象强制转换成串口对象。
* 获得此串口对象以后,就可以获得其输出流。先设置串口对象的参数,然后就可以输入自己想要输入串口对象的字符串对象了。
* @param args
*/

public static void main(String[] args) {
/* 获得CommPortIdentifier对象的Enumeration对象 */
portList = CommPortIdentifier.getPortIdentifiers();

byte[] bt = new byte[8];
bt[0] = (byte)0xaa;
bt[1] = (byte)0x00;
bt[2] = (byte)0xdb;
bt[3] = (byte)0x01;//开启1号灯,开启2~4号灯为:0x02~0x04
bt[4] = (byte)0x01; //0x01【启动】 、0x00【关闭】
bt[5] = (byte)0x00;
bt[6] = (byte)0x00;
bt[7] = (byte)0x55;

/* 迭代此枚举对象*/
while (portList.hasMoreElements()) {
/* 获得一个CommPortIdentifier对象 */
portId = (CommPortIdentifier) portList.nextElement();

/**
* 如果端口标识对象的端口为1
* 备注:
* CommPortIdentifier.PORT_SERIAL = 1
* 端口包括:串口、并口。
*/
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000);

//打开输出流,写入命令
outputStream = serialPort.getOutputStream();
outputStream.write(bt);

//获得输入流,根据输入流获得返回值
inputStream = serialPort.getInputStream();

int count = 0;
StringBuffer sb = new StringBuffer();
while((count=inputStream.read())!=-1){
if('\r' == (char)count){/* '\r'为Enter键 */

}else{
sb.append((char)count);
}
count = 0;
}
System.out.println("结果:"+sb.toString());
} catch (Exception e) {
System.out.println("异常出现:"+e.getMessage());
} finally{
System.out.println("finally...");

//关闭流和端口
try {
inputStream.close();
} catch (IOException e1) {
System.out.println("关闭输入流时异常出现:"+e1.getMessage());
}
try {
outputStream.close();
} catch (IOException e) {
System.out.println("关闭输出流时异常出现:"+e.getMessage());
}
try{
serialPort.close();
}catch(Exception e){
System.out.println("关闭端口时异常出现:"+e.getMessage());
}
}
}
}
}
}
}
...全文
1056 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
azhu8345 2010-06-23
  • 打赏
  • 举报
回复
你这没有关闭。当然报异常了。
你打开串口的方法也不对,应当用
portId=CommPortIdentifier.getPortIdentifier(portName);
/**下面是你用的**/
serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000);
/**上面是你用的**/


另外还要关闭函数要加:
serialPort.close();
closePort(serialPort);


awusoft 2010-03-17
  • 打赏
  • 举报
回复
我路过的....
bayougeng 2010-03-16
  • 打赏
  • 举报
回复
如果没有用多线程的话,从任务管理器里看看,是否还有java程序在运行。
config_man 2010-03-16
  • 打赏
  • 举报
回复
程序总是出现上面的我描述的那2个问题,每次遇到串口被占用时,我都不得不重启电脑,为此我重启了N次。
因此希望各位朋友能帮忙将那2个问题解决掉。

PS:之前在网上查看了好多相关资料。有的说是线程问题,我的并没有用线程方法,所以应该没有关系吧;有的说是内存泄露,不知道是不是在读取返回值的时候出现了内存泄露,这个我不太懂。

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧