资源简介
包括:
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)
相关资源
- javaMail 用三种方式实现发送附件
- Google 推出的 Java 编码规范
- java 实现支付宝接口
- java,Eclipse,ATM自动柜台模拟系统
- DFA NFA java实现
- java农夫过河问题
- 图书进存销系统java完整代码
- GN算法Java版源码,个人鼎作
- java课程设计 客房管理系统
- c调用java的
- JAVA爬虫项目源代码
- java 聊天室 计算机网络
- JAVA算术编码
- 小叮当代码JAVA.rar
- java多线程文件传输基于swing
- SpringBoot实现Socket.io消息推送(java+h
- 图书馆管理系统java+S2SH+MySQL
- 数据库课程设计教务管理系统
- java数字波形生成器
- 简单的java实现数据库连接及宾馆客房
- Java编码规范
- java获取鼠标坐标位置swing
- java采用Bresenham算法画直线
- 选课授课信息管理系统
- SL275lab 练习答案
- JAVA修改AD域密码_免证书
- Java写的吃豆豆游戏源代码
- Java写的画图程序
- MIPS反编译器与模拟器使用JAVA
- JAVA管程解决哲学家问题
评论
共有 条评论