成功接收批量传输数据,但其长度不够
successfully received bulk transfer data but its length was not enough
我正在研究 bulk transfer Android。我发送和接收数据 to/from USB 成功。我发送的数据是 cbwBuffer
这是一个命令,它告诉 USB 我想读取 USB 从哪个扇区开始读取多少个扇区。下面是我的代码。
byte cswBuffer[] = new byte[18432];
iRes = conn.bulkTransfer(epOUT, cbwBuffer, 31, 500);//send data
iRes2 = conn.bulkTransfer(epIN, cswBuffer, 18432, 500);//receive data
收到的数据应该正好是18432,但是iRes2等于16384,也就是说我收到的数据只有16384字节,不是18432字节。我检查了 cswBuffer(接收到的数据),发现我的数据仅在 0 索引到 16384 索引之间是正确的,但其余数据在哪里?我的意思是数据还不够。我还需要 18432-16384=2048 字节。
18432 表示我读取了 USB 的 36 个扇区,因为 1 个扇区 = 512 字节。我刚刚意识到,如果我读取超过 32 个扇区,我将始终获得 16384 个字节。
我发现的结论是最大可以接收的数据长度是16384字节。如果想要更多,只需再输入一个 iRes = conn.bulkTransfer(epIN, cswBuffer, remaining, 500)
即可接收剩余的数据。
所以我需要对每个数据最多 16384 字节的数据进行分块。
if (noOfSector > 32) {
//CHUNK DATA
int maxLength = 16384;
int lengthReceivedData = 0;
int remaining;
int chunkSize = (noOfSector + 32 - 1) / 32;int first= chuckSize;
byte[] receivedData = new byte[maxLength];
while (chunkSize > 1) {
cswBuffer = new byte[maxLength];
iRes = conn.bulkTransfer(epIN, cswBuffer, maxLength, 500);
if (chunkSize == first) {//if first time do this
receivedData = cswBuffer;
} else {
receivedData = concatenateByteArrays(receivedData, cswBuffer);
}
chunkSize--;
lengthReceivedData += maxLength;
}
remaining = sectorLength - lengthReceivedData;
if (chunkSize == 1) {//last one sector
cswBuffer = new byte[remaining];
iRes = conn.bulkTransfer(epIN, cswBuffer, remaining, 500);
receivedData = concatenateByteArrays(receivedData, cswBuffer);
finalBuffer = receivedData;
}
//END CHUNK DATA
我正在研究 bulk transfer Android。我发送和接收数据 to/from USB 成功。我发送的数据是 cbwBuffer
这是一个命令,它告诉 USB 我想读取 USB 从哪个扇区开始读取多少个扇区。下面是我的代码。
byte cswBuffer[] = new byte[18432];
iRes = conn.bulkTransfer(epOUT, cbwBuffer, 31, 500);//send data
iRes2 = conn.bulkTransfer(epIN, cswBuffer, 18432, 500);//receive data
收到的数据应该正好是18432,但是iRes2等于16384,也就是说我收到的数据只有16384字节,不是18432字节。我检查了 cswBuffer(接收到的数据),发现我的数据仅在 0 索引到 16384 索引之间是正确的,但其余数据在哪里?我的意思是数据还不够。我还需要 18432-16384=2048 字节。
18432 表示我读取了 USB 的 36 个扇区,因为 1 个扇区 = 512 字节。我刚刚意识到,如果我读取超过 32 个扇区,我将始终获得 16384 个字节。
我发现的结论是最大可以接收的数据长度是16384字节。如果想要更多,只需再输入一个 iRes = conn.bulkTransfer(epIN, cswBuffer, remaining, 500)
即可接收剩余的数据。
所以我需要对每个数据最多 16384 字节的数据进行分块。
if (noOfSector > 32) {
//CHUNK DATA
int maxLength = 16384;
int lengthReceivedData = 0;
int remaining;
int chunkSize = (noOfSector + 32 - 1) / 32;int first= chuckSize;
byte[] receivedData = new byte[maxLength];
while (chunkSize > 1) {
cswBuffer = new byte[maxLength];
iRes = conn.bulkTransfer(epIN, cswBuffer, maxLength, 500);
if (chunkSize == first) {//if first time do this
receivedData = cswBuffer;
} else {
receivedData = concatenateByteArrays(receivedData, cswBuffer);
}
chunkSize--;
lengthReceivedData += maxLength;
}
remaining = sectorLength - lengthReceivedData;
if (chunkSize == 1) {//last one sector
cswBuffer = new byte[remaining];
iRes = conn.bulkTransfer(epIN, cswBuffer, remaining, 500);
receivedData = concatenateByteArrays(receivedData, cswBuffer);
finalBuffer = receivedData;
}
//END CHUNK DATA