java - 如何在 java 中使用 AT 命令将短信发送到 CDMA 调制解调器?

标签 java serial-port zooming at-command cdma

我正在尝试在 windows-7 上制作一个简单的 java 应用程序,它可以在我的 citycell Zoom USB 调制解调器上发送 SMS。我使用 AT 命令(http://www.canarysystems.com/nsupport/CDMA_AT_Commands.pdf)。流动我的 Java 代码:

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

public class SimpleWrite implements Runnable, SerialPortEventListener {

    public void run() {
    }
    static Enumeration portList;
    static CommPortIdentifier portId;
    static String messageString = "AAA";
    static char ch = '"';
    static String dest = ch + "01739557775" + ch;  // 11 Digit Mobile Number.
    static InputStream inputStream;
    static SerialPort serialPort;
    static OutputStream outputStream;

public void serialEvent(SerialPortEvent event) {
    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: {

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            try {

                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                System.err.println("Error while reading Port " + e);
            }
            break;

        }
    } //switch
}

public SimpleWrite(SerialPort serial) {
    try {
        inputStream = serial.getInputStream();
        try {
            serial.addEventListener(this);
        } catch (TooManyListenersException e) {
            System.out.println("Exception in Adding Listener" + e);
        }
        serial.notifyOnDataAvailable(true);

    } catch (Exception ex) {
        System.out.println("Exception in getting InputStream" + ex);
    }

}

public static void main(String[] args) {
    String line1 = "AT+CSMS=1\r\n";
    String line2 = "AT+CMGS=" + dest + "\r\n";
    String line3 = messageString + "\r\n";

    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (portId.getName().equals("COM13")) {
                System.out.println("SMS Sending....Port Found");
                try {
                    serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                    SimpleWrite wr = new SimpleWrite(serialPort);

                } catch (PortInUseException e) {
                    System.out.println("Port In Use " + e);
                }
                try {
                    outputStream = serialPort.getOutputStream();
                } catch (IOException e) {
                    System.out.println("Error writing to output stream " + e);
                }
                try {
                    serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                } catch (UnsupportedCommOperationException e) {
                }
                try {
                    outputStream.write(line1.getBytes());
                    outputStream.write(line1.getBytes());
                    outputStream.write(line2.getBytes());
                    outputStream.write(line3.getBytes());
                    outputStream.write(26);
                    outputStream.flush();
                } catch (Exception e) {
                    System.out.println("Error writing message " + e);
                }
            }
        }
    }
}

/**
 * show text in the text window
 *
 * @param Text text string to show on the display
 */
public static void showText(String Text) {
    System.out.println(Text);
  }
}

输出:

SMS Sending....Port Found
AT+CSMS=1

ERROR
AT+CSMS=1

ERROR
AT+CMGS="01739557775"

ERROR

请帮帮我。

最佳答案

尝试在CDMA中为AT命令附加H,例如发送时使用:

AT+HCMGS="01739557775"

关于java - 如何在 java 中使用 AT 命令将短信发送到 CDMA 调制解调器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17691632/

相关文章:

java - 对形状进行分组,以便我可以通过单击鼠标循环浏览它们 - 处理和 java

java - 如何使用 ExecutorService 跟踪任务执行统计信息?

linux - UDEV-如何获取子设备属性的值

Python:serial.readline() - 如何定义从\n到\n\n的EOL

javascript - 如何缩放 Canvas ?

java - EasyMock 和返回泛型类的方法

java - 如何使用带注释的查询让 PageRequest 在 Spring Boot 中工作

javascript - 如何通过串口将数据从网站发送到处理器?

c++ - 如何使用 Win32/WTL 实现 ListView 的缩放

java - Android 开发 - 使用 PNG 作为 map ,标记当前位置