当前位置: 技术问答>java相关
请各位帮忙!有分送(32分)
来源: 互联网 发布时间:2015-01-10
本文导语: import java.io.*; import java.util.*; import javax.comm.*; public class SimpleWrite { static Enumeration portList; static CommPortIdentifier portId; static String messageString = "Hello, world!n"; static SerialPort serialPort; static Out...
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!n";
static SerialPort serialPort;
static OutputStream outputStream;
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")) {
try {
serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) { System.out.println( "" + e ); }//运行到此产生异常,为什么?
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
}
}
}
}
}
|
产生异常的位置到底在哪里?你把System.out.println(""+e)改成e.printTrace()再观察一下出错信息,可以知道准确的错误定位。
估计是2000端口已经被别的应用程序占用了。你换个端口号看看
估计是2000端口已经被别的应用程序占用了。你换个端口号看看