java sendcommand_Send At Command to gsm modem with Java

该博客讨论了作者在尝试使用Java程序通过COM3端口向GSM调制解调器发送AT命令发送短信时遇到的问题。程序能够成功建立连接并发送命令,但无法收到响应。作者分享了修改后的代码,问题在于缺少了发送结束符。在修正后的代码中,通过添加Ctrl+Z(ASCII码26)作为结束符,程序能够成功发送短信。
摘要由CSDN通过智能技术生成

问题

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 \r";

static String messageString2 = "AT+CMGS=\"+4866467xxxx\"\r";

static String messageString3 = "TESting \u001A\r\n";

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

}

}

}

}

}

回答1:

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

}

}

}

}

}

来源:https://stackoverflow.com/questions/39444744/send-at-command-to-gsm-modem-with-java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值