您的位置:首页 > 运维架构 > Linux

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下的连接方法

全部代码

 

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();
}

}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息