`
bdk82924
  • 浏览: 553134 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

串口开发 comm.jar

阅读更多
1、下载java Communications api开发包。
2、将win32com.dll拷贝入C:\j2sdk1.4.2_04\bin

3、将comm.jar拷贝入C:\j2sdk1.4.2_04\jre\lib\ext

4、将javax.comm.properties拷贝入C:\j2sdk1.4.2_04\jre\lib

5、编译CommTest.java文件

import java.io.*;

import java.util.*;

import javax.comm.*;

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=CommPortIdentifier.getPortIdentifier("COM1");

}catch(NoSuchPortException ne) {

System.out.println("ne"); ne.printStackTrace();

}

try{

serialPort=(SerialPort) portId.open("TestComm", 5);

OutputStream output = serialPort.getOutputStream();

doutput=new DataOutputStream(output);

inputStream = serialPort.getInputStream();

}catch(PortInUseException ex) {

System.out.println("ex"); ex.printStackTrace();

}catch(IOException ie) {

System.out.println("ie");

ie.printStackTrace();

//serialPort.close();

}

try {

serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

} catch (UnsupportedCommOperationException e) {}

}

try {

doutput.write(messageString.getBytes());

} catch (IOException e) {}

}

6、串口打开后,用InputStream和DataOutputStream读写就可以了。

7、由于串口为共享资源,所以在设计程序时应采用单例模式。
4
0
分享到:
评论
2 楼 kinganpo 2011-03-04  
1 楼 xff 2010-01-08  
你好,我是学java不长时间,对这个比较感兴趣,但是我不知道com口是什么?我在网上下了一段程序,也照样子配置好了,那么它提示我配置端口地址,这是因为我没有将数据线连在com口是吗?

相关推荐

Global site tag (gtag.js) - Google Analytics