第一次来论坛,
呵呵,
我在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,弄了好多天了,没有找到原因,希望哪位大哥能指点一下。

解决方案 »

  1.   

    建议你debug一下,跟踪处理,就明白怎么会是false了
      

  2.   

    不好意思,有人能给我说一下debug的具体过程吗?我debug好多变量都跟踪不了啊。
      

  3.   

    用eclipse直接在程序前面有个小框架一样的东西点一下就行了会出现小圆圈的
      

  4.   

    你不要用RUN,用DEBUG加你的项目运行;到出错的时候,右上角有个(X)Variables,这个窗口可以查看所有变量.你按个点开,用一次,就能知道是不是你想要的了.