• 大小: 35KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-18
  • 语言: Java
  • 标签: comm  win32comm  java  

资源简介

包括: 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


评论

共有 条评论