2016-09-12 30 views
1

我想做一個程序發送短信。我編寫了程序,但沒有成功發送消息。我的程序向我的計算機的端口COM發送一個At命令,但是我沒有收到我的GSM調制解調器的響應。我正在使用COM終端(Temp pro ...)使用at命令發送短信,並且我可以發送短信。所以我不知道爲什麼這個程序不能發送短信。發送命令到與gsm調制解調器與Java

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

而不是使用'調用println( 「ERR3」)的'放一個'e.printStackTrace()'在'catch'條款。這可能更好地解釋什麼是例外。在所有'catch'子句中這樣做,[編輯]你的問題並將堆棧跟蹤添加到它。記住要正確格式化它(不使用反引號,而是使用'{}'按鈕)。 – RealSkeptic

+0

什麼是你使用的jar文件** SerialPort ** –

回答

1

好的我做的程序,我可以發送短信。我使用CTRL + Z和Enter,這是一個問題。 :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();} 
     } 
    } 
} 

}}

相關問題