您的位置:程序门 -> java -> gui 设计



如何用 java 判断串口是否有设备?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何用 java 判断串口是否有设备?
发表于:2008-01-08 14:44:12 楼主
请问如何判断串口是否有设备?
发表于:2008-01-09 16:37:291楼 得分:0
下面是一个简单的对取串口的例子

import   java.io.*;
import   java.text.simpledateformat;
import   java.util.date;
import   java.util.toomanylistenersexception;


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   javax.comm.*;//windows下用

/**
  *   <p> title:   </p>
  *
  *   <p> description:   </p>
  *
  *   <p> copyright:   copyright   (c)   2006 </p>
  *
  *   <p> company:   </p>
  *
  *   @author   not   attributable
  *   @version   1.0
  */
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   readwritelock   =   new   object();


        public   serialcomm()
        {
isopen   =   false;
isstart   =   false;
issave   =   true;
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
        {
portname   =   port;
portaddress   =   portname;
dataprotocol   =   protocol;

init();
        }

        public   void   init(string   port,   string   address,   string   protocol)   throws   exception
        {
portname   =   port;
portaddress   =   address;
dataprotocol   =   protocol;

init();
        }

        public   void   init()   throws   ioexception,   exception,   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());
}   catch   (portinuseexception   ex)
{
        throw   new   exception(ex.tostring());
}
        }

        public   void   start()   throws   exception
        {
if   (!isopen)
{
        throw   new   exception(portname   +   "   has   not   been   opened.");
}

try
{
        readthread   =   new   thread(this);
        readthread.start();

//设置串口数据可用
        serialport.notifyondataavailable(true);

//设置串口监听对象
        serialport.addeventlistener(this);

        isstart   =   true;
}   catch   (toomanylistenersexception   ex)
{
        throw   new   exception(ex.tostring());
}
        }

        public   void   run()
        {
//组装发送数据
        string   at   =   "at^hcmgr=1\r";
string   strtemp   =   at   +   (char)   integer.parseint("1a",   16)   +   "z";

system.out.println("send:"+strtemp);

//向串口发送数据
writecomm(strtemp);
isprint   =   true;
        }

        public   void   stop()
        {
if   (isstart)
{
//设置串口数据是否可用
        serialport.notifyondataavailable(false);
//移除串口监听事件
serialport.removeeventlistener();

        isstart   =   false;
}
        }

        public   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();
break;
        default:
break;
}
        }

        public   void   readcomm()
        {

//下面这些都是用流读取数据。具体怎么用看自己需要用什么样的流对象
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)
        {
        system.out.println("c="+c);
        readbuffer.append((char)   c);
        }
scannedinput   =   readbuffer.tostring().trim();
}   catch   (ioexception   ex)
{
        ex.printstacktrace();
}   catch   (exception   ex)
{
        ex.printstacktrace();
}

        }

        public   void   writecomm(string   outstring)
        {
synchronized   (readwritelock)
{
        try
        {
//向串口设备写数据
outputstream.write(outstring.getbytes());
        }   catch   (ioexception   ex)
        {
        }
}
        }

        public   static   void   main(string[]   args)
        {
serialcomm   serialcomm   =   new   serialcomm();

try
{
        serialcomm.init("com3",   "air");//windows下测试端口
        serialcomm.start();
}   catch   (exception   ex)
{
        system.out.println(ex.tostring());
}
        }

}


快速检索

最新资讯
热门点击