⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendmessagepdu.java

📁 wap短信发送
💻 JAVA
字号:
/*
 * Created on 2005-9-21
 * 
 */

/**
 * @author XenonGas Copyright http://www.04tech.com
 *  
 */

package net.mcool.www.message;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;

public class SendMessagePDU {

	static Enumeration portList;

	static CommPortIdentifier portId;

	static SerialPort serialPort;

	static OutputStream OutputStream;

	static InputStream InputStream;

	private String message;

	private int commandDelay;

	private String replyString;

	private int getReplyInterval;

	// ********************************************************************
	// 对串口的读写操作
	// 方法: writeToSerialPort 对串口的写操作
	// 参数: msgString 传递字符串到接口
	private void writeToSerialPort(String msgString) {
		try {
			SendMessagePDU.OutputStream.write(msgString.getBytes());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	// 方法: waitForRead 等待返回信息
	// 参数: waitTime 进程等待时间
	private void waitForRead(int waitTime) {
		try {
			Thread.sleep(waitTime);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	// ********************************************************************
	// ********************************************************************
	// 发送信息:
	// 1. 打开串口
	// 2. at + cmgf = 0 设置待发送短消息的格式。
	// 3. 转换传来的字符串(参数messageString)。
	// 4. at + cmgs = 被转换完成的16位字串。
	// 5. System.out.println() 返回到命令行串口的信息。
	// 6. 关闭IO流和串口
	public void sendmsg(String messageString, String phoneNumber)
			throws Exception {
		// 打开串口
		portList = CommPortIdentifier.getPortIdentifiers();
		while (portList.hasMoreElements()) {
			portId = (CommPortIdentifier) portList.nextElement();
			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				if (portId.getName().equals("COM1")) {
					try {
						serialPort = (SerialPort) portId.open("SendMessagePDU",
								2000);
					} catch (PortInUseException e) {
						e.printStackTrace();
					}
					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();
					}
				}
			}
		}
		// ********

		//选择短消息格式: 0-PDU; 1-文本
		writeToSerialPort("AT+CMGF=0\r");
		waitForRead(this.commandDelay);
		byte[] readBuffer = new byte[1];
		int msglength;
		String sendmessage, tempSendString;
		msglength = messageString.length();
		//AT命令: 发送短消息
		String atcmgs = "AT+CMGS=" + (msglength * 2 + 15);
		// "000801"说明:分为00,08,01,
		// "00",普通GSM类型,点到点方式
		// "08",UCS2编码
		// "01",有效期
		if (msglength < 8) {
			tempSendString = "000800" + "0"
					+ Integer.toHexString(msglength * 2).toUpperCase()
					+ asc2unicode(new StringBuffer(messageString));
		} else {
			tempSendString = "000800"
					+ Integer.toHexString(msglength * 2).toUpperCase()
					+ asc2unicode(new StringBuffer(messageString));
		}
		// "011000D91" 说明:PDU代码
		// "91", "+"字符
		//
		if (phoneNumber.trim().length() > 0) {
			sendmessage = "011000D91" + "68" + changePhoneNumber(phoneNumber)
					+ tempSendString;
			this.message = "";
			
			writeToSerialPort(atcmgs + "\r\n");//发送命令: 发送短消息
			waitForRead(this.commandDelay);
			writeToSerialPort(sendmessage + (char) 26);
			waitForRead(this.commandDelay);
			try {
				InputStream = serialPort.getInputStream();
			} catch (IOException e) {
			}
			try {
				while (InputStream.available() > 0) {
					int numBytes = InputStream.read(readBuffer);
					waitForRead(this.commandDelay);
					System.out.print(new String(readBuffer));
				}
			} catch (IOException e) {
			}
			waitForRead(this.commandDelay);
		}
		// System.out.println("\n");
		closeIOStream();
		closeSerialPort();
		message = "";
		// System.out.println("发送完毕!");
	}

	// 修正号码在内存中的表示,每2位为1组,每组2个数字交换,
	// 若号码个数为奇数,则在末尾补'F'凑成偶数,然后再进行变换,
	// 因为在计算机中,表示数字高低位顺序与我们的习惯相反.
	// 如:"8613851872468" --> "683158812764F8"
	private String changePhoneNumber(String phoneNumber) {
		int numberLength = phoneNumber.length();
		if (phoneNumber.length() % 2 != 0) {
			phoneNumber = phoneNumber + "F";
			numberLength += 1;
		}
		char newPhoneNumber[] = new char[numberLength];
		for (int i = 0; i < numberLength; i += 2) {
			newPhoneNumber[i] = phoneNumber.charAt(i + 1);
			newPhoneNumber[i + 1] = phoneNumber.charAt(i);
		}
		return (new String(newPhoneNumber));
	}

	private String reversalPhoneNumber(String phoneNumber) {
		int numberLength = phoneNumber.length();
		char newPhoneNumber[] = new char[numberLength];
		for (int i = 0; i < numberLength; i += 2) {
			newPhoneNumber[i] = phoneNumber.charAt(i + 1);
			newPhoneNumber[i + 1] = phoneNumber.charAt(i);
		}
		String phone = new String(newPhoneNumber);
		if(newPhoneNumber[numberLength] == 'F'){
			phone = phone.substring(0, phone.length() - 1);
		}
		return phone;
	}

	// 将要发送的字符串(String messageString)转换为UNICODE编码
	private String asc2unicode(StringBuffer msgString) {
		StringBuffer msgReturn = new StringBuffer();
		int msgLength = msgString.length();
		if (msgLength > 0) {
			for (int i = 0; i < msgLength; i++) {
				new Integer((int) msgString.charAt(0)).toString();
				msgReturn.append(new StringBuffer());
				String msgCheck = new String(Integer
						.toHexString((int) msgString.charAt(i)));
				if (msgCheck.length() < 4) {
					msgCheck = "00" + msgCheck;
				}
				msgReturn.append(new StringBuffer(msgCheck));
			}
		}
		return (new String(msgReturn).toUpperCase());
	}
	private String unicode2asc(StringBuffer msgString) {
		StringBuffer msgReturn = new StringBuffer();
		int msgLength = msgString.length();
		if (msgLength > 0) {
			for (int i = 0; i < msgLength; i++) {
//				new Integer((int) msgString.charAt(0)).toString();
//				msgReturn.append(new StringBuffer());
				String msgCheck = new String(Integer.toHexString((int) msgString.charAt(i)));
				if (msgCheck.length() < 4) {
					msgCheck = "00" + msgCheck;
				}
				msgReturn.append(new StringBuffer(msgCheck));
			}
		}
		return (new String(msgReturn).toUpperCase());
	}

	// *****************************************************************
	// ********************************************************************
	// 操作结束,关闭所用资源
	public void closeSerialPort() {
		if (SendMessagePDU.serialPort != null) {
			try {
				SendMessagePDU.serialPort.close();
			} catch (RuntimeException e) {
				System.out.println(e.getMessage());
			}
		}
		// System.out.println("已断开连接!");
	}

	public void closeIOStream() {
		if (SendMessagePDU.InputStream != null) {
			try {
				SendMessagePDU.InputStream.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
		if (SendMessagePDU.OutputStream != null) {
			try {
				SendMessagePDU.OutputStream.close();
			} catch (IOException e1) {
				System.out.println(e1.getMessage());
			}
		}
		// System.out.println("已关闭I/O流!");
	}

	// ********************************************************************

	public boolean sendSMS(String message, String phoneNumber) {
		try {
			sendmsg(message, phoneNumber);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static void main(String[] args) {
		SendMessagePDU smp = new SendMessagePDU();
		smp.sendSMS("您好您好", "13521262171");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -