java电话系统_JAVA串口开发短信发送及电话拨打

本文提供了一个使用 Java 实现的短信发送示例代码。该示例利用了 smslib 库来通过串口连接调制解调器发送短信。文中详细展示了如何初始化串口、配置调制解调器网关、构建短信消息并发送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.[文件]

SmSMassage.java ~ 7KB

下载(89)

import org.smslib.Message;

import org.smslib.OutboundMessage;

import org.smslib.Service;

import org.smslib.helper.CommPortIdentifier;

import org.smslib.helper.SerialPort;

import org.smslib.modem.SerialModemGateway;

import javax.comm.PortInUseException;

import javax.comm.UnsupportedCommOperationException;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.*;

/**

* ------------------------------------------------

*

Copyright (C) All rights reserved by KK

*

* Developer: ZhouHong

* Date: 15-4-16 下午1:38

*

* Change Description

*

* ------------------------------------------------

*/

public class SmSMassage {

//获取COM口,注释放开即读配置文件中配置的COM串口

public String getPort() {

return "COM8";

// String courseFile = this.getClass().getResource("/smsPhone.properties").getPath();

// Properties prop = new Properties();

// try {

// prop.load(new FileInputStream(courseFile));

// return prop.getProperty("smsPort").equals("")?"COM1":prop.getProperty("smsPort");

// } catch (IOException e) {

// return "COM1";

// }

}

public static void main(String[] args) {

SmSMassage sos = new SmSMassage();

sos.SMSMessage(new String[]{"185xxxxxxxx"}, "测试短信");

try {

sos.SOSIphone("18516071901");

} catch (PortInUseException e) {

e.printStackTrace();

} catch (UnsupportedCommOperationException e) {

e.printStackTrace();

} catch (TooManyListenersException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

// smsIphone(port); //放入单独线程中运行

// smsMessage(map);//放入单独线程中运行

}

public void smsIphone(String telephone){

final String phone = telephone;

new Thread(){

@Override

public void run() {

try {

SOSIphone(phone);

} catch (PortInUseException e1) {

e1.printStackTrace();

} catch (UnsupportedCommOperationException e1) {

e1.printStackTrace();

} catch (TooManyListenersException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

} catch (InterruptedException e1) {

e1.printStackTrace();

}

}

}.start();

}

public void smsMessage(String[] telephones, String msg){

final String[] iphones = telephones;

final String text = msg;

new Thread(){

@Override

public void run() {

SMSMessage(iphones, text);

}

}.start();

}

private boolean checkPort(String port){

boolean flag = false;

for (Enumeration e = CommPortIdentifier

.getPortIdentifiers(); e.hasMoreElements();) {

CommPortIdentifier portId = e.nextElement();

if (portId.getName().equals(port)) {

flag = true;

break;

}

}

return flag;

}

private void SMSMessage(String[] phones,String text){

String port = new SmSMassage().getPort();

CommPortIdentifier portId = getPortP(port);

// SerialPort serialPort = null;

try{

if (portId!=null) {

if (phones.length<=0)

return;

// serialPort = portId.open("wavecom1", 1000);

// Service srv = Service.getInstance();

SerialModemGateway gateway = new SerialModemGateway("modem.com5",

port, 115200, "WAVECOM", "–");

gateway.setInbound(true);

gateway.setOutbound(true);

gateway.setSimPin("0000");

Service.getInstance().addGateway(gateway);

Service.getInstance().startService();

List outboundMessageList = new ArrayList();

for (String phone:phones){

OutboundMessage msg = new OutboundMessage(phone,text);

msg.setEncoding(Message.MessageEncodings.ENCUCS2);

outboundMessageList.add(msg);

}

Service.getInstance().sendMessages(outboundMessageList);

// for (AGateway g: srv.getGateways()) {

// g.stopGateway();

// }

gateway.stopGateway();

Service.getInstance().stopService();

Service.getInstance().removeGateway(gateway);

System.out.print("短信发送成功!");

}else{

System.out.print("找不到端口"+port);

}

}catch(Exception e){

e.getMessage();

}

}

private void SOSIphone(String telephone)throws PortInUseException, UnsupportedCommOperationException,

TooManyListenersException, IOException, InterruptedException{

String port = new SmSMassage().getPort();

CommPortIdentifier portId = getPortP(port);

if (portId!=null) {

final SerialPort serialPort = portId.open("wavecom1", 1000);

serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);

serialPort.setSerialPortParams(115200,// 波特率

SerialPort.DATABITS_8,// 数据位数

SerialPort.STOPBITS_1, // 停止位

SerialPort.PARITY_NONE);// 奇偶位

System.out.println("端口已打开。\n发送AT指令 …");

InputStream inputStream = serialPort.getInputStream();

OutputStream outputStream = serialPort.getOutputStream();

String at_cmd = "ATD"+telephone.trim()+";\r";

outputStream.write(at_cmd.getBytes());

Thread.sleep(2000);

byte[] data = new byte[1024];

inputStream.close();

outputStream.close();

serialPort.close();

System.out.println("关闭端口。");

}

}

//获取端口是否存在及端口状态

private static CommPortIdentifier getPortP(String port){

CommPortIdentifier comm = null;

for (Enumeration e = CommPortIdentifier

.getPortIdentifiers(); e.hasMoreElements();) {

CommPortIdentifier portId = e.nextElement();

if (portId.getName().equals(port)) {

comm = portId;

break;

}

}

return comm;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值