java调用AT指令发送短信问题!!!

RXTXcomm.jar实现Java串口通信,发送信息时没报错,但也没收到信息。请帮忙看下什么原因呢?


以下源码:

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) {
}


}


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

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值