第一次来论坛,
呵呵,
我在java中碰见了一个怪问题,是在操作串口时出现的,用的是rxtx-2.1-7-bins-r2.zip,主要代码代码如下:
SerialConnection.java
................................................
public void serialEvent(SerialPortEvent e) {
// Create a StringBuffer and int to receive input data.
StringBuffer inputBuffer = new StringBuffer(); //int newData = 0;//
byte []readbuffer = new byte[1]; // Determine type of event.
switch (e.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
try {
while (is.available() > 0) {
int numBytes = is.read(readbuffer);
databuffer =(int) readbuffer[0];
SimpleRead1();
SimpleRead2();
//System.out.print(databuffer); //测试
}
//System.out.print(new String(readbuffer));
//System.out.print(new String(Byte.toString(readBuffer)));
//System.out.print();
} catch (IOException e1) {}
break;
default:
databuffer =0;
//SimpleRead1();
break;
}
}
public Boolean SimpleRead1(){
if(databuffer == 85){//0x55
//System.out.print("true");//测试用
//databuffer = 0;
return true;
}
else{
//System.out.print("false"); //测试用
return false;
}
}
public Boolean SimpleRead2(){
if(databuffer == 86){//0x56
System.out.print("true");//测试用
//databuffer = 0;
return true;
}
else{
//System.out.print("false"); //测试用
return false;
}
}
..............................................上面这段程序,
我在frame里调用了一下,
初始化代码里这样写的
private SerialConnection connection = new SerialConnection();
打开串口之后,然后这样调用了一下
public void jButton3_actionPerformed(ActionEvent e) {
//向串口发出播放命令 int flag = 0;
connection.SimpleWrite(1);
connection.SimpleWrite(0);
for(index = 0;index < 600;flag = 0){
while(flag == 0){//播放命令返回失败,继续写
if(connection.SimpleRead1() == true){
connection.SimpleWrite(1);
connection.SimpleWrite(0);
System.out.print("connection.SimpleRead1()==true");
flag = 1;
}
}
index += 20;
System.out.print(index);//测试用
}
}
在这个函数里只调用SimpleRead1()一次,返回值正确,调用两次以上只返回false,弄了好多天了,没有找到原因,希望哪位大哥能指点一下。
呵呵,
我在java中碰见了一个怪问题,是在操作串口时出现的,用的是rxtx-2.1-7-bins-r2.zip,主要代码代码如下:
SerialConnection.java
................................................
public void serialEvent(SerialPortEvent e) {
// Create a StringBuffer and int to receive input data.
StringBuffer inputBuffer = new StringBuffer(); //int newData = 0;//
byte []readbuffer = new byte[1]; // Determine type of event.
switch (e.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
try {
while (is.available() > 0) {
int numBytes = is.read(readbuffer);
databuffer =(int) readbuffer[0];
SimpleRead1();
SimpleRead2();
//System.out.print(databuffer); //测试
}
//System.out.print(new String(readbuffer));
//System.out.print(new String(Byte.toString(readBuffer)));
//System.out.print();
} catch (IOException e1) {}
break;
default:
databuffer =0;
//SimpleRead1();
break;
}
}
public Boolean SimpleRead1(){
if(databuffer == 85){//0x55
//System.out.print("true");//测试用
//databuffer = 0;
return true;
}
else{
//System.out.print("false"); //测试用
return false;
}
}
public Boolean SimpleRead2(){
if(databuffer == 86){//0x56
System.out.print("true");//测试用
//databuffer = 0;
return true;
}
else{
//System.out.print("false"); //测试用
return false;
}
}
..............................................上面这段程序,
我在frame里调用了一下,
初始化代码里这样写的
private SerialConnection connection = new SerialConnection();
打开串口之后,然后这样调用了一下
public void jButton3_actionPerformed(ActionEvent e) {
//向串口发出播放命令 int flag = 0;
connection.SimpleWrite(1);
connection.SimpleWrite(0);
for(index = 0;index < 600;flag = 0){
while(flag == 0){//播放命令返回失败,继续写
if(connection.SimpleRead1() == true){
connection.SimpleWrite(1);
connection.SimpleWrite(0);
System.out.print("connection.SimpleRead1()==true");
flag = 1;
}
}
index += 20;
System.out.print(index);//测试用
}
}
在这个函数里只调用SimpleRead1()一次,返回值正确,调用两次以上只返回false,弄了好多天了,没有找到原因,希望哪位大哥能指点一下。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货