使用 Java 将 At 命令发送到 gsm 调制解调器 [英] Send At Command to gsm modem with Java

查看:43
本文介绍了使用 Java 将 At 命令发送到 gsm 调制解调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个发送短信的程序.我编写了程序,但没有成功发送消息.我的程序向计算机中的端口 COM 发送 At 命令,但我的 gsm 调制解调器没有收到响应.我正在使用 COM 终端(Temp pro...)通过 at 命令发送短信,并且我能够发送短信.因此我不知道为什么程序不能发送短信.

I am trying to make a program to send a sms. I wrote the program but doesn't successfully send the message. My program send an At command to the Port COM in my computer but I do not get a response from my gsm modem. I am using COM terminal (Temp pro...) to send sms with the at command and I am able to send sms. Therefore I don't know why the program can't send a sms.

import java.io.*;
import java.util.*;
import gnu.io.*;

public class prawiefinal {
    static Enumeration portList;
    static CommPortIdentifier portId;
    static String messageString = "AT+CMGF=1 
";
    static String messageString2 = "AT+CMGS="+4866467xxxx"
";
    static String messageString3 = "TESting u001A
";
    static SerialPort serialPort;
    static OutputStream outputStream;


    public static void main(String[] args) throws InterruptedException {
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {

            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

                if (portId.getName().equals("COM3")) {

                    try {
                        serialPort = (SerialPort)
                            portId.open("COM3", 2000);
                    } catch (PortInUseException e) {System.out.println("err");}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {e.printStackTrace();}
                    try {
                        serialPort.setSerialPortParams(9600,
                                SerialPort.DATABITS_8,
                                SerialPort.STOPBITS_1,
                                SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {e.printStackTrace();}
                    try {
                        outputStream.write(messageString.getBytes());
                        Thread.sleep(3000); 
                        outputStream.flush();

                        outputStream.write(messageString2.getBytes());  
                        Thread.sleep(3000);  
                        outputStream.flush();

                        outputStream.write(messageString3.getBytes()); 
                        Thread.sleep(3000);  
                        outputStream.write(26);
                        outputStream.flush();
                        System.out.println(messageString);  
                        Thread.sleep(3000);






                        outputStream.close();
                        serialPort.close();

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

推荐答案

好的,我制作了程序,我可以发送短信.我使用 CTRL+Z 并输入这是一个问题.:D

Ok I make the program and I can send a SMS. I use CTRL+Z and Enter that was a problem. :D

import java.io.*; 
import java.util.*; 
import gnu.io.*; 

public class SMSsender { 
static Enumeration portList; 
static CommPortIdentifier portId; 
static String messageString1 = "AT";
static String messageString2 = "AT+CPIN="7078"";
static String messageString3 = "AT+CMGF=1"; 
static String messageString4 = "AT+CMGS="+4866467xxxx"";


static String messageString5 = "TESTY2";
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static char enter = 13;

static char CTRLZ = 26;
public static void main(String[] args) throws InterruptedException {
 portList = CommPortIdentifier.getPortIdentifiers();



while (portList.hasMoreElements()) {

    portId = (CommPortIdentifier) portList.nextElement();
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

         if (portId.getName().equals("COM3")) {

            try {
                serialPort = (SerialPort)
                    portId.open("COM3", 2000);
            } catch (PortInUseException e) {System.out.println("err");}
            try {
                outputStream = serialPort.getOutputStream();
                inputStream = serialPort.getInputStream();
            } catch (IOException e) {e.printStackTrace();}
            try {
                serialPort.setSerialPortParams(9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE); 
            } catch (UnsupportedCommOperationException e) {e.printStackTrace();}
            try { 

                outputStream.write((messageString1 + enter).getBytes());


                Thread.sleep(100); 
                outputStream.flush();

                outputStream.write((messageString2 + enter).getBytes()); 

                 Thread.sleep(100); 
                 outputStream.flush();

                outputStream.write((messageString3 + enter).getBytes());

                Thread.sleep(100); 
                outputStream.flush(); 




                outputStream.write((messageString4 + enter).getBytes()); 

                Thread.sleep(100);  
                outputStream.flush();

               outputStream.write((messageString5 + CTRLZ).getBytes());  

                outputStream.flush(); 
                Thread.sleep(100); 


    System.out.println("Wyslano wiadomosc");  
    Thread.sleep(3000);


    outputStream.close();
    serialPort.close();
    System.out.println("Port COM zamkniety"); 

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

}}

这篇关于使用 Java 将 At 命令发送到 gsm 调制解调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆