
2008-3-11 13:45
鼎月星辰
请教一个java发送AT指令的代码是否正确
各位大哥,小弟打算向一个CDMA的modem中发送一条AT命令
代码如下:
outputStream.write("AT+CGMI".getBytes());
outputStream.write("AT+CMGS='13800000000',10 ".getBytes());
outputStream.write("0x600xa80x590x7d0x000x1a".getBytes());
请问,这样可以达到向号码为13800000000的手机发送一条“您好”的短信么?如果不行,请赐教应该怎么写?:idle:
2008-3-11 20:29
鼎月星辰
全部的代码如下
public class J2MEMain {
/**
* @param args
* 本类是发送短信的主类,负责调用其他类对象的函数
*/
public static void main(String[] args) {
SMSSend smsSend=new SMSSend();
smsSend.sendSMS();
}
}
//-----------------------------------------------------------------------------------------------------
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.*;
import java.util.Enumeration;
import java.util.TooManyListenersException;
/*
* 短信发送的主要功能类
*/
public class SMSSend {
SerialPort serialPort=null;
CommPortIdentifier portId = null;
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
final SMSReturnEventListener sMSReturnEventListener=new SMSReturnEventListener();
public SMSSend(){
while (portList.hasMoreElements()){
CommPortIdentifier com = (CommPortIdentifier)portList.nextElement();
if (com.getPortType() == CommPortIdentifier.PORT_SERIAL){
portId=com;//由于已知机器上只有一个串口,因此这样就可以
break;
}
}
try {
serialPort=(SerialPort)portId.open("SMS", 2000);
System.out.println(serialPort.getName());
} catch (PortInUseException e) {
System.out.println("PortInUseException");
e.printStackTrace();
}
try {
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, //设置串口读写参数
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
} catch (UnsupportedCommOperationException e) {
}
}//SMSSend()
public void sendSMS(){
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(serialPort.getInputStream());
outputStream = new BufferedOutputStream(serialPort.getOutputStream());
sMSReturnEventListener.setInputStream(inputStream);
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(sMSReturnEventListener);
outputStream.write("AT+CMGF=1".getBytes());
outputStream.write("AT+CNMI=2,2,0,0,0".getBytes());
outputStream.write("AT+CMPS=17,23,64,244".getBytes());
outputStream.write("AT+CGMI".getBytes());
outputStream.write("AT+CMGS='13880458775',10 ".getBytes());
outputStream.write("0x600xa80x590x7d0x000x1a".getBytes());
// outputStream.write(("您好".getBytes("GB2312")+"0x000x1a").getBytes());
// 测试如何转换汉字为Unicode,希望成功
// 也可以用AT指令调试精灵来把任何的语句进行Unicode编码
} catch (IOException e) {
System.err.println("发生了某些异常");
e.printStackTrace();
}catch (TooManyListenersException e) {
e.printStackTrace();
}finally{
try {
inputStream.close();
outputStream.close();
serialPort.close();
} catch (IOException e) {
e.printStackTrace();
}//这一句很重要,必须关闭串口资源
}
}
}//class
//----------------------------------------------------------------------------------------------------
import java.io.IOException;
import java.io.InputStream;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
/*
* 短信发送AT指令可能有返回值,本事件监听器专门监听返回值
*/
public class SMSReturnEventListener implements SerialPortEventListener {
private InputStream inputStream;
@Override
public void serialEvent(SerialPortEvent event) {
System.out.println("得到了一些返回值");
/*now I don't want to insert some programme into this block ,because
* I can't be sure that the super block would work well
*/
InputStream inputStream=null;
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE://当有可用数据时读取数据,并且给串口返回数据
byte[] readBuffer = new byte[1024];
try {
while (inputStream.available() > 0) {//下一次可以不受阻塞而读取的字节数
int numBytes = inputStream.read(readBuffer);//从输入流中读取一定数量的字节,并将其存储在缓冲区数组readBuffer中。以整数形式返回实际读取的字节数。
}
System.out.println(new String(readBuffer));
} catch (IOException e){
}finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
}
public void setInputStream(InputStream inputStream){
this.inputStream=inputStream;
}
}
2008-3-12 22:22
鼎月星辰
有没有搞错,这么大的论坛,这么多高人,就没人能帮我解决一个简单的问题么?:confused:
2008-3-21 23:06
voca
。。。。。。
这个怎么测试哦
我看我这边的哥们是发给直接到手机作测试
试试看就知道了阿
2008-4-26 11:47
tc35
我最近也在做这方面的开发,看过你的代码,基本就是JCOMM里例子.如果执行正确会有返回值比如字符OK,或数字格式的OK,可以用at设置的.判断返回值就能判断发送有没有成功[email=.[email]55407501@163.com[/email]]. [email]55407501@163.com[/email][/email] 大家交流,看时间可能你已经解决了,呵呵
页:
[1]

Powered by ITPUB论坛