当前位置:文档之家› java串口通讯程序

java串口通讯程序

java串口通讯程序
1、下载java Communications api开发包。

|
是Sun公司提供的,用于开发平台独立的通讯应用程序的扩展API。

2、将拷贝入C:\j2sdk1.4.2_04\bin
3、将拷贝入C:\j2sdk1.4.2_04\jre\lib\ext
4、将拷贝入C:\j2sdk1.4.2_04\jre\lib
5、编译文件
import .*;
import .*;
import .*;
public class CommTest{
public static void main(String[] args){
SerialPort serialPort=null;
DataOutputStream doutput=null;
InputStream inputStream;
CommPortIdentifier portId=null;
String messageString="hello \n";
try{
portId=("COM1");
}catch(NoSuchPortException ne) {
"ne"); ();
}
try{
serialPort=(SerialPort) ("TestComm", 5);
OutputStream output = ();
doutput=new DataOutputStream(output);
inputStream = ();
}catch(PortInUseException ex) {
"ex");
();
}catch(IOException ie) {
"ie");
();
//();
}
try {
(9600, , , ;
} catch (UnsupportedCommOperationException e) {}
}
try {
());
} catch (IOException e) {}
}
6、串口打开后,用InputStream和DataOutputStream读写就可以了。

7、由于串口为共享资源,所以在设计程序时应采用单例模式。

8、API
在下有13个类和接口,分别是
4个接口
CommDriver可负载设备(the loadable device)驱动程序接口的一部分CommPortOwnershipListener传递各种通讯端口的所有权事件ParallelPortEventListener传递并行端口事件SerialPortEventListener传递串行端口事件
6个类
CommPort通讯端口
CommPortIdentifier通讯端口管理
ParallelPort并行通讯端口
ParallelPortEvent并行端口事件
SerialPort RS-232串行通讯端口
SerialPortEvent串行端口事件
3个异常类
NoSuchPortException当驱动程序不能找到指定端口时抛出PortInUseException当碰到指定端口正在使用中时抛出UnsupportedCommOperationException驱动程序不允许指定操作时抛出。

相关主题