串口连接好了,剩下的就是写程序了
1.准备
下载Communication包
下载地址如下:http://mdubuc.freeshell.org/Jolt/javacomm20-win32.zip.
2.配置
解压缩javacomm20-win32.zip
把win32com.dll拷贝到{JAVA_HOME}\jre\bin
把comm.jar拷贝到{JAVA_HOME}\jre\lib\ext
把javax.comm.properties拷贝到{JAVA_HOME}\jre\lib
再环境变量的CLASSPATH中添加一项:{JAVA_HOME}\jre\lib\ext\comm.jar;
我的CLASSPATH添加后为:
.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\jre\lib\ext\comm.jar;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\jtds.jar;%Tomcat_Home%\common\lib\servlet-api.jar;
3.列举出本机所有可用串口
void listPortChoices() {
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType()