求java通过串口发短信的例子。谢谢。 这个你需要一个 sp端口,端口号要向移动或联通申请java 串口发短信只能发送一个http 请求到你的网关上,而后通过网关发送到sp端口。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个在你的手机的Support中应该可以找到对应的信息,你可以到相应的手机网站看看Nokia http://www.nokia.com/ 你们都答什么啊?题目的重点内容是,JAVA如何发信息到串口(就是说,如何与串口通信),CSDN J2SE版的人越来越糊涂了. http://www-900.ibm.com/developerWorks/cn/java/joy-comm/index.shtml可以去这里看看 我做的用rxtx实现的,sun的包不好使import java.util.Map;import java.util.Enumeration;import gnu.io.*;import java.io.*;public class RXTXSerialConnect implements ISerialConnectable { public static final String OWNER_NAME = "Serial_Communication"; static String portName; CommPortIdentifier portId; SerialPort serialPort; static OutputStream out; static InputStream in; public SerialBuffer serialBuffer; ReadSerial readSerial; static int port; static Map properties; public RXTXSerialConnect() { } public static void main(String[] args) { RXTXSerialConnect connect = new RXTXSerialConnect(); } /** * 打开制定Com端口,端口号在properties中指定 * @param port int * @param properties Map * @throws Exception */ public void open(int port, Map properties) throws Exception { this.port = port; this.properties = properties; int InitSuccess = 1; int InitFail = -1; portName = "COM" + port; System.out.println("serialPort name:" + portName); try { portId = CommPortIdentifier.getPortIdentifier(portName); portId.addPortOwnershipListener( new CommPortOwnershipListener() { public void ownershipChange(int ownType) { if(ownType == CommPortOwnershipListener.PORT_UNOWNED) reopen(); } } ); System.out.println("Port2:" + portId); serialPort = (SerialPort) portId.open(OWNER_NAME, 1000); System.out.println("serialPort open:" + serialPort); serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT); serialPort.enableReceiveTimeout(750); if (!serialPort.isReceiveTimeoutEnabled()) System.out.println("Receive Timeout Not Enabled!"); serialPort.addEventListener(new SerialPortEventListener() { public void serialEvent(SerialPortEvent spe) { System.out.println("overrun error occurred!"); } }); serialPort.notifyOnOverrunError(true); } catch (Exception e) { System.out.println("----------" + e); e.printStackTrace(); throw e; }//Use InputStream in to read from the serial port, and OutputStream//out to write to the serial port. try { System.out.println("serialPort3:" + serialPort); in = serialPort.getInputStream(); out = serialPort.getOutputStream(); System.out.println("in:" + in + " out=" + out); } catch (IOException e) { e.printStackTrace(); throw e; }// when successfully open the serial port, create a new serial buffer,// then create a thread that consistently accepts incoming signals from // the serial port. Incoming signals are stored in the serial buffer. serialBuffer = new SerialBuffer(); readSerial = new ReadSerial(serialBuffer, in); readSerial.start(); } /** * 读取指定端口的数据 * @return String */ public String read() { String content = null; while ((content = serialBuffer.getContent()) == null || content.length() <= 1) { try { Thread.sleep(200); } catch (InterruptedException ex) { ex.printStackTrace(); } } return serialBuffer.get(content.length() - 1); } /** * 向指定端口写数据 * @param content byte[] * @throws Exception */ public void write(byte[] content) throws Exception { out.write(content); } public void reopen() { try{ close(); }catch(Exception ex){ ex.printStackTrace(); } try { System.out.println( "listener need to restart , the port is closed for some reason."); Thread.sleep(2000); open(RXTXSerialConnect.port, RXTXSerialConnect.properties); System.out.println("port is reopened!"); } catch (InterruptedException ex) { ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } public void write(String content) throws Exception { try { for (int i = 0; i < content.length(); i++) out.write(content.charAt(i)); } catch (IOException e) { e.printStackTrace(); } } /** * 关闭端口 */ public void close() { readSerial.stop(); serialPort.close(); }} 大哥gnu.io类在那里可以找到? oldblue(jimmey)能发一个完整的代码给我吗?别外再加500分。我在用http://www-900.ibm.com/developerWorks/cn/java/joy-comm/index.shtml这个例子发送AT指定时,返回值要自己指定一个返回长度,要如何取得返回数据流的长度? 其实说白了,就是用java怎么去操作串口gnu.io类,到 http://www.rxtx.org/ 去下 上面的代码出现一个“不能解析ISerialConnectable,或者不是超级接口”的错误。为什么? 哦,这个接口是我写的一个简单的接口import java.util.Map;import java.io.InputStream;import java.io.OutputStream;/** * <p>Title: WAP Push Project</p> * <p>Description: WAP Push by SMS</p> * <p>Copyright: Copyright (c) 2004</p> * @version $Id$ */public interface ISerialConnectable { public static final String PROPERTY_LISTEN_PORT = "listenPort"; public static final String PROPERTY_COM_PORT = "port"; public static final String PROPERTY_BAUDRATE = "baudrate"; public static final String PROPERTY_DATA_BIT = "databit"; public static final String PROPERTY_STOP_BIT = "stopbit"; public static final String PROPERTY_PARITY = "parity"; public static final String PROPERTY_SMSC = "smsc"; public static final String PROPERTY_SENDER = "senderPhone"; /** * 连接属性 */ public static final String [] PROPERTY_NAMES = {PROPERTY_COM_PORT,PROPERTY_BAUDRATE,PROPERTY_DATA_BIT, PROPERTY_STOP_BIT,PROPERTY_PARITY,PROPERTY_SMSC,PROPERTY_SENDER}; /** * 打开制定Com端口,端口号在properties中指定 * @param properties Map * @throws Exception */ public void open(int port,Map properties) throws Exception; /** * 读取指定端口的数据 * @return String */ public String read(); /** * 向指定端口写数据 * @param content byte[] * @throws Exception */ public void write(byte[] content) throws Exception; public void write(String content) throws Exception; /** * 关闭端口 */ public void close(); public void reopen();} oldblue(jimmey)大哥,能将serialBuffer类一起帖出来吗?谢谢。谢谢。 toolscool,能不能共享您的资源,我现在也在开发这样的程序,但是不懂怎么用java设置短信中心号码、对方手机号码和短信内容。能否见教。[email protected] 对象序列号问题? java 小程序可不可以打包成jar文件?? 小弟我在写一个五子棋,正在写下棋的部分 如果把当前的 JPanel 当作图片输出?? 请教 JDK1.4.2 的一个“规则表达式”的问题:过滤以"L_"开头的文件名 java中怎麼實現動態數組? 小弟初学,请指教!! ----------我已经改过几次环境变量的设置,但在编译时任说找不到applet,awt包.请高高手指点--------- 这样的问题--关于Applet显示的很简单的问题 关于筛选查找 求助~~! 有没有办法把整型以二进制形式输出
可以去这里看看
import java.util.Enumeration;
import gnu.io.*;
import java.io.*;
public class RXTXSerialConnect
implements ISerialConnectable {
public static final String OWNER_NAME = "Serial_Communication";
static String portName;
CommPortIdentifier portId;
SerialPort serialPort;
static OutputStream out;
static InputStream in;
public SerialBuffer serialBuffer;
ReadSerial readSerial;
static int port;
static Map properties; public RXTXSerialConnect() {
} public static void main(String[] args) {
RXTXSerialConnect connect = new RXTXSerialConnect();
} /**
* 打开制定Com端口,端口号在properties中指定
* @param port int
* @param properties Map
* @throws Exception
*/
public void open(int port, Map properties) throws Exception {
this.port = port;
this.properties = properties;
int InitSuccess = 1;
int InitFail = -1;
portName = "COM" + port;
System.out.println("serialPort name:" + portName); try {
portId = CommPortIdentifier.getPortIdentifier(portName);
portId.addPortOwnershipListener(
new CommPortOwnershipListener() {
public void ownershipChange(int ownType) {
if(ownType == CommPortOwnershipListener.PORT_UNOWNED)
reopen();
}
}
);
System.out.println("Port2:" + portId); serialPort = (SerialPort)
portId.open(OWNER_NAME, 1000);
System.out.println("serialPort open:" + serialPort); serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE); serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN
| SerialPort.FLOWCONTROL_RTSCTS_OUT);
serialPort.enableReceiveTimeout(750);
if (!serialPort.isReceiveTimeoutEnabled())
System.out.println("Receive Timeout Not Enabled!");
serialPort.addEventListener(new SerialPortEventListener() {
public void serialEvent(SerialPortEvent spe) {
System.out.println("overrun error occurred!");
}
});
serialPort.notifyOnOverrunError(true); }
catch (Exception e) {
System.out.println("----------" + e);
e.printStackTrace();
throw e;
}
//Use InputStream in to read from the serial port, and OutputStream
//out to write to the serial port.
try {
System.out.println("serialPort3:" + serialPort);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
System.out.println("in:" + in + " out=" + out);
}
catch (IOException e) {
e.printStackTrace();
throw e;
}
// when successfully open the serial port, create a new serial buffer,
// then create a thread that consistently accepts incoming signals from
// the serial port. Incoming signals are stored in the serial buffer.
serialBuffer = new SerialBuffer();
readSerial = new ReadSerial(serialBuffer, in);
readSerial.start(); } /**
* 读取指定端口的数据
* @return String
*/
public String read() {
String content = null;
while ((content = serialBuffer.getContent()) == null ||
content.length() <= 1) {
try {
Thread.sleep(200);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
} return serialBuffer.get(content.length() - 1); } /**
* 向指定端口写数据
* @param content byte[]
* @throws Exception
*/
public void write(byte[] content) throws Exception {
out.write(content); } public void reopen() {
try{
close();
}catch(Exception ex){
ex.printStackTrace();
}
try { System.out.println(
"listener need to restart , the port is closed for some reason.");
Thread.sleep(2000);
open(RXTXSerialConnect.port, RXTXSerialConnect.properties);
System.out.println("port is reopened!");
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
} } public void write(String content) throws Exception {
try {
for (int i = 0; i < content.length(); i++)
out.write(content.charAt(i));
}
catch (IOException e) {
e.printStackTrace();
}
} /**
* 关闭端口
*/
public void close() {
readSerial.stop();
serialPort.close();
}}
gnu.io类,到 http://www.rxtx.org/ 去下
import java.io.InputStream;
import java.io.OutputStream;
/**
* <p>Title: WAP Push Project</p>
* <p>Description: WAP Push by SMS</p>
* <p>Copyright: Copyright (c) 2004</p>
* @version $Id$
*/public interface ISerialConnectable {
public static final String PROPERTY_LISTEN_PORT = "listenPort";
public static final String PROPERTY_COM_PORT = "port";
public static final String PROPERTY_BAUDRATE = "baudrate";
public static final String PROPERTY_DATA_BIT = "databit";
public static final String PROPERTY_STOP_BIT = "stopbit";
public static final String PROPERTY_PARITY = "parity";
public static final String PROPERTY_SMSC = "smsc";
public static final String PROPERTY_SENDER = "senderPhone"; /**
* 连接属性
*/
public static final String [] PROPERTY_NAMES
= {PROPERTY_COM_PORT,PROPERTY_BAUDRATE,PROPERTY_DATA_BIT,
PROPERTY_STOP_BIT,PROPERTY_PARITY,PROPERTY_SMSC,PROPERTY_SENDER};
/**
* 打开制定Com端口,端口号在properties中指定
* @param properties Map
* @throws Exception
*/
public void open(int port,Map properties) throws Exception;
/**
* 读取指定端口的数据
* @return String
*/
public String read();
/**
* 向指定端口写数据
* @param content byte[]
* @throws Exception
*/
public void write(byte[] content) throws Exception;
public void write(String content) throws Exception;
/**
* 关闭端口
*/
public void close(); public void reopen();
}