能不能写一个servlet读写本地串口的数据

sshany 2003-07-16 07:13:27
thx
...全文
52 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Grace_ghb 2003-12-09
  • 打赏
  • 举报
回复
能说详细点?
wenming168 2003-12-09
  • 打赏
  • 举报
回复
servlet运行在服务器端不可能读本地串口,要用签名的applet
Acylas 2003-12-09
  • 打赏
  • 举报
回复
我有一篇文章你可以看看,肯定可以实现到,不过比较长这里贴不了。
你看了可以结贴了。
wangyanqiu 2003-07-30
  • 打赏
  • 举报
回复


up
qiuyilai 2003-07-30
  • 打赏
  • 举报
回复
import java.io.*;
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;
}
}
}
qiuyilai 2003-07-30
  • 打赏
  • 举报
回复
SUN Java Communications API 2.0

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧