SUN Java Communications API 2.0
解决方案 »
- 求助 s2sh+jpa
- 请问web上为什么很少用多级popup窗口?
- html向action提交表单,中文都变成了乱码,这该如何解决?
- 看看这段JavaScript代码
- 网上客服,点对点聊天(跟qq一样),该怎么做!
- jsp 如何获得使用用户的网络信息
- java使用socket实现聊天程序问题
- jexcel生成的execl文件,如果有数值型的0,那0就会不显示,就显示0,如何解决
- resin与tomcat
- servlet中初始化的数据库缓冲池变量怎么找?急!
- jsp可以像vbscript一样提取XML中的记录吗
- 我装了Tomcat4.1.24和Apache2.0.48,现在我想装一个IIS,怎么配置?
import java.util.*;
import javax.comm.*;public class SimpleRead 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("COM1")) {
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
} public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 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) {
System.out.println("event.getEventType() : " + event.getEventType());
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.print(new String(readBuffer));
}
catch (IOException e) {}
break;
}
}
}