比如说我发送:abcdefg我的接收段: read.java会报出a, 在报出 bcdefg   请问有哪位高手知道是怎么回事

解决方案 »

  1.   

    难道 csdn 的高手,,,都还没上班?
      

  2.   

    把 我的接收段: read.java  里面的内容贴出来
      

  3.   

    read.java 主要代码如下:
    public void connectPort(){

    if(this.serialPort !=null){

    this.serialPort.close();

    }

    try
    {
    portId = CommPortIdentifier.getPortIdentifier(this.portName);
    serialPort = (SerialPort) portId.open("ReadComm", 2000); 
    this.p3f2.setText(this.portName+" Success\n");


    }catch(Exception ee){

    JOptionPane.showMessageDialog(this,ee.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
    this.p3f2.setText(this.portName+" failure\n");
    }
    try
    {
    serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); 

    }catch(Exception ee){

    JOptionPane.showMessageDialog(this,"Please connect serial port first!","Message",JOptionPane.INFORMATION_MESSAGE);
    }

    try{

    this.inputStream = serialPort.getInputStream();
    this.serialPort.addEventListener(this);

    }catch(Exception ee){


    }

    serialPort.notifyOnDataAvailable(true);

    }

    public void WritePortData(String str){

    try{

    outputStream = serialPort.getOutputStream(); 
    outputStream.write(str.getBytes());

    }catch(Exception ee){

    JOptionPane.showMessageDialog(this,ee.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
    }


    }
    public void serialEvent(SerialPortEvent event) {

    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[21];        
                  try{  
                         
                   while(inputStream.available()>0){
                   
                          int numBytes=inputStream.read(readBuffer);                      
                       
                   }
                   
                  // JOptionPane.showMessageDialog(this, new   String(readBuffer),"当前接收字符",JOptionPane.ERROR_MESSAGE);
                   
                   /*模拟在屏上显示,内容从单片机中接收*/
                   this.dp.tme1.stop();
                   this.dp.tme2.stop();
                   
                   String comStr = new String(readBuffer).trim();      //主要是这里它会分两次显示....比如说我接到到123456789 先显示1 在显示2345678...问题出在哪里
                   System.out.println(comStr.length());
                   
                   if(comStr.length()>15){
                   
                   this.dp.tme1.start();
                   
                   
                   }
                   
                   this.f.str=comStr;
                   this.f.strcopy = comStr;
                   
                   this.f.stri = "";
                   this.f.stricopyi = "";
                   
                   f.repaint();
                   
              
                   
                   
                             
                  }catch(Exception e){
                   
                   
                  }
                  
                  
                  
                  break;   

    }



    }
      

  4.   

    while(inputStream.available()>0){
      int numBytes=inputStream.read(readBuffer);                      
    }
    这样的while有意义吗?每次都覆盖前面的数据
    numBytes值有看过吗,每次是多少分两次显示会不会是多线程在读,没有看到循环的代码
      

  5.   

    bug 一下就OK了呀,在while这搞一个断点
      

  6.   

    我也遇到这个问题,用的moxa的串口卡,
    每次只能读96,郁闷中呀,
    你试试enableReceiveThreshold
    看能不能接收问题,
    我进行测试下,