📄 nokiacommopen.java
字号:
package com.sunfruit.comm.test;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import java.util.Enumeration;
import javax.comm.SerialPortEventListener;
import javax.comm.SerialPortEvent;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.*;
import javax.comm.CommPort;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class NokiaCommOpen {
private static final String COMPORT="COM10";
private static final String SMSCENTERNUMBER ="8613800100500";
private static final String SENDTO ="8613910225677";
// private static final String SENDTO ="13651076584";
private static final String ENTER=System.getProperty("line.separator");
public NokiaCommOpen() {
this.openComm();
}
public static void main(String[] ages)
{
new NokiaCommOpen();
}
private void openComm()
{
Enumeration en = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId=null;
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
/*如果端口类型是串口,则打印出其端口信息*/
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if(portId.getName().equals(COMPORT))
{
break;
}
System.out.println(portId.getName()+"1");
}
}
System.out.println(portId.getName()+"2"+" "+portId.getCurrentOwner());
byte[] bytes=new byte[128];
CommPort commPort=null;
// CommPort commport=null;
// commport.
OutputStream out=null;
InputStream input=null;
try {
commPort=portId.open("nokia", 120);
// commPort.enableReceiveFraming(0);
// commPort.enableReceiveThreshold(1);
// commPort.enableReceiveTimeout(2);
out=commPort.getOutputStream();
input=commPort.getInputStream();
// out.write(("AT+CGMI"+ENTER).getBytes());
// out.flush();
// bytes=new byte[input.available()];
// input.read(bytes);
// System.out.println(new String(bytes,"UTF-8"));
//
//
// out.write(("AT+CGMM"+ENTER).getBytes());
// out.flush();
// input.read(bytes);
// System.out.println(new String(bytes,"UTF-8"));
//
out.write(("AT+CSCA=\"+8613800100500\",145\r").getBytes());
out.flush();
bytes=new byte[input.available()];
input.read(bytes);
System.out.println(new String(bytes,"UTF-8"));
out.write(("AT+CNMI=0,2,0,1,0" + ENTER).getBytes());
out.flush();
bytes = new byte[input.available()];
input.read(bytes);
System.out.println(new String(bytes, "UTF-8"));
out.write(("AT+CMGF=0"+ENTER).getBytes());
out.flush();
bytes=new byte[input.available()];
int i=input.read(bytes);
String retstr=(new String(bytes,0,i,"UTF-8")).toUpperCase();
// retstr=retstr.substring(13).trim();
if(retstr.indexOf("OK")>=0)
{
String sendtext="";
System.out.println("连接成功");
NokiaCoding nokiaCoding=new NokiaCoding();
String from=nokiaCoding.getSendNumber(SMSCENTERNUMBER);
String sendto=nokiaCoding.getSendNumber(SENDTO);
String PDU=nokiaCoding.getPDUCoding(from,sendto,nokiaCoding.getASCII("这是123"));
out.write(("AT+CMGS="+nokiaCoding.getPDULength()+"\r").getBytes());
//out.write(("AT+CMGS=21\r").getBytes());
out.flush();
bytes=new byte[input.available()];
input.read(bytes);
System.out.println(new String(bytes,"UTF-8"));
// sendtext="AT+CMGS="+((PDU.length()-18)/2)+"0X0D"+PDU+"0X^Z"+ENTER;
out.write((PDU+(char)0X1A).getBytes());
//out.write(("\1A\r").getBytes());
//out.write("0891683108100005F011000D91683118405057F000000006C8329BFD0E01^Z".getBytes());
out.flush();
bytes=new byte[input.available()];
input.read(bytes);
System.out.println(new String(bytes,"UTF-8"));
}
else
{
System.out.println(retstr);
}
//commPort.addEventListener();
} catch (Exception ex) {
ex.printStackTrace();
}
finally
{
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
try {
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
commPort.close();
}
}
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
class MySerialPortEventListener implements SerialPortEventListener
{
public void serialEvent(SerialPortEvent ev)
{
//ev.
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -