RXTXcomm.jar实现Java串口通信,发送信息时没报错,但也没收到信息。请帮忙看下什么原因呢?
以下源码:
public class J2MEMain {
/**
* @param args
* 本类是发送短信的主类,负责调用其他类对象的函数
*/
public static void main(String[] args) {
SMSSend smsSend=new SMSSend();
smsSend.sendSMS();
}
}
//-----------------------------------------------------------------------------------------------------
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.*;
import java.util.Enumeration;
import java.util.TooManyListenersException;
/*
* 短信发送的主要功能类
*/
public class SMSSend {
SerialPort serialPort=null;
CommPortIdentifier portId = null;
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
final SMSReturnEventListener sMSReturnEventListener=new SMSReturnEventListener();
public SMSSend(){
while (portList.hasMoreElements()){
CommPortIdentifier com = (CommPortIdentifier)portList.nextElement();
if (com.getPortType() == CommPortIdentifier.PORT_SERIAL){
portId=com;//由于已知机器上只有一个串口,因此这样就可以
break;
}
}
try {
serialPort=(SerialPort)portId.open("SMS", 2000);
System.out.println(serialPort.getName());
} catch (PortInUseException e) {
System.out.println("PortInUseException");
e.printStackTrace();
}
try {
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, //设置串口读写参数
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
} catch (UnsupportedCommOperationException e) {
}
}
public void sendSMS(){
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(serialPort.getInputStream());
outputStream = new BufferedOutputStream(serialPort.getOutputStream());
sMSReturnEventListener.setInputStream(inputStream);
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(sMSReturnEventListener);
outputStream.write("AT+CMGF=1".getBytes());
outputStream.write("AT+CNMI=2,2,0,0,0".getBytes());
outputStream.write("AT+CMPS=17,23,64,244".getBytes());
outputStream.write("AT+CGMI".getBytes());
outputStream.write("AT+CMGS='13880458775',10 ".getBytes());
outputStream.write("0x600xa80x590x7d0x000x1a".getBytes());
// outputStream.write(("您好".getBytes("GB2312")+"0x000x1a").getBytes());
// 测试如何转换汉字为Unicode,希望成功
// 也可以用AT指令调试精灵来把任何的语句进行Unicode编码
} catch (IOException e) {
System.err.println("发生了某些异常");
e.printStackTrace();
}catch (TooManyListenersException e) {
e.printStackTrace();
}finally{
try {
inputStream.close();
outputStream.close();
serialPort.close();
} catch (IOException e) {
e.printStackTrace();
}//这一句很重要,必须关闭串口资源
}
}
}//class
//----------------------------------------------------------------------------------------------------
import java.io.IOException;
import java.io.InputStream;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
/*
* 短信发送AT指令可能有返回值,本事件监听器专门监听返回值
*/
public class SMSReturnEventListener implements SerialPortEventListener {
private InputStream inputStream;
@Override
public void serialEvent(SerialPortEvent event) {
System.out.println("得到了一些返回值");
/*now I don't want to insert some programme into this block ,because
* I can't be sure that the super block would work well
*/
InputStream inputStream=null;
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE://当有可用数据时读取数据,并且给串口返回数据
byte[] readBuffer = new byte[1024];
try {
while (inputStream.available() > 0) {//下一次可以不受阻塞而读取的字节数
int numBytes = inputStream.read(readBuffer);//从输入流中读取一定数量的字节,并将其存储在缓冲区数组readBuffer中。以整数形式返回实际读取的字节数。
}
System.out.println(new String(readBuffer));
} catch (IOException e){
}finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
}
public void setInputStream(InputStream inputStream){
this.inputStream=inputStream;
}
}
以下源码:
public class J2MEMain {
/**
* @param args
* 本类是发送短信的主类,负责调用其他类对象的函数
*/
public static void main(String[] args) {
SMSSend smsSend=new SMSSend();
smsSend.sendSMS();
}
}
//-----------------------------------------------------------------------------------------------------
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.*;
import java.util.Enumeration;
import java.util.TooManyListenersException;
/*
* 短信发送的主要功能类
*/
public class SMSSend {
SerialPort serialPort=null;
CommPortIdentifier portId = null;
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
final SMSReturnEventListener sMSReturnEventListener=new SMSReturnEventListener();
public SMSSend(){
while (portList.hasMoreElements()){
CommPortIdentifier com = (CommPortIdentifier)portList.nextElement();
if (com.getPortType() == CommPortIdentifier.PORT_SERIAL){
portId=com;//由于已知机器上只有一个串口,因此这样就可以
break;
}
}
try {
serialPort=(SerialPort)portId.open("SMS", 2000);
System.out.println(serialPort.getName());
} catch (PortInUseException e) {
System.out.println("PortInUseException");
e.printStackTrace();
}
try {
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, //设置串口读写参数
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
} catch (UnsupportedCommOperationException e) {
}
}
public void sendSMS(){
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(serialPort.getInputStream());
outputStream = new BufferedOutputStream(serialPort.getOutputStream());
sMSReturnEventListener.setInputStream(inputStream);
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(sMSReturnEventListener);
outputStream.write("AT+CMGF=1".getBytes());
outputStream.write("AT+CNMI=2,2,0,0,0".getBytes());
outputStream.write("AT+CMPS=17,23,64,244".getBytes());
outputStream.write("AT+CGMI".getBytes());
outputStream.write("AT+CMGS='13880458775',10 ".getBytes());
outputStream.write("0x600xa80x590x7d0x000x1a".getBytes());
// outputStream.write(("您好".getBytes("GB2312")+"0x000x1a").getBytes());
// 测试如何转换汉字为Unicode,希望成功
// 也可以用AT指令调试精灵来把任何的语句进行Unicode编码
} catch (IOException e) {
System.err.println("发生了某些异常");
e.printStackTrace();
}catch (TooManyListenersException e) {
e.printStackTrace();
}finally{
try {
inputStream.close();
outputStream.close();
serialPort.close();
} catch (IOException e) {
e.printStackTrace();
}//这一句很重要,必须关闭串口资源
}
}
}//class
//----------------------------------------------------------------------------------------------------
import java.io.IOException;
import java.io.InputStream;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
/*
* 短信发送AT指令可能有返回值,本事件监听器专门监听返回值
*/
public class SMSReturnEventListener implements SerialPortEventListener {
private InputStream inputStream;
@Override
public void serialEvent(SerialPortEvent event) {
System.out.println("得到了一些返回值");
/*now I don't want to insert some programme into this block ,because
* I can't be sure that the super block would work well
*/
InputStream inputStream=null;
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE://当有可用数据时读取数据,并且给串口返回数据
byte[] readBuffer = new byte[1024];
try {
while (inputStream.available() > 0) {//下一次可以不受阻塞而读取的字节数
int numBytes = inputStream.read(readBuffer);//从输入流中读取一定数量的字节,并将其存储在缓冲区数组readBuffer中。以整数形式返回实际读取的字节数。
}
System.out.println(new String(readBuffer));
} catch (IOException e){
}finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
}
public void setInputStream(InputStream inputStream){
this.inputStream=inputStream;
}
}