2016-09-12 5 views
1

SMS를 보내려는 프로그램을 만들려고합니다. 프로그램을 작성했지만 메시지를 보내지 못했습니다. 내 프로그램이 내 컴퓨터의 포트 COM에 At 명령을 보내지 만 gsm 모뎀에서 응답을받지 못합니다. COM 터미널 (Temp pro ...)을 사용하여 at 명령과 함께 sm을 보내면 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' 절에서 그렇게하고, [edit] 질문하고 스택 추적을 추가하십시오. 올바른 포맷을하는 것을 잊지 마십시오 (백틱을 사용하지 않고'{}'버튼 사용). – RealSkeptic

+0

** SerialPort **에 사용 된 jar 파일은 무엇입니까? –

답변

1

좋아요. 나는 프로그램을 만들고 SMS를 보낼 수 있습니다. 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();} 
     } 
    } 
} 

} } 대신`에 println ("ERR3")를 사용