Send At Command to gsm modem with Java

前端 未结 1 1753
臣服心动
臣服心动 2021-01-15 07:30

I am trying to make a program to send a sms. I wrote the program but doesn\'t successfully send the message. My program send an At command to the Port COM in my computer but

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-15 08:05

    Ok I make the program and I can send a SMS. I use CTRL+Z and Enter that was a problem. :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();}
            }
        }
    }
    

    } }

    0 讨论(0)
提交回复
热议问题