我是用
File ComPort = new File("COM1");
try {
FileWriter out = new FileWriter(ComPort);
out.write(str);
out.close();
}
catch (Exception ex) {
ex.printStackTrace();
}这方法写数据进接在串口的设备的,当接上设备后,是没什么问题,但当没接设备时,每一次发送都让程序停顿大概一秒
所以,请问有什么办法解决,或有什么方法可以检测当前串口是接有设备呢(好让我在程序一开始时检测不到设备,就不再向它发数据)
File ComPort = new File("COM1");
try {
FileWriter out = new FileWriter(ComPort);
out.write(str);
out.close();
}
catch (Exception ex) {
ex.printStackTrace();
}这方法写数据进接在串口的设备的,当接上设备后,是没什么问题,但当没接设备时,每一次发送都让程序停顿大概一秒
所以,请问有什么办法解决,或有什么方法可以检测当前串口是接有设备呢(好让我在程序一开始时检测不到设备,就不再向它发数据)
解决方案 »
- 【强力100分】关于一个java和一个页面交互的问题,求思路或者可行性。
- 作业的一道加分题求帮助。
- 怎么实现一个下载链接的 下载量???
- 为什么报错: ".class" expected
- 这个程序编译出错,可我不知道错在哪里,大家帮我看一下吧,谢谢了!
- 急请高手解决的java数学类问题
- 一个小问题 帮忙解释一下
- 为什么在JDK执行这个收邮件程序会出错的??
- 初等问题:我刚安装上Java,却找不到Javac,按说明我去修改autoexec.bat,但是,我的autoexec.bat文件是空的.请问autoexec.bat文件的标准格式
- 简单问题,送分了
- 初学stucts,问一个问题。请高手指点
- 让我啼笑皆非的错误
串口跟接不接设备好象没有关系。
一般接上设备之后,给他发特定的指令 比如初始化,或者状态查询指令,应该会有返回。
根据返回值就能够判断是否接上设备或者判断设备是否异常了。
另外。老兄的方法好象不对头。
串口通讯一般都是采用 comm.jar 那个串口通讯包,用那个包你可以检测那个串口被占用了,但是不能检测是否接有设备。至于停顿问题,很好办,可以肯定写是不会有停顿时间的,(通过outputstream.write)
读的时候是堵塞读,但是可以通过 enablereceivetimeout() 来设置超时。by the way , 虽然没有测试过你的代码但是直觉上 我觉得
File ComPort = new File("COM1");
try {
FileWriter out = new FileWriter(ComPort);
耗时应该是这两条语句,不是前一条就是后一条。
import java.io.*;
import javax.comm.*;
import ssts.device.*;
private String commportName="COM1";
private int commportBaudrate=19200;
private int commportDatabits=SerialPort.DATABITS_8;
private int commportStopbits=SerialPort.STOPBITS_1;
private int commportParity=SerialPort.PARITY_NONE; private SerialPort commPort=null;
private InputStream portInputStream=null;
private OutputStream portOutputStream=null; /**************************************************************
函数原型: int open(String com , int ntimeout)
功能: 打开串口
入口参数: com串口名字, int ntimeout 允许的最大超时时间
出口参数: 打开成功与否,1 是打开成功,0是打开失败
**************************************************************/
public synchronized int open(String com , int ntimeout) throws Exception{
this.commportName=com; boolean foundPortFlag=false;
CommPortIdentifier tmpCommportIdent=null;
Enumeration commportEnum=CommPortIdentifier.getPortIdentifiers();
while(commportEnum.hasMoreElements()){
tmpCommportIdent=(CommPortIdentifier)commportEnum.nextElement();
if((tmpCommportIdent.getName().trim().toUpperCase().equals(this.commportName)) &&
(tmpCommportIdent.getPortType()==CommPortIdentifier.PORT_SERIAL)){
foundPortFlag=true;
break;
}
}
if(foundPortFlag != true)
throw new Exception("JUFCMSGCARDREADER_OPEN_ERR:INVALID PORT");
if(tmpCommportIdent.isCurrentlyOwned())
{
System.out.println("port is owned!");
return 0;
}
else{
try{
this.commPort=(SerialPort)tmpCommportIdent.open("UFCMAGCARDREADER",ntimeout);
this.commPort.setSerialPortParams(this.commportBaudrate,this.commportDatabits,this.commportStopbits,this.commportParity);
this.commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
this.commPort.setRTS(false);
this.commPort.setDTR(false); this.portInputStream=this.commPort.getInputStream();
this.portOutputStream=this.commPort.getOutputStream();
}
catch(Exception e){
//e.printStackTrace();
throw new Exception("JUFCMSGCARDREADER_OPEN_ERR:OPEN PORT FAIL");
}
}
return 1;
}
*函数原形 :byte[] sendCommand(
byte[] xbtSrcCommand,
int iMaxExecuteTimeOut,
int iSendBlockSize,
int iSendBlockTime,
int iReceiveSize,
int iReceiveBlockSize,
int iReceiveByteTimeout
)
*功能 :执行命令流。流程如下:将输入的命令流按指定的块大小和时间发送往端口。等待返回值。
*入口参数 :xbtSrcCommand : 命令流
xiMaxExecuteTimeout : 超时时间
xiSendBlockSize : 数据分块发送,这里指定每次发送的块的大小.
xiSendBlockTime : 发送完一块后休息时间。//有些驱动需要完成该块的操作时间。
但一般建议为0
xiReceiveSize : 总共要接收的块的大小。在变长的情况下,可用估计值。
估计大小一定要大于现实的大小。
xiReceiveBlockSize : 接收也按照块来接收,这里指定接收缓冲区块的大小。
xiReceiveSize > = xiReceiveBlockSize
xiReceiveByteTimeout : 每个字节到达的最大的时间。
*出口参数 :无
*返回值 :接受到的数据,如果数组为空,则标明没有收到数据。
*附注 : 因为存在读卡器模块返回的值长度不一的情况,所以,在接受返回结果的时候,
以最小的单位划分接受的缓冲区块的大小,而可能的最大值作为总的接收的快的大小。
否则可能会出现问题。
*******************************************/
private byte[] sendCommand( byte[] xbtSrcCommand,
int xiMaxExecuteTimeOut,
int xiSendBlockSize,
int xiSendBlockTime,
int xiReceiveSize,
int xiReceiveBlockSize,
int xiReceiveByteTimeout
) throws Exception
{
//System.out.println("time is " + new Date().getTime());
//flog.writeLog("JUfcWHICReader","sendCommand","enter into ");
//参数检查 //切割输入数据流成块,并发送。
byte[] btSendBuff = null; for ( int iCount = 0 ;
iCount < xbtSrcCommand.length;
iCount += xiSendBlockSize
)
{
//计算每次发送缓冲区的长度。 和拷贝初始位置
//计算公式应为 xbtSrcCommand.length -1 - iCount +1
if (xbtSrcCommand.length - iCount > xiSendBlockSize)
{
btSendBuff = new byte[xiSendBlockSize];
}
else
{
btSendBuff = new byte[xbtSrcCommand.length - iCount];
}
//拷贝数据到发送缓冲区,发送
System.arraycopy(xbtSrcCommand,iCount,btSendBuff,0,btSendBuff.length);
try
{
this.portOutputStream.write(btSendBuff);
this.portOutputStream.flush();
}
catch (Exception e)
{
throw new Exception("UFCICREADER_SENDCOMMAND_ERR0:WRITE_TO_PORT");
}
//是否等待一定时间
if (xiSendBlockTime != 0)
{
Thread.currentThread().sleep(xiSendBlockTime);
}
}
//分块接收数据,接收策略如下,如果有数据到达那么后续的数据到达大的时间将会非常短。
//因此可以划分块接收,一旦接收到数据,就减少接收时间。
byte[] retRawData=new byte[1024];
int retRawDataSize = 0;
int retRawDataSizeTmp = 0; for(int iCount = 0,iTime = xiMaxExecuteTimeOut,iBlockSize = xiReceiveBlockSize;
iCount < xiReceiveSize ;
iCount += xiReceiveBlockSize , iTime = (iBlockSize * xiReceiveByteTimeout)
)
{
//计算接收缓冲区大小。
if (iCount + xiReceiveBlockSize > xiReceiveSize)
{
iBlockSize = xiReceiveSize - iCount ; //都以1开始计数,不用补增量
}
//System.out.println("receiveBlock Size is " + iBlockSize +" time is "+iTime);
this.commPort.enableReceiveThreshold(iBlockSize);
this.commPort.enableReceiveTimeout(iTime);//whx byte[] retRawDataTmp = new byte[500];
retRawDataSizeTmp = 0 ;
try
{
retRawDataSizeTmp = this.portInputStream.read(retRawDataTmp);
this.commPort.disableReceiveThreshold();
}
catch (Exception e)
{
throw new Exception("UFCICREADER_SENDCOMMAND_ERR0:READ_FROM_PORT");
} if (retRawDataSizeTmp != 0)
{
System.arraycopy(retRawDataTmp,0,retRawData,retRawDataSize,retRawDataSizeTmp);
retRawDataSize += retRawDataSizeTmp;
if (retRawDataSize >= xiReceiveSize)
{
break;//如果不小心接受了太多的话,就够了。
}
}
}
//返回结果
if (retRawDataSize == 0) //没有数据
{
flog.writeLog("JUfcWHICReader","SENDCommand","THROW RAISE:CANNOT READ DATA");
return null;
} byte[] retValidData=new byte[retRawDataSize];
System.arraycopy(retRawData,0,retValidData,0,retRawDataSize); flog.writeHexLog("JUfcWHICReader","doCommand","read response : ",retValidData,retValidData.length);
flog.writeLog("JUfcWHICReader","doCommand","return"); return retValidData;
}