我尝试创建一个可以与 USB msp430 微控制器 USB(HID 类)通信的应用程序,因此我尝试将字母 T 发送给 récupperer 微控制器中的当前日期(异步)。当我调用“getLokerTime”函数时,应用程序崩溃。我认为这是因为“if (connection.requestWait () == request)”,因为当我输入提供的评论时,应用程序不会崩溃。
你能帮助我吗?
void getLokerTime(UsbDeviceConnection 连接){
int bufferMaxLength=endPointWrite.getMaxPacketSize();
ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength);
UsbRequest request = new UsbRequest(); // create an URB
request.initialize(connection, endPointWrite);
buffer.putChar('T');
// queue the outbound request
boolean retval = request.queue(buffer, 1);
Toast.makeText(getApplicationContext(), " envoi de la donnée " + retval , Toast.LENGTH_SHORT).show();
if (connection.requestWait() == request) {
//if(retval == true){
// wait for confirmation (request was sent)
UsbRequest inRequest = new UsbRequest();
// URB for the incoming data
inRequest.initialize(connection, endPointRead);
// the direction is dictated by this initialisation to the incoming endpoint.
if(inRequest.queue(buffer, bufferMaxLength) == true){
connection.requestWait();
// wait for this request to be completed
// at this point buffer contains the data received
byte[] dst = new byte[8];
buffer.get(dst);
buffer.clear();
String contenu;
//byte[] data = buffer.array();
//String str = new String( data);
try {
contenu = new String(dst , "UTF-8");
Toast.makeText(getApplicationContext(), " le contenu du buffer : " + contenu , Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}