在Java中操作串口实现短信收发
采用串口操作进行短信收发,是比较常见的一种方式.比如,很多群发软件,用的就是这种方法.
1.配置comm.jar.
Comm.jar是Sub实现底层串口操作的API,调用了本地的DLL文件,因为Java本身不具备直接访问硬件设置的能力,都是通过调用本地方法来实现的.可以Java的官方网站下载.下载之后把其中Comm.jar包导入到工程的Classpath中,把另外两个非常重要的文件javax.comm.properties和win32com.dll考贝到你的工程目录下,即java.user下.
2.打开串口.
在打开串口前首先要加载Win32com.dll,因为我们没有把它放到JRE路径下,所以必须要自己显式的加载.
String driverName = "com.sun.comm.Win32Driver";
CommDriver driver = null;
try {
System.loadLibrary("win32com");
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
} catch (InstantiationException e1) {
logger.error("1:" + e1.getMessage());
} catch (IllegalAccessException e1) {
logger.error("2:" + e1.getMessage());
} catch (ClassNotFoundException e1) {
logger.error(e1.getMessage());
}
然后获取你指定的端口:
SerialPort sPort = null;
CommPortIdentifier portID;
String owner = new String("modemn");
int keeptime = 5000;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifier) portList.nextElement();
if (portID.getName().equals(com))
try {
sPort = (SerialPort) portID.open(owner, keeptime);
break;
}// 打开一个串口
catch (PortInUseException e) {
logger.fatal(e.getMessage());
System.exit(1);
}
相关文档:
JAVA annotation入门
最近对spring源码感兴趣,今天看到annotation部分,略记之。
一. 最常见的annotation
@Override:用在方法之上,用来告诉别人这一个方法是改写父类的
@Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会用产生警告信息,可以设定在 ......
今天领导吩咐一个任务,就是用.net技术去跳用java端写的webservers,而且要采用https访问方式,强制论证
String SecurelyStoredPassword = "adminsd";
&nb ......
JAVA annotation入门
最近对spring源码感兴趣,今天看到annotation部分,略记之。
一. 最常见的annotation
@Override:用在方法之上,用来告诉别人这一个方法是改写父类的
@Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会用产生警告信息,可以设定在 ......
Java设计模式之Singleton单例模式篇
java 单例模式的实现方式
实现1:
public class Singleton {
private static Singleton instance;
private Singleton (){ //首先有一个私有的构造器
}
......
1 编译命令
javac [options] filename.java
options->
-classpath path 编译时需要的类路径
-d directory 设定编译生成的.class文件输入到哪一个目录。
关于-d小技巧 : 如果.java文件中使用了package语句, 例如 package com.test.maths; 加上- d . 选项会帮助在当前目 ......