简体   繁体   English

java jssc modem发送消息

[英]java jssc modem send msg

I am trying to send text messages from Java/Netbeans using a cell modem based on the SIM900 . 我正在尝试使用基于SIM900的单元调制解调器从Java / Netbeans发送文本消息。 Using TeraTerm, I verify that I can send messages using the modem with basic AT commands. 使用TeraTerm,我确认可以使用带有基本AT命令的调制解调器发送消息。 The following code attempts to use jssc to send messages. 以下代码尝试使用jssc发送消息。
I do not get errors and the data appears to be written to the modem, but I never receive a text message. 我没有收到错误,并且数据似乎已写入调制解调器,但是我从未收到任何短信。 For the phone number, I have tried both with an + , and without. 对于电话号码,我尝试使用+或不使用。
In TeraTerm, the number must be without the + to work. 在TeraTerm中,数字必须没有+才能起作用。 Many variations have been tried, and many .println 's used. 已经尝试了许多变体,并使用了许多.println Still not making progress. 仍然没有进步。
I hope someone can see the error of my ways. 我希望有人能看到我的方式的错误。

Thanks in advance. 提前致谢。 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 ***********************

I was able to answer the question I posed above. 我能够回答我上面提出的问题。 Below code works. 下面的代码有效。 The event listener does not need to be in there. 事件侦听器不必在那里。 The major changes that helped are defining the new line and end of file as bytes "public static final Byte eof = 0x1A, nl=0x0D;" 有助于将文件的新行和结尾定义为字节的主要更改“公共静态最终字节eof = 0x1A,nl = 0x0D;” then writing the bytes to the serialPort separately from the commands "serialPort.writeByte(nl);". 然后将字节与命令“ serialPort.writeByte(nl);”分开写入串行端口。 I hope this helps others. 我希望这对其他人有帮助。

Doug 道格

PS: I wrote a java class that may simplify sending code to the SIM900 using jssc, if anyone is interested. PS:我编写了一个Java类,可以简化使用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");
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM