如何用 JAVA 判断串口是否有设备? 请问如何判断串口是否有设备? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是一个简单的对取串口的例子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()); } }} 下面有介绍,还有需要的动态库及类库下载http://www.programmerstudy.com/programme/java/20084/97.html 一道java题,帮忙看下 java连接sql2005的问题 关于JTREE的问题~~~~~~~~~ 学习android 大概都有哪些入门的书呢 两个错误请教高手(在线等待) 关于java 类型初始化问题 使用sybase出现SET CHAINED command not allowed within multi-statement transaction. 为什么会出现这种情况呢 Applet问题:这是为什么? jbuilder的问题? 谁知道 jdbc-odbc数据库连接是否要装jdbc程序 java串口问题,显示不出来 请教,用comm.jar实现串口通信怎样才比较好。
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());
}
}}
http://www.programmerstudy.com/programme/java/20084/97.html