当前位置: 技术问答>java相关
请问高手们如何用JAVA对COM端口分别进行读和写操作?50分相送谢了!
来源: 互联网 发布时间:2015-02-05
本文导语: 请给我详细代码,谢谢。 注:我参考并修改了SUN的Java(tm) Communications API Win32 文档和实例程序但是好象不行呀,代码如下: import java.io.*; import java.util.*; import javax.comm.*; public class SimpleWrite { static Enumerat...
请给我详细代码,谢谢。
注:我参考并修改了SUN的Java(tm) Communications API Win32 文档和实例程序但是好象不行呀,代码如下:
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) {
//Obtains an enumeration object that contains a CommPortIdentifier object for each port in the system.
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) { //Tests if this enumeration contains more elements.
System.out.println("Port Detected!");
//Returns the next element of this enumeration if this enumeration object has at least one more element to provide.
portId = (CommPortIdentifier) portList.nextElement();
//Returns the port type. Returns:portType - PORT_SERIAL or PORT_PARALLEL
//CommPortIdentifier.PORT_SERIAL:RS-232 serial port
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//Returns the name of the port. The port name should be identifiable by the user. Ideally, it should be the label on the hardware. For example, "COM1" and "COM2" on PCs; "Serial A" and "Serial B" on Sun Ultra workstations.
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException 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());
//System.out.println("abc");
} catch (IOException e) {}
}
}
}
}
}
如果程序能检测到端口的话,就会进入WHILE循环并显示"Port Detected!",但是执行结束后没能显示此字符串,说明WHILE循环并未被真正执行。
请高手们相助!
注:我参考并修改了SUN的Java(tm) Communications API Win32 文档和实例程序但是好象不行呀,代码如下:
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) {
//Obtains an enumeration object that contains a CommPortIdentifier object for each port in the system.
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) { //Tests if this enumeration contains more elements.
System.out.println("Port Detected!");
//Returns the next element of this enumeration if this enumeration object has at least one more element to provide.
portId = (CommPortIdentifier) portList.nextElement();
//Returns the port type. Returns:portType - PORT_SERIAL or PORT_PARALLEL
//CommPortIdentifier.PORT_SERIAL:RS-232 serial port
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//Returns the name of the port. The port name should be identifiable by the user. Ideally, it should be the label on the hardware. For example, "COM1" and "COM2" on PCs; "Serial A" and "Serial B" on Sun Ultra workstations.
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException 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());
//System.out.println("abc");
} catch (IOException e) {}
}
}
}
}
}
如果程序能检测到端口的话,就会进入WHILE循环并显示"Port Detected!",但是执行结束后没能显示此字符串,说明WHILE循环并未被真正执行。
请高手们相助!
|
Hi qianying :)
我这儿一切正常啊, 我用的是2.0 for windows, 运行环境是IBM JDK1.3 + Windows 2000 Professional ...
我这儿一切正常啊, 我用的是2.0 for windows, 运行环境是IBM JDK1.3 + Windows 2000 Professional ...
|
你看看你的系统啊, 有没有什么冲突? 再看看BIOS, 有没有禁用什么的 ... 反正应该不是程序的问题了 ...
算我多嘴, 不知道你用这个打算做什么呢? 你对串口通信很熟吧? 我最近也对JNI很感兴趣, 特别想做点东西:p 记得以前有人做出在linux上通过自制的串口线来检测主机是否停电, 并配合UPS完成自动关机的程序, 是用C写的 ... 当然我不是说要用java再写一边, 那简直就是体力劳动乐, 我只是觉得通过串口还可以做更多的东西, 你觉得呢?
算我多嘴, 不知道你用这个打算做什么呢? 你对串口通信很熟吧? 我最近也对JNI很感兴趣, 特别想做点东西:p 记得以前有人做出在linux上通过自制的串口线来检测主机是否停电, 并配合UPS完成自动关机的程序, 是用C写的 ... 当然我不是说要用java再写一边, 那简直就是体力劳动乐, 我只是觉得通过串口还可以做更多的东西, 你觉得呢?
|
看看有没有将javax.comm.properties文件放在lib目录下,如果没有将检测不到任何端口。
|
看看你的程序退出时是否有错。