Java串口访问控制短信猫发送短信实例

//注意手机号码要奇偶移位 1369... -> 3196...

import gnu.io.*;
import java.util.*;
import java.io.*;

public class CommTest
{
 static CommPortIdentifier portId;
 static Enumeration portList;
 static int bauds[] = { 9600,115200 ........};

 public static void main(String[] args)
 {
  portList = CommPortIdentifier.getPortIdentifiers();

  while (portList.hasMoreElements())
  {
   portId = (CommPortIdentifier) portList.nextElement();
   if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
   {
    System.out.println("Found port: " + portId.getName());
    for (int i = 0; i < bauds.length; i++)
    {
     System.out.println("Trying at " + bauds[i] + "...");
     SerialPort serialPort = null;
     try
     {      
      InputStream inStream;
      OutputStream outStream;
      int c;
      String response;

      serialPort = (SerialPort) portId.open("SMS", 2000);
      serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
      serialPort.setSerialPortParams(bauds[i], SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
      inStream = serialPort.getInputStream();
      outStream = serialPort.getOutputStream();
      serialPort.enableReceiveTimeout(1000);

      c = inStream.read();
      while (c != -1) c = inStream.read();
      outStream.write('A');
      outStream.write('T');
      outStream.write('/r');
      try { Thread.sleep(1000); } catch (Exception e) {}
      response = "";
      c = inStream.read();
      while (c != -1)
      {
       response += (char) c;
       c = inStream.read();
      }
      if (response.indexOf("OK") >= 0)
      {
       try
       {
        System.out.println("Getting Info...");
        outStream.write("AT+CMGF=0/r".getBytes());
        getReply(inStream);
        outStream.write("AT+CMGS=19/r".getBytes());                
        getReply(inStream);
        outStream.write(("0011000D9168XXXXXXXXXXXF90008AA044F60597D"+(char)0x1a).getBytes());
        getReply(inStream);
       }
       catch (Exception e)
       {
        e.printStackTrace();
       }
      }
      else{       
       System.out.println("短信猫状态错误!"+response);
      }
      
     }
     catch (Exception e)
     {      
      System.out.println("  Nobody here!");
     }
     finally{
      serialPort.close();
     }
    }
   }
  }
 }
 
 private static void getReply(InputStream inStream){  
  try {
   String response="";
   int c = inStream.read();
   while (c != -1)
   {
    response += (char) c;
    c = inStream.read();
   }
   System.out.println("Reply:" + response.replaceAll("/n", " "). replaceAll("/r", ""));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的Java短信接口的示例代码: ```java import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import gnu.io.CommPortIdentifier; import gnu.io.NoSuchPortException; import gnu.io.PortInUseException; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; public class SMSInterface implements SerialPortEventListener { private static final int TIME_OUT = 2000; private static final int DATA_RATE = 9600; private SerialPort serialPort; private InputStream input; private OutputStream output; private List<String> messages; public List<String> getMessages() { return messages; } public void initialize(String portName) { CommPortIdentifier portId = null; Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers(); while (portEnum.hasMoreElements()) { CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); if (currPortId.getName().equals(portName)) { portId = currPortId; break; } } if (portId == null) { System.out.println("Could not find COM port."); return; } try { serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT); serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); input = serialPort.getInputStream(); output = serialPort.getOutputStream(); serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); messages = new ArrayList<String>(); } catch (PortInUseException | IOException | gnu.io.UnsupportedCommOperationException e) { System.err.println(e.toString()); } } public void send(String phoneNumber, String message) { try { output.write(("AT+CMGF=1" + "\r\n").getBytes()); Thread.sleep(1000); output.write(("AT+CMGS=\"" + phoneNumber + "\"\r\n").getBytes()); Thread.sleep(1000); output.write((message + "\r\n").getBytes()); Thread.sleep(1000); output.write((char) 26); } catch (IOException | InterruptedException e) { System.err.println(e.toString()); } } @Override public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { int available = input.available(); byte[] chunk = new byte[available]; input.read(chunk, 0, available); String message = new String(chunk); messages.add(message); } catch (IOException e) { System.err.println(e.toString()); } } } public void close() { if (serialPort != null) { serialPort.removeEventListener(); serialPort.close(); } } } ``` 这个示例代码使用了RXTXcomm库,你需要将它导入到你的Java项目中。这个示例代码连接到指定的串口,然后提供了发送短信的方法,可以向指定的手机号发送短信。它还实现了SerialPortEventListener接口,可以接收来自串口的数据,并将它们存储在消息列表中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值