linux下用java通过串口连接手机或短信猫发送中文短信的方法
2012-01-14 14:34
525 查看
下载jar包,解压,把其中so文件复制到/usr/lib
把javax.comm.properties复制到jre目录下
把comm.jar放到classpath目录下
串口连接:
CommPortIdentifier portID;
String owner = new String("modem");
int keeptime = 5000;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();
// String driverName = "com.sun.comm.Win32Driver";
String driverName = "com.sun.comm.LinuxDriver";
CommDriver driver = null;
try {
// System.loadLibrary("win32com");
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifier) portList.nextElement();
System.out.println("COM:" + portID.getName());
// if (portID.getName().equals("COM1"))
if (portID.getName().equals("/dev/ttyS0"))
try {
sPort = (SerialPort) portID.open(owner, keeptime);
break;
}catch (PortInUseException e) {
e.printStackTrace();
System.exit(1);
}
}
try{
if (sPort != null)
{
in = sPort.getInputStream();
out = sPort.getOutputStream();
try {
sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
}catch(Exception e)
{
e.printStackTrace();
}
其中注释掉的是windows下的连接方法
全部代码
把javax.comm.properties复制到jre目录下
把comm.jar放到classpath目录下
串口连接:
CommPortIdentifier portID;
String owner = new String("modem");
int keeptime = 5000;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();
// String driverName = "com.sun.comm.Win32Driver";
String driverName = "com.sun.comm.LinuxDriver";
CommDriver driver = null;
try {
// System.loadLibrary("win32com");
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifier) portList.nextElement();
System.out.println("COM:" + portID.getName());
// if (portID.getName().equals("COM1"))
if (portID.getName().equals("/dev/ttyS0"))
try {
sPort = (SerialPort) portID.open(owner, keeptime);
break;
}catch (PortInUseException e) {
e.printStackTrace();
System.exit(1);
}
}
try{
if (sPort != null)
{
in = sPort.getInputStream();
out = sPort.getOutputStream();
try {
sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
}catch(Exception e)
{
e.printStackTrace();
}
其中注释掉的是windows下的连接方法
全部代码
import javax.comm.*; import java.util.Enumeration; import java.util.TooManyListenersException; import java.io.*; public class ATSms implements SerialPortEventListener { private SerialPort sPort = null; private InputStream in = null; private OutputStream out = null; private byte b[] = new byte[1024]; private String cmd = ""; private int len = 0; private String recvMsg = ""; public ATSms() { init(); } public synchronized void serialEvent(SerialPortEvent serialPortEvent) { int c = 0; // 如果有串口事件发生 if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { System.out.print("Recv :" ); while ((c = in.read()) != -1) { System.out.print((char)c); recvMsg += ((char)c); } }// try catch (IOException e) { e.printStackTrace(); } } } private static String getUnicodeString(String s) { String unicodeStr = ""; for(int i=0;i<S.LENGTH();I++) { String c = Integer.toHexString(s.charAt(i) & 0xffff); while(c.length()<4) { c = "0" + c; } unicodeStr += c; } return unicodeStr; } private static String getSMobile(String mobile) { StringBuffer sMobile = new StringBuffer("68"); for( int i=0;i<MOBILE.LENGTH();I=I+2) { sMobile.append((i+1<MOBILE.LENGTH())?MOBILE.CHARAT(I+1):"F"); sMobile.append(mobile.charAt(i)); } return sMobile.toString(); } private void init() { CommPortIdentifier portID; String owner = new String("modem"); int keeptime = 5000; Enumeration portList; portList = CommPortIdentifier.getPortIdentifiers(); // String driverName = "com.sun.comm.Win32Driver"; String driverName = "com.sun.comm.LinuxDriver"; CommDriver driver = null; try { // System.loadLibrary("win32com"); // System.out.println("Win32Com Library Loaded"); driver = (CommDriver) Class.forName(driverName).newInstance(); driver.initialize(); System.out.println("Win32Driver Initialized"); } catch (Exception e) { e.printStackTrace(); } // 如果有多个端口 while (portList.hasMoreElements()) { portID = (CommPortIdentifier) portList.nextElement(); System.out.println("COM:" + portID.getName()); // if (portID.getName().equals("COM1")) if (portID.getName().equals("/dev/ttyS0")) try { // System.out.println("try to open the /dev/ttyS0"); sPort = (SerialPort) portID.open(owner, keeptime); // System.out.println("try to open the /dev/ttyS0 OK"); break; }catch (PortInUseException e) { e.printStackTrace(); System.exit(1); } } try{ if (sPort != null) { in = sPort.getInputStream(); out = sPort.getOutputStream(); try { sPort.addEventListener(this); sPort.notifyOnDataAvailable(true); } catch (TooManyListenersException e) { e.printStackTrace(); } } /** System.out.print("set PDU mode :"); out.write("at+cmgf=0\r".getBytes()); out.flush(); // len = in.read(b); for(int i=0;i<3;i++) { recvMsg = readThread.readComMsg(); if(!"".equals(recvMsg))break; Thread.sleep(1000); System.out.println("Sleep 1001."); } if(recvMsg.indexOf("OK")>=0) { System.out.println("OK"); }else { System.out.println("ERROR"); } */ }catch(Exception e) { e.printStackTrace(); } } public void close() { try { Thread.sleep(4000); in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } sPort.close(); } public int sendSms(String recv,String content) { int sendok = 1; for(int i=0;i<CONTENT.LENGTH();I=I+70) { sendok = sendShortSms(recv, ((i+70<=content.length())?content.substring(i,i+70):content.substring(i)) ); } return sendok; } private int sendShortSms(String recv,String content) { int sendok = -1; try{ System.out.println("send sms to:" + recv + "\ncontent:" + content); content = getUnicodeString(content); // content = (j+70>uc.length()/2)?uc.substring(j):uc.substring(j,j+140); // System.out.println(j + ":" + content); cmd = "at+cmgs=" + Integer.toString(15+content.length()/2) + "\r"; out.write(cmd.getBytes()); out.flush(); System.out.println("Send:" + cmd); for(int i=0;i<5;i++) { if(recvMsg.indexOf(">")>=0) { recvMsg = recvMsg.substring(recvMsg.indexOf(">")+1); break; }else { try { Thread.sleep(1000); }catch(Exception e) { } } } cmd = Integer.toHexString(content.length()/2 & 0xffff); if(cmd.length()<2)cmd="0"+cmd; cmd = "0011000D91" + getSMobile(recv) + "000800" + cmd + content; cmd = cmd.toUpperCase(); out.write(cmd.getBytes()); out.write(26); out.flush(); System.out.println("Send:" + cmd); for(int i=0;i<5;i++) { if(recvMsg.indexOf("OK")>=0) { recvMsg = recvMsg.substring(recvMsg.indexOf("OK")+2); sendok = 1; break; }else { try { Thread.sleep(1000); }catch(Exception e) { } } } }catch(Exception e) { sendok = -1; e.printStackTrace(); } return sendok; } public static void main(String args[]) { String c = ("16日" ); ATSms at = new ATSms(); at.sendSms("13999999999",c); at.close(); } }
相关文章推荐
- 手机连接PC通过串口发送PDU短信. 没有Iphone也可以欺骗了!
- 用 Java 通过串口发送手机短信(一)环境安装设置
- 用Java通过串口发送手机短信
- 用 Java 通过串口发送手机短信(二)简单使用
- 通过串口发送手机短信(C#版)
- java通过ssh2远程连接计算机并执行linux命令的方法(转)
- JAVA通过JDBC连接Mysql获取中文输出乱码解决方法
- Linux平台,使用JavaComm3 API及SMSLib项目实现在Web Application中发送手机短信的功能
- 用java通过串口发送短信,并且部署到tomcat
- Linux下发送中文短信到手机
- 通过串口发送手机短信(C#版)
- JAVA发送手机短信的最常见三种方法
- 通过串口发送手机短信(C#版)1
- 通过串口发送手机短信(C#版)2
- 通过串口发送手机短信(C#版)3
- 在JAVA中通过短信的形式发送到手机号码上
- 用串口连接GSM手机发送和接收短消息,在应用程序中如何编程实现?
- Arduino通过串口与linux上的java进程通讯
- 利用java 发送手机短信
- java本地方法通过jni连接至c函数