Transferring larger amounts of data USB

泄露秘密 提交于 2020-01-01 15:29:10

问题


Hey I have been trying to work on this for some time but I am just getting no where

I am trying to get bytes in from a USB device, this works fine for one or two lines coming in but when it gets to quite a few it just doesn't work.

I am using this line of code to bring in data:

conn.bulkTransfer(epIN, buffer, 57600, 500);

is there any way to do a USB transfer over and over again until there is no more data coming in? Because the amount of data coming in can be anything from 8 bytes to a few MB


In reply to user387184

Would something like this work to transfer large amounts of data?

while ( conn.bulkTransfer(epIN, buffer, 57600, 500) > 0 ){
    \\do some stuff
}

回答1:


If your USB connection really stops receiving data if there is no more data then all you need to do is to put a while receivedBufferIsNotEmpty around your statement since:

lenOfReceivedBuffer = conn.bulkTransfer(epIN, buffer, 57600, 500);

gets 0 in lenOfReceivedBuffer if no more data can be found on the port using 500ms timeout.

You may also increase the timeout a bit, if your sending side is too slow.




回答2:


When trying to communicate with a (ultimately) serial device as you have downstream of the converter, it is quite useful if the protocol is designed such that messages sent and received have terminating characters. This prevents you having to guess if more data will be coming in another few milliseconds. Newline is a very common choice. For example:

Host: What is your name?\n

Device: Sir Galahad of Camelot.\n

Host: What is your favourite colour?\n

Device: Blue\nNo, yellow\n

Given that the device has terminated the response with \n after "Blue" the host won't be expecting to see the second clarifying answer. Typically what would happen if the device "broke the rules" this way is that the host won't read "No, yellow" until it looking for the answer to whatever it asks next, at which point it would probably reject it as an implausible response, and issue a reset command to the device. (Some protocols have a "pre-pend the response with a repeat of the question" scheme to protect against such confusion)

There are of course two other well known possibilities apart from using a terminating character: one would be very abbreviated codes - single character or potentially unprintable binary - having expected length responses (possibly different depending on the beginning of the code). The other is having something in the way of a packet structure - an identifiable header, a length field, and then data.

The main point though is that "wait and see if there is any more" protocols are primarily used between two people or between a person and an automated system. When automated systems talk to each other, they tend instead to use a protocol which defines how you know if there will be any more. If the creator of your ultimate downstream endpoint device's firmware knew their business, there will be evident rules for when you should, and should not, expect more data.



来源:https://stackoverflow.com/questions/10931558/transferring-larger-amounts-of-data-usb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!