資源簡介
包括:
comm.jar 串口通訊的jar包。
win32com.dll windows系統底層接口,需要放到windows\system32下。
SimpleRead.java 是監聽方式讀取串口反饋信息。
ReadPort.java 是輪詢方式讀取串口反饋信。

代碼片段和文件信息
import?java.io.*;
import?java.text.SimpleDateFormat;
import?java.util.Calendar;
import?java.util.Date;
import?java.util.Enumeration;
import?java.util.Timer;
import?java.util.TimerTask;
import?java.util.TooManyListenersException;
import?javax.comm.CommDriver;
import?javax.comm.CommPortIdentifier;
import?javax.comm.NoSuchPortException;
import?javax.comm.PortInUseException;
import?javax.comm.SerialPort;
import?javax.comm.UnsupportedCommOperationException;
/**
?*
?*?This?class?reads?message?from?the?specific?serial?port?and?save
?*?the?message?to?the?serial?buffer.
?*
?*/
public?class?ReadPort?extends?Thread
{
public?static?void?main(String[]?args)?{
try
{
new?MythreadPort().start();
}
catch(Exception?e)
{
System.out.println(“err:?“+e);
}
}
}
class?MythreadPort?extends?java.util.Date??implements?Runnable?{
int?i=0;
static?CommPortIdentifier?portId;
static?Enumeration???????portList;
InputStream????????inputStream;
SerialPort????????serialPort;
ISNO?isno?=?new?ISNO();
String?path=““;
String?str?=?““;
String?restr?=?“第一次..“;
long?timeLast?=??isno.getime();
long?timeNew?=??isno.getime();
Thread?t;
public?MythreadPort()?{
super();
}
public?void?start()?{
if?(t?==?null)?{
t?=?new?Thread(this);
t.start();
}
}
public?void?run()?{
LoadDriver();
try?{
Thread?current?=?Thread.currentThread();
while?(t?==?current){
Read();
Thread.sleep(50);
}
}?catch?(InterruptedException?e)?{
//?TODO?自動生成?catch?塊
e.printStackTrace();
}
}
public?void?LoadDriver(){
String?driverName?=?“com.sun.comm.Win32Driver“;
CommDriver?driver?=?null;
try?{
System.loadLibrary(“win32com“);
driver?=?(CommDriver)?Class.forName(driverName).newInstance();
driver.initialize();
}?catch?(InstantiationException?e1)?{
e1.printStackTrace();
}?catch?(IllegalAccessException?e1)?{
e1.printStackTrace();
}?catch?(ClassNotFoundException?e1)?{
e1.printStackTrace();
}
}
public?String?Read()?{
try?{
try?{
portId?=?CommPortIdentifier.getPortIdentifier(“COM1“);
}?catch?(NoSuchPortException?e)?{
//?TODO?自動生成?catch?塊
e.printStackTrace();
}
serialPort?=?(SerialPort)?portId.open(“Read“?100);
}?catch?(PortInUseException?e)?{}
try?{
inputStream?=?serialPort.getInputStream();
}?catch?(IOException?e)?{}
serialPort.notifyondataavailable(true);
try?{
serialPort.setSerialPortParams(19200?SerialPort.DATABITS_8?
SerialPort.STOPBITS_1?
SerialPort.PARITY_NONE);
}?catch?(UnsupportedCommOperationException?e)?{}
byte[]?readBuffer?=?new?byte[14];
try?{
while?(inputStream.available()?>?9)?{
int?numBytes?=?inputStream.read(readBuffer);
str?=?new?String(readBuffer);
System.out.println(“str=“+str);
if?(str.indexOf(restr)?==?-1)?{
i
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????13633??2006-09-25?12:06??SimpleRead.java
?????文件??????28043??1998-11-15?16:00??comm.jar
?????文件??????27648??1998-11-15?16:00??win32com.dll
?????文件???????4091??2006-09-25?11:25??ReadPort.java
?????文件????????566??2008-12-04?09:19??說明.txt
-----------?---------??----------?-----??----
????????????????73981????????????????????5
- 上一篇:javaMail 用三種方式實現發送附件
- 下一篇:網上心理測試代碼(JSP)
評論
共有 條評論