gpt4 book ai didi

Java短信网关可以连接通讯端口但不发送消息

转载 作者:行者123 更新时间:2023-12-01 05:23:40 24 4
gpt4 key购买 nike

这是一个应该向任何号码发送短信的程序,但失败了。它可以与端口连接,但不发送任何消息。请有人帮忙吗?

package mateorssms;

import java.io.*;
import java.util.*;
import javax.comm.*;


public class GSMConnect implements SerialPortEventListener,
CommPortOwnershipListener {

private String comPort = "COM3"; // This COM Port must be connect with GSM Modem or your mobile phone
private String messageString = "hi there!";
private CommPortIdentifier portId = null;
private Enumeration portList;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private SerialPort serialPort;

/** Creates a new instance of GSMConnect */
public GSMConnect(String comm) {

//this.comPort = comm;

}

public boolean init() {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comPort)) {
return true;
}
}
}
return false;
}

public void checkStatus() {
send("AT+CREG?\r\n");
//reurn outt;
}

public void dial(String phoneNumber) {
try {
//dial to this phone number
messageString = "ATD" + phoneNumber + ";\n\r";
outputStream.write(messageString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}


public void send(String cmd) {
try {
outputStream.write(cmd.getBytes());
inputStream = serialPort.getInputStream();
System.out.println(" Input Stream... " + inputStream.toString());

} catch (IOException e){
e.printStackTrace();
}

}

public void sendMessage(String phoneNumber, String message) {
//send("AT+CMGS=\"" + phoneNumber + "\"\r\n");

send("AT+CMGS=\""+phoneNumber+"\"\r\n");
send(message + '\032');

}



public void hangup() {
send("ATH\r\n");
}





public void connect() throws NullPointerException {
if (portId != null) {
try {
portId.addPortOwnershipListener(this);
serialPort = (SerialPort) portId.open("MobileGateWay", 2000);
} catch (PortInUseException e) {
e.printStackTrace();
}

try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}

try {
/** These are the events we want to know about*/
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}

//Register to home network of sim card

send("ATZ\r\n");

} else {
throw new NullPointerException("COM Port not found!!");
}
}

@Override
public void serialEvent(javax.comm.SerialPortEvent serialPortEvent) {
switch (serialPortEvent.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:
case SerialPortEvent.DATA_AVAILABLE:

byte[] readBuffer = new byte[2048];
try {
while (inputStream.available()>0)
{
int numBytes = inputStream.read(readBuffer);
}
//print response message
System.out.print(new String(readBuffer));
} catch (IOException e) {
}
break;
}
}

@Override
public void ownershipChange(int type) {
switch (type) {
case CommPortOwnershipListener.PORT_UNOWNED:
System.out.println(portId.getName() + ": PORT_UNOWNED");
break;
case CommPortOwnershipListener.PORT_OWNED:
System.out.println(portId.getName() + ": PORT_OWNED");
break;
case CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED:
System.out.println(portId.getName() + ": PORT_INUSED");
break;
}

}

public static void main(String args[]) {
GSMConnect gsm = new GSMConnect("COM3");
if (gsm.init()){
try {
gsm.connect();
gsm.checkStatus();
Thread.sleep(3000);
gsm.sendMessage("01911507103", "Your Message");
Thread.sleep(3000);
gsm.hangup();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Can't init this card");
}
}
}

这是我在 Netbeans 7.1 中运行时的输出。

run:
COM3: PORT_OWNED
Input Stream... com.sun.comm.Win32SerialInputStream@a32b
Input Stream... com.sun.comm.Win32SerialInputStream@a32b
ATZ
OK
Input Stream... com.sun.comm.Win32SerialInputStream@a32b
AT+CMGS="01911507103"
Input Stream... com.sun.comm.Win32SerialInputStream@a32b
ERROR
Input Stream... com.sun.comm.Win32SerialInputStream@a32b
ATH
OK

提前致谢...

最佳答案

尝试使用AT+CMGF=1将调制解调器置于短信模式

如果还是不行,请尝试发送短信AT+CMGW="<number>"\r\n<message>它将把短信存储在内存中并返回一个索引,例如+CMGW: <index> 。然后发出命令AT+CMSS=<index>实际发送它。

另外两件事可能会有所帮助;

  • flush() send 方法中的输出流。
  • 将两者结合send()里面的操作sendMessage()只有一个send()

像这样:

public void sendMessage(String phoneNumber, String message) {
send("AT+CMGS=\""+phoneNumber+"\"\r\n" + message + '\032');
}

关于Java短信网关可以连接通讯端口但不发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9994906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com