import java.io.*;
import java.util.*;
import javax.comm.*;
public class ReadCard implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM2")) {
ReadCard reader = new ReadCard();
}
}
}
}
public ReadCard() {
try {
serialPort = (SerialPort) portId.open("ReadCardApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
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:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.println( new String(readBuffer) + "end");
} catch (IOException e) {}
break;
}
}
}
小弟在CSDN上找的例子 例子上的错误是每次接收数据时没有等到接收完全部数据 就已经结束了
参考 之前没有设enableReceiveThreshold(int x) 所以程序在自动读取几个字符后就返回一次了、、
小弟新手 没有弄懂 请指教...
import java.util.*;
import javax.comm.*;
public class ReadCard implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM2")) {
ReadCard reader = new ReadCard();
}
}
}
}
public ReadCard() {
try {
serialPort = (SerialPort) portId.open("ReadCardApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
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:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.println( new String(readBuffer) + "end");
} catch (IOException e) {}
break;
}
}
}
小弟在CSDN上找的例子 例子上的错误是每次接收数据时没有等到接收完全部数据 就已经结束了
参考 之前没有设enableReceiveThreshold(int x) 所以程序在自动读取几个字符后就返回一次了、、
小弟新手 没有弄懂 请指教...
解决方案 »
- 狂晕,很久没来,怎么没有了netbeans版,却只有eclipse版???
- 问个 字符串 检索的问题
- 求助---一个关于时间的问题
- 新手关于java版本的疑问
- 学玩J2SE想学J2EE??
- FCKeditor在weblogic7配置问题?弄了两天还不行!!!555555555555555
- 利用String的getBytes方法,怎么得到的结果是这样?
- 一个取数组宽度问题,请指教!!!
- 调查一下,关于 jbuilder6。多多参加,哦,人人得分的问题哦
- 关于MyWindowListener
- 如何加密 sql password 在 hibernate.cfg.xml ?
- html向applet传参数
comm。jar文件配置不正确,但是经过我仔细验证,不大像。
还有便是enableReceiveThreshold(int x)及enableReceiveTimeout(int x)语句,可我加上之后还是存在这样的问题。
也有说就是一次只能接受8byte数据一次,多的要重复读入。
我现在的暂时处理方法是设置一个缓冲,通过程序逻辑将重复读入的数据组织一起放在缓冲中。在前台读去缓冲之后,将缓冲清掉,等待下次的输入。