2016-09-12 5 views
1

私はSMSを送信するプログラムを作ろうとしています。私はプログラムを書いたが、メッセージを正常に送信しなかった。私のプログラムは、私のコンピュータのポートCOMにAtコマンドを送信しますが、私のgsmモデムからの応答は得られません。私はCOMターミナル(Temp pro ...)を使用してatコマンドでsmsを送信しています。私はsmsを送信できます。したがって、私はなぜプログラムがSMSを送信できないのか分かりません。Javaでgsmモデムにコマンドを送信

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

' 'e.printStackTrace()' 'あなたのcatch'で句を置きます。それは例外が何であるかをよりよく説明するかもしれない。すべての 'catch'節でそれを行い、あなたの質問を編集し、スタックトレースを追加します。適切に書式を設定することを忘れないでください(バッククォートを使用せず、 '{}'ボタンを使用してください)。 – RealSkeptic

+0

** SerialPort **用に使用したJARファイルは何ですか? –

答えて

1

私はプログラムを作成してSMSを送信できます。私はCtrl + Zキーを使用して問題が発生しました。 :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();} 
     } 
    } 
} 

}} 代わりに `printlnを( "ERR3")を使用しての

関連する問題