当前位置: 技术问答>java相关
帮忙看一下程序,关于串口操作的(急!!!在线等待)
来源: 互联网 发布时间:2015-07-06
本文导语: 此程序中,serialEvent 只被执行一次,怎么回事(我想让它执行两次,第一次是向串口发送一个数据,该数据是让连接在串口的数据猫发送一条信息给自己,然后从串口读数据猫的确认值“OK”;第二次是向数据猫发一...
此程序中,serialEvent 只被执行一次,怎么回事(我想让它执行两次,第一次是向串口发送一个数据,该数据是让连接在串口的数据猫发送一条信息给自己,然后从串口读数据猫的确认值“OK”;第二次是向数据猫发一条命令,从穿口接收数据猫刚刚收到的消息)
发送数据成功,但不能收数据。
import java.io.*;
import javax.comm.*;
import java.util.*;
import java.awt.event.*;
public class receiveCmd extends Thread implements SerialPortEventListener {
SerialPort sPort=null;
static BufferedReader in=null;
public receiveCmd(SerialPort sPort) {
this.sPort=sPort;
this.setPriority(1);
}
public void send(SerialPort sPort,String cmd)
{
PrintWriter pw=null;
try{
pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd);
pw.flush();
pw.close();
System.out.println("command has been send");
} catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
}
public void receive(SerialPort sPort)
{
BufferedReader br=null;
String msg=null;
SerialPortEvent sEvent=null;
byte[] b=null;
//SerialPortEventListener sListener=null;
try{
in=new BufferedReader(new InputStreamReader(sPort.getInputStream()));
sPort.addEventListener(this );
sPort.notifyOnDataAvailable(true);
System.out.println("msg is receiveing...");
}
catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
catch (TooManyListenersException e) {e.getMessage();}
}
public void run()
{
String cmd1="AT+CMGS="+8613939060564"r sinboy";
cmd1+=(char)(Integer.parseInt("1a",16))+"z";//用ctrl-z表示结束并发送
String cmd2="AT+CMGL="REC UNREAD"";
String cmd3="AT+CMGL="ALL"";
for(int i=1;i
发送数据成功,但不能收数据。
import java.io.*;
import javax.comm.*;
import java.util.*;
import java.awt.event.*;
public class receiveCmd extends Thread implements SerialPortEventListener {
SerialPort sPort=null;
static BufferedReader in=null;
public receiveCmd(SerialPort sPort) {
this.sPort=sPort;
this.setPriority(1);
}
public void send(SerialPort sPort,String cmd)
{
PrintWriter pw=null;
try{
pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd);
pw.flush();
pw.close();
System.out.println("command has been send");
} catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
}
public void receive(SerialPort sPort)
{
BufferedReader br=null;
String msg=null;
SerialPortEvent sEvent=null;
byte[] b=null;
//SerialPortEventListener sListener=null;
try{
in=new BufferedReader(new InputStreamReader(sPort.getInputStream()));
sPort.addEventListener(this );
sPort.notifyOnDataAvailable(true);
System.out.println("msg is receiveing...");
}
catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
catch (TooManyListenersException e) {e.getMessage();}
}
public void run()
{
String cmd1="AT+CMGS="+8613939060564"r sinboy";
cmd1+=(char)(Integer.parseInt("1a",16))+"z";//用ctrl-z表示结束并发送
String cmd2="AT+CMGL="REC UNREAD"";
String cmd3="AT+CMGL="ALL"";
for(int i=1;i