在使用bulktransfer()进行数据传输时获得-1

问题描述 投票:0回答:1

我正在开发一个需要Android手机和Camera之间进行数据通信的应用程序。通过USB连接的Android手机和摄像头。

我可以用bulkTransfer()进行第一次数据读写,但是第二次尝试时,我得到负数1。我也尝试了不同的超时时间。任何建议都将受到高度赞赏。

下面给出了我的代码

public int process()
{
    // prepare the command data
    ContainerCommand data = prepareCommand();

    int reqSize = data.size();
    data.prepareRequest(outBuffer);

    if (! skipLogging) {
        Log.i(TAG, "-== Ready to send Command [**- " + commandName + " -**] ==-");
        if (OperationCode.debugVerbose) {
            Log.v(TAG, data.toString());
            Log.v(TAG, StringUtility.dumpByteArray(outBuffer, reqSize));
        }
        Log.d(TAG, "reqLen: " + reqSize);
    }

    int sentLen;
    sentLen = usbConnection.bulkTransfer(epHost2Camera, outBuffer, reqSize, usbTimeout);
    if (sentLen != reqSize) {
        if (! skipLogging) {
            Log.w(TAG, "******--- UNEXPECTED send len:" + sentLen);
        }
        return UsbCameraConstants.RET_GENERAL_ERROR;
    }

    int resultCode;
    resultCode = sendExtraData4Command();
    if (resultCode != UsbCameraConstants.RET_SUCCESSFULLY_DONE) {
        if (! skipLogging) {
            Log.w(TAG, "******--- UNEXPECTED extra data send for command! retCode:" 
                    +  short2Hex(resultCode) );
        }
        return resultCode;          
    }

    resultCode = UsbCameraConstants.RET_GENERAL_ERROR;
    // get response
    int readLen;
    boolean responseGet = false;
    boolean errorFound = false;

    readLen = usbConnection.bulkTransfer(epCamera2Host, inBuffer
            , maxInPacketSize, usbTimeout);
    if (! skipLogging) {
        Log.d(TAG, "******--- Got first response, read len:" + readLen);
    }
    if (readLen <= 0) {
        if (! skipLogging) {
            Log.w(TAG, "******--- UNEXPECTED read len:" + readLen);
        }
        errorFound = true;  
    }
    while ((readLen > 0) && (! responseGet) && (! errorFound)) {
        ContainerBase container = null;
        try {
            container = ContainerBase.parseBaseValue(inBuffer, 0, readLen);
        } catch (Exception e) {
            Log.e(TAG, "Failed to parse container packet!", e);
            errorFound = true;
            resultCode = UsbCameraConstants.RET_GENERAL_ERROR;
        }
        if (container != null) {
            if (! skipLogging) {
                Log.d(TAG, "Response packet basic information:" + container.toString());
                if (OperationCode.debugVerbose) { 
                    Log.v(TAG, StringUtility.dumpByteArray(inBuffer, readLen));
                }
            }
            if (container.type == ContainerBase.TYPE_DATA_BLOCK) {
                // need read again
                if (! skipLogging) {
                    Log.d(TAG, "Data container block found!");
                }
                if (readLen > leastPacketSize) {
                    // need process the data content
                    if (! processDataBlock(container.length, readLen) ) {
                        Log.w(TAG, "Failed to Process Data Packet!");
                        errorFound = true;
                    }
                }
            }
            else if (container.type == ContainerBase.TYPE_RESPONSE_BLOCK) {
                resultCode = container.code;
                if (! skipLogging) {
                    Log.d(TAG, "Response container block found! retCode:" + short2Hex(resultCode));
                }
                responseGet = true;
                if (! processResponseBlock() ) {
                    Log.w(TAG, "Failed to Process Response Packet!");
                }
            }
            if (! responseGet) {
                do {
                    readLen = usbConnection.bulkTransfer(epCamera2Host, inBuffer
                            , maxInPacketSize, usbTimeout);
                    if (! skipLogging) {
                        Log.d(TAG, "******--- Got packet from endpoint Camera2Host, read len:" 
                                    + readLen);
                    }
                    if (readLen == 0) {
                        Log.w(TAG, "***ReadLen=0, camera data not ready yet. sleep a little while!");
                        try {
                            Thread.sleep(50L); // 50 milliseconds
                        }
                        catch (Exception e) {
                            // do nothing
                            e.printStackTrace();
                        }
                    }                       
                } while (readLen == 0);

            }
        }


    }

    return resultCode;
} 
android android-camera
1个回答
0
投票

很遗憾,无法通过提供的信息解决您的问题。

我将其写为答案而不是评论,因为我希望有机会向您介绍一些可能导致您出现问题的可能性。

-1bulkTransfer返回意味着存在一些读取或写入错误。

可能是超时问题,但也可能是连接问题或协议问题。

您需要检查以下可能性:

  1. 您的USB电缆是否状况良好,并且连接器已正确插入?物理连接可能不稳定,并且您丢失了相机。

  2. 什么是相机协议?在发送命令之间可能需要做一些事情,例如重置接口,通过控制传递在端点0上发送一些命令等。

  3. 您发送的命令正确吗?

  4. 您还记得声明过该接口吗?

  5. 是在与发送命令的代码相同的线程中打开USB连接的代码吗?您尝试发送命令时,应用程序的其他部分可能正在关闭连接。

  6. 您是否为相机USB设置了正确的配置?某些USB设备具有多个配置。

至少,我建议您将使用的摄像机的确切型号添加到您的问题中,以便如果有人熟悉它的协议可以为您提供建议。

此外,除非您有相机的协议文档,否则我建议您将其连接到PC,并使用WireSHark USB模块或其他USB捕获软件来监视协议并学习如何实现。

© www.soinside.com 2019 - 2024. All rights reserved.