linux下用java通过串口连接手机或短信猫发送中文短信的方法
作者:r_elease靜 | 来源:互联网 | 2017-08-01 10:57
linux下用java通过串口连接手机或短信猫发送中文短信的方法--Linux通用技术-Linux编程与内核信息,下面是详情阅读。
下载jar包,解压,把其中so文件复制到/usr/lib
把javax.comm.properties复制到jre目录下
把comm.jar放到classpath目录下
串口连接:
CommPortIdentifier portID;
String owner = new String("modem");
int keeptime = 5000;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();
// String driverName = "com.sun.comm.Win32Driver";
String driverName = "com.sun.comm.LinuxDriver";
CommDriver driver = null;
try {
// System.loadLibrary("win32com");
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifier) portList.nextElement();
System.out.println("COM:" + portID.getName());
// if (portID.getName().equals("COM1"))
if (portID.getName().equals("/dev/ttyS0"))
try {
sPort = (SerialPort) portID.open(owner, keeptime);
break;
}catch (PortInUseException e) {
e.printStackTrace();
System.exit(1);
}
}
try{
if (sPort != null)
{
in = sPort.getInputStream();
out = sPort.getOutputStream();
try {
sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
}catch(Exception e)
{
e.printStackTrace();
}
其中注释掉的是windows下的连接方法
全部代码
import javax.comm.*;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import java.io.*;
public class ATSms implements SerialPortEventListener {
private SerialPort sPort = null;
private InputStream in = null;
private OutputStream out = null;
private byte b[] = new byte[1024];
private String cmd = "";
private int len = 0;
private String recvMsg = "";
public ATSms()
{
init();
}
public synchronized void serialEvent(SerialPortEvent serialPortEvent) {
int c = 0;
// 如果有串口事件发生
if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try {
System.out.print("Recv :" );
while ((c = in.read()) != -1) {
System.out.print((char)c);
recvMsg += ((char)c);
}
}// try
catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getUnicodeString(String s)
{
String unicodeStr = "";
for(int i=0;i
{
String c = Integer.toHexString(s.charAt(i) & 0xffff);
while(c.length()<4)
{
c = "0" + c;
}
unicodeStr += c;
}
return unicodeStr;
}
private static String getSMobile(String mobile)
{
StringBuffer sMobile = new StringBuffer("68");
for( int i=0;i
{
sMobile.append((i+1
sMobile.append(mobile.charAt(i));
}
return sMobile.toString();
}
private void init()
{
CommPortIdentifier portID;
String owner = new String("modem");
int keeptime = 5000;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();
// String driverName = "com.sun.comm.Win32Driver";
String driverName = "com.sun.comm.LinuxDriver";
CommDriver driver = null;
try {
// System.loadLibrary("win32com");
// System.out.println("Win32Com Library Loaded");
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifier) portList.nextElement();
System.out.println("COM:" + portID.getName());
// if (portID.getName().equals("COM1"))
if (portID.getName().equals("/dev/ttyS0"))
try {
// System.out.println("try to open the /dev/ttyS0");
sPort = (SerialPort) portID.open(owner, keeptime);
// System.out.println("try to open the /dev/ttyS0 OK");
break;
}catch (PortInUseException e) {
e.printStackTrace();
System.exit(1);
}
}
try{
if (sPort != null)
{
in = sPort.getInputStream();
out = sPort.getOutputStream();
try {
sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
/**
System.out.print("set PDU mode :");
out.write("at+cmgf=0\r".getBytes());
out.flush();
// len = in.read(b);
for(int i=0;i<3;i++)
{
recvMsg = readThread.readComMsg();
if(!"".equals(recvMsg))break;
Thread.sleep(1000);
System.out.println("Sleep 1001.");
}
if(recvMsg.indexOf("OK")>=0)
{
System.out.println("OK");
}else
{
System.out.println("ERROR");
}
*/
}catch(Exception e)
{
e.printStackTrace();
}
}
public void close()
{
try {
Thread.sleep(4000);
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
sPort.close();
}
public int sendSms(String recv,String content)
{
int sendok = 1;
for(int i=0;i
{
sendok = sendShortSms(recv, ((i+70<=content.length())?content.substring(i,i+70):content.substring(i)) );
}
return sendok;
}
private int sendShortSms(String recv,String content)
{
int sendok = -1;
try{
System.out.println("send sms to:" + recv + "\ncontent:" + content);
cOntent= getUnicodeString(content);
// cOntent= (j+70>uc.length()/2)?uc.substring(j):uc.substring(j,j+140);
// System.out.println(j + ":" + content);
cmd = "at+cmgs=" + Integer.toString(15+content.length()/2) + "\r";
out.write(cmd.getBytes());
out.flush();
System.out.println("Send:" + cmd);
for(int i=0;i<5;i++)
{
if(recvMsg.indexOf(">")>=0)
{
recvMsg = recvMsg.substring(recvMsg.indexOf(">")+1);
break;
}else
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
}
}
}
cmd = Integer.toHexString(content.length()/2 & 0xffff);
if(cmd.length()<2)cmd="0"+cmd;
cmd = "0011000D91" + getSMobile(recv) + "000800" + cmd + content;
cmd = cmd.toUpperCase();
out.write(cmd.getBytes());
out.write(26);
out.flush();
System.out.println("Send:" + cmd);
for(int i=0;i<5;i++)
{
if(recvMsg.indexOf("OK")>=0)
{
recvMsg = recvMsg.substring(recvMsg.indexOf("OK")+2);
sendok = 1;
break;
}else
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
}
}
}
}catch(Exception e)
{
sendok = -1;
e.printStackTrace();
}
return sendok;
}
public static void main(String args[])
{
String c = ("16日" );
ATSms at = new ATSms();
at.sendSms("13999999999",c);
at.close();
}
}
推荐阅读
-
本文介绍了在Hibernate配置lazy=false时无法加载数据的问题,通过采用OpenSessionInView模式和修改数据库服务器版本解决了该问题。详细描述了问题的出现和解决过程,包括运行环境和数据库的配置信息。 ...
[详细]
蜡笔小新 2023-12-14 13:59:45
-
本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ...
[详细]
蜡笔小新 2023-12-14 18:16:27
-
-
本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ...
[详细]
蜡笔小新 2023-12-14 17:06:58
-
本文介绍了如何去除Win7快捷方式的箭头的方法,通过生成一个透明的ico图标并将其命名为Empty.ico,将图标复制到windows目录下,并导入注册表,即可去除箭头。这样做可以改善默认快捷方式的外观,提升桌面整洁度。 ...
[详细]
蜡笔小新 2023-12-14 16:17:05
-
本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ...
[详细]
蜡笔小新 2023-12-14 15:56:00
-
本文介绍了使用windows操作系统时的一些隐藏功能,包括便签快捷键、截图功能等。同时探讨了windows和macOS操作系统之间的优劣比较,以及人们对于这两个系统的不同看法。 ...
[详细]
蜡笔小新 2023-12-14 15:47:35
-
本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ...
[详细]
蜡笔小新 2023-12-14 15:43:50
-
本文是一位90后程序员分享的职业发展经验,从年薪3w到30w的薪资增长过程。文章回顾了自己的青春时光,包括与朋友一起玩DOTA的回忆,并附上了一段纪念DOTA青春的视频链接。作者还提到了一些与程序员相关的名词和团队,如Pis、蛛丝马迹、B神、LGD、EHOME等。通过分享自己的经验,作者希望能够给其他程序员提供一些职业发展的思路和启示。 ...
[详细]
蜡笔小新 2023-12-14 15:22:09
-
本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ...
[详细]
蜡笔小新 2023-12-14 15:08:18
-
本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ...
[详细]
蜡笔小新 2023-12-14 14:44:00
-
本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ...
[详细]
蜡笔小新 2023-12-14 14:25:33
-
本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ...
[详细]
蜡笔小新 2023-12-14 14:15:30
-
本文介绍了Win10下游戏不能全屏的解决方法,包括修改注册表默认值和查看兼容游戏列表。同时提供了部分已经支持Win10的热门游戏列表,帮助玩家解决游戏不能全屏的问题。 ...
[详细]
蜡笔小新 2023-12-14 13:40:42
-
本文介绍了在联想win10专业版中修改账户名称的方法,包括在计算机管理中找到要修改的账户,通过重命名来修改登录名和属性来修改显示名称。同时指出了windows10家庭版无法使用此方法的限制。 ...
[详细]
蜡笔小新 2023-12-14 13:35:37
-
本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ...
[详细]
蜡笔小新 2023-12-14 13:11:00
-