2016-09-23 12 views
0

SIM900을 기반으로하는 셀 모뎀을 사용하여 Java/Netbeans에서 문자 메시지를 보내려고합니다. TeraTerm을 사용하여 기본 AT 명령을 사용하여 모뎀을 사용하여 메시지를 보낼 수 있는지 확인합니다. 다음 코드는 jssc을 사용하여 메시지를 보내려고합니다.
오류가 발생하지 않고 데이터가 모뎀에 기록 된 것처럼 보였지만 문자 메시지는 수신되지 않습니다. 전화 번호는 +과 함께 사용하지 않고 시도했습니다.
TeraTerm에서 숫자는 +없이 작동해야합니다. 많은 변형이 시도되었고 많은 .println이 사용되었습니다. 아직 진전이 없습니다.
누군가 내 방식의 오류를 볼 수 있기를 바랍니다.java jssc 모뎀이 메시지를 보냅니다

미리 감사드립니다. Doug

package jssc_test; 

import jssc.SerialPort; 
import jssc.SerialPortException; 
import jssc.SerialPortList; 

public class Jssc_test { 

    public static SerialPort serialPort=null; 

    public static void main(String[] args) throws InterruptedException { 
     try { 
      String[] portNames = SerialPortList.getPortNames(); 
      for(int i = 0; i < portNames.length; i++){ 
       System.out.println(portNames[i]); 
      } 

      if(portNames.length < 1){ 
       System.out.println("No ports available"); 
       System.exit(0); 
      } 
      else{ 
       serialPort = new SerialPort(portNames[0]); 
      } 
      System.out.println("Port opened: " + serialPort.openPort()); 
      System.out.println("Params set: " + serialPort.setParams(9600, 8, 1, 0)); 
      System.out.println("\"Hello World!!!\" successfully writen to port: " + serialPort.writeBytes("Hello World!!!".getBytes())); 

      serialPort.writeBytes(" AT+CMGF=1".getBytes()); 
      Thread.sleep(1000); 
      System.out.println("bytes back = " + serialPort.readString()); 
      serialPort.writeBytes(" AT+CMGS=\"585*******\"".getBytes()); // \r = <CR>. Tried both with and without '+'. In TeraTerm, only works without +. error if use: \r\n 
      //Thread.sleep(1000); 
      //System.out.println("bytes back = " + serialPort.readString()); 
      //serialPort.writeBytes("0x0D".getBytes()); // send <CR> 
      Thread.sleep(1000); 
      System.out.println("bytes back = " + serialPort.readString()); 
      serialPort.writeBytes("THIS IS A TEST from DS.".getBytes()); // placing Cntr-Z string in text did not work: \u001A 
      //serialPort.writeBytes("0x0D".getBytes()); // send <CR> 
      serialPort.writeBytes("0x1A".getBytes()); // send <ctrl>Z 
      Thread.sleep(1000); 
      System.out.println("bytes back = " + serialPort.readString()); 

      System.out.println("Port closed: " + serialPort.closePort()); 
     } 
     catch (SerialPortException ex){ 
      System.out.println(ex); 
     } 
    } // ******************* end main *************** 

} // *********************** end main class *********************** 

답변

0

내가 위의 질문에 대답 할 수있었습니다. 아래 코드가 작동합니다. 이벤트 리스너가있을 필요는 없습니다. 주요 변경 사항은 새로운 줄과 파일 끝을 바이트로 정의하는 것입니다. "public static final Byte eof = 0x1A, nl = 0x0D;" "serialPort.writeByte (nl);"명령과 별도로 serialPort에 바이트를 씁니다. 나는 이것이 다른 사람들을 돕기를 바랍니다.

더그

PS : 나는 누군가가 관심이 있다면, JSSC를 사용하여 SIM900에 코드를 전송 단순화 할 수있는 자바 클래스를 썼다.

package jssc_test; 

//import jssc.SerialPort; 
//import jssc.SerialPortException; 
//import jssc.SerialPortList; 
import java.io.UnsupportedEncodingException; 
import java.util.Arrays; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import jssc.*; 
import static jssc.SerialPort.PURGE_RXCLEAR; 
import static jssc.SerialPort.PURGE_TXCLEAR; 
import static jssc_test.Jssc_test.serialPort; 
/** 
* 
* @author DStockman 
*/ 
public class Jssc_test { 

    public static SerialPort serialPort=null; 
    public static final Byte eof = 0x1A, nl=0x0D; 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws InterruptedException { 
     //SerialPort serialPort = null; 
    try { 
     String[] portNames = SerialPortList.getPortNames(); 
    for(int i = 0; i < portNames.length; i++){ 
     System.out.println(portNames[i]); 
    } 

     if(portNames.length < 1){ 
      System.out.println("No ports available"); 
      System.exit(0); 
     } 
     else{ 
      serialPort = new SerialPort(portNames[0]); 
     } 
     System.out.println("Port opened: " + serialPort.openPort()); 
     System.out.println("Params set: " + serialPort.setParams(9600, 8, 1, 0)); 

     serialPort.writeBytes("ATI".getBytes()); // get modem ID 
     serialPort.writeByte(nl); 
     Thread.sleep(1000); 
     System.out.println("modem ID = " + serialPort.getEventsMask()); 

     /* 
     int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask 
     serialPort.setEventsMask(mask);//Set mask 
     try{ 
      serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener 
     } 
     catch (SerialPortException ex) { 
      System.out.println(ex); 
     } 
     */ 

     // just looking up settings 
     System.out.println("events mask = " + serialPort.getEventsMask()); 
     System.out.println("flow control mode = " + serialPort.getFlowControlMode()); 
     System.out.println("output buffer bytes = " + serialPort.getOutputBufferBytesCount()); 
     //serialPort.purgePort(PURGE_RXCLEAR | PURGE_TXCLEAR); 

     serialPort.writeBytes(" AT+CMGF=1".getBytes()); 
     serialPort.writeByte(nl); 
     Thread.sleep(1000); 
     System.out.println("bytes back set modem to text mode = " + serialPort.readString()); 
     System.out.println("Success entering number: " + serialPort.writeBytes(" AT+CMGS=\"5857738696\";".getBytes())); // \r = <CR>. Tried both with and without '+'. In TeraTerm, only works without +. error if use: \r\n 
     serialPort.writeByte(nl); 
     Thread.sleep(1000); 
     System.out.println("bytes back after number entered = " + serialPort.readString()); 
     serialPort.writeBytes("THIS IS A third TEST from DS 09/29/16.2.".getBytes()); 
     serialPort.writeByte(nl); 
     Thread.sleep(1000); 
     serialPort.writeByte(eof); 
     Thread.sleep(1000); 
     System.out.println("bytes back = " + serialPort.readString()); 
     Thread.sleep(10000); 
     //serialPort.purgePort(SerialPort.PURGE_TXCLEAR); 

     //attempt to get msgs received by modem 
     serialPort.writeBytes("AT+CMGL=\"ALL\"".getBytes()); 
     serialPort.writeByte(nl); 
     Thread.sleep(1000); 
     System.out.println("bytes back = " + serialPort.readString()); 

     System.out.println("Port closed: " + serialPort.closePort()); 
    } 
    catch (SerialPortException ex){ 
     System.out.println(ex); 
    } 
    } // ******************* end main *************** 

} // *********************** end main class *********************** 


class SerialPortReader implements SerialPortEventListener { 

    public void serialEvent(SerialPortEvent event) { 
     if(event.isRXCHAR()){//If data is available 
      if(event.getEventValue() == 1){//Check bytes count in the input buffer 
       //Read data, if 10 bytes available 
       try { 
        System.out.println("bytes back inside listener = " + serialPort.readString()); 
        byte buffer[] = Jssc_test.serialPort.readBytes(10); 
        System.out.println("listener text:"); 
        System.out.print(Arrays.toString(buffer)); 
        System.out.println("End listener text:"); 
       } 
       catch (SerialPortException ex) { 
        System.out.println(ex); 
       } 
      } 
     } 
     else if(event.isCTS()){//If CTS line has changed state 
      if(event.getEventValue() == 1){//If line is ON 
       System.out.println("CTS - ON"); 
      } 
      else { 
       System.out.println("CTS - OFF"); 
      } 
     } 
     else if(event.isDSR()){///If DSR line has changed state 
      if(event.getEventValue() == 1){//If line is ON 
       System.out.println("DSR - ON"); 
      } 
      else { 
       System.out.println("DSR - OFF"); 
      } 
     } 
    } 
}