【问题标题】:FTDI D2xx android java not readingFTDI D2xx android java不读取
【发布时间】:2014-04-10 10:35:38
【问题描述】:

我目前正在将我从 C# 中获得的一些代码移植到 Java 以在 Android 系统上运行。

在我的代码中,我必须从具有 FTDI 芯片的板上读取一些数据。我正在尝试使用 FTDI 网站上的 java 驱动程序。

我可以连接到设备并向其正确发送命令(LED 应闪烁)。董事会似乎正在正确地向我发送数据。

bytesAvailable = ftDevice.getQueueStatus(); 返回预期数字

int bytesRead = ftDevice.read(rxData, bytesAvailable); 返回相同的数字

但是,在调用read() 时,我在我的 logcat 中看到以下内容: 无法从源读取数据!! 来自标签: 读取BulkInData::

我看不出这可能是什么。我尝试在我的代码中摆弄设备的设置,但无济于事。

【问题讨论】:

    标签: java android ftdi


    【解决方案1】:

    我解决了这个问题。

    通过将整个读取指令序列(getQueueStatus()read())放在另一个Thread 中。具体来说,我使用了AsyncTask,并将阅读说明放在了它的doInBackground()方法中。

    【讨论】:

    • 太棒了。我被锁定在这一点上,我最终在这一点上使用了不完整的第三个库。请注意,在我的情况下,它在 ARM 4.2+ 上完美运行,我在切换到 x86 4.2+ 时遇到了问题(测试 4.2.1、4.4.2)
    【解决方案2】:

    我已经修改了 FTDI 示例的部分写入和 等待,直到收到答案。这适用于 Parker Compax3 伺服驱动器的简单 HMI 应用程序。该序列每 100 毫秒触发一次。

            // part of FTDI example
            synchronized (ftDev) {
            if(ftDev.isOpen() == false) {
                return;
            }
            ftDev.setLatencyTimer((byte)16);
            String writeString = tvWrite.getText().toString()+"\r";
            byte[] writeByte = writeString.getBytes();
            // write
            ftDev.write(writeByte, writeString.length());
    
            // new - wait until the buffer have data - no fixed length of incoming data - 4 - 8 bytes     
            readRequest = true;
            startTimeRead = SystemClock.uptimeMillis();
            while (true){
                timeOutInMilliseconds = SystemClock.uptimeMillis() - startTimeRead;
    
                if (timeOutInMilliseconds > timeOutTime) // checking after 70ms
                {
                    readSize = ftDev.getQueueStatus();
                    if(readSize>0) {
                            mReadSize = readSize;
                    if(mReadSize > READBUF_SIZE) {
                        mReadSize = READBUF_SIZE;
                    }
    
                    readCompleted = false;
    
                    // call asynctask
                    ReadAsyncTask task = new ReadAsyncTask();
                    task.execute();
    
                    // wait until asynctask has completed
                    while (readCompleted = false){ // endless loop until asynctask have read
                        if (readCompleted = true){ // <- i know this is not necessary :-)
                            break;  
                        }
                    }
    
                    // if read completed, write values to string/textview
                    if (readCompleted = true){
                        textView13.setText("Ok" + " " + mReadSize );
                        tvRead.setText(readString); //now it updates the textboxes, strings
                    }
                }
    
                //do anything if there are no data after 70ms
                else{
                    readString="**";
                    textView13.setText("Timeout, no data");  
                    }
                // go out, wait 30ms and do it again
                break;  
                }
            }
        }
    

    这里是异步任务

    class ReadAsyncTask extends AsyncTask<Void, Void, Void>{  
          @Override  
          protected Void doInBackground(Void... params) {  
    
              int j = 0;
              ftDev.read(rbuf,mReadSize);
              for(j=0; j<mReadSize; j++) {
                  rchar[j] = (char)rbuf[j];
              }
    
              // clear buffer
              ftDev.purge((byte) 1);
    
              // copy to string
              readString =String.copyValueOf(rchar,0,mReadSize);
              readCompleted = true;
              return null;
          }  
          @Override  
          protected void onPreExecute() {  
               super.onPreExecute();  
          }  
          @Override  
          protected void onPostExecute(Void result) {  
               super.onPostExecute(result); 
          }  
     } 
    

    更新调用后/棘手,但它适用于我

    public void requestCompax() {
        sendseq += 1;
        if (sendseq > 6){
            sendseq = 1;
        }
    
        switch (sendseq){
        case 1: //request planejado
            tvWrite.setText("O1903.1");         // *send "planejado"
            senddata();                         // call write and read serial
            tvIntrodutor.setText(readString);   // *normally received the answer must be here
            break;
    
        case 2: //request produzido
            tvWrite.setText("O1903.2");         // send produzido
            senddata();                         // call write and read serial
            tvPlanejado.setText(readString);    // *but received answer "planejado" - it comes here, next call later ?!?!
            break;
    
       case 3:  //request value caixas
            tvWrite.setText("O1903.3");         // * send caixas
            senddata();
            tvProduzido.setText(readString);    // same with produzido
            break;
    
        case 4: //request pulas
            tvWrite.setText("O1903.4");         
            senddata();
            tvCaixas.setText(readString);       // same with "caixas"
            break;
    
        case 5: //request caixas/hora
            tvWrite.setText("O1903.5");         
            senddata();
            tvPulas.setText(readString);         // same with pulas
            break;
    
        case 6: //request adiantar/atrasar
            tvWrite.setText("O1902.2");         //adiantar/atrasar
            senddata();
            tvCaixasHora.setText(readString);   //same with caixas/hora   
            break;
    
        default:
    
            break;
        }
    }
    

    它很有趣,在下一个 task.execute() 调用时更新字符串和文本框。 这对我有用,可能有点棘手,但我只需要读写 5 个参数。

    这里有人知道如何解决这个问题吗?这是通过模拟器在线测试的(延迟 10 毫秒),结果相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多