我正在开发一个程序,允许我在我的Android手机和使用FTDI芯片(FT232R)进行USB通信的微控制器之间交换数据。到目前为止,我已经成功创建了一个线程,允许我执行USB通信而不会影响应用程序的其余部分。此外,我已经成功地初始化并向FTDI设备发送数据,但我似乎无法从设备读取数据。我已经设置了USB设备,每100毫秒发送一个4字节的数据包,看看我是否可以读取数据包并将其回送到总线上。但是,我无法读取数据。
在设备方面,我每100毫秒发送一个四字节的ASCII数据包'a''''''''''在主机端,更具体地说是USB线程,我试图看看我是否可以接收这些数据。我正在读取while循环中的数据,该循环正在检查我的接收缓冲区中的第一个位置是否等于正在发送的任何字符(a,b,c或d)。我这样做的原因是因为我怀疑数据是混乱的。在while循环中,每次都清除缓冲区。然后调用bulkTransfer函数来尝试和接收数据,如果我理解正确,将接收数据,直到它已经接收到64字节或1000毫秒(超时)。然后我检查是否收到了通过检查缓冲区第一个元素中的任何4个字符发送的任何数据。如果我检测到一个字符,则while循环退出,我将前四个字符从缓冲区发送到总线上。但是,我用逻辑分析仪监控总线,我似乎永远无法检测到来自主机的任何传输,表明我没有收到数据。任何帮助将不胜感激。我已经做了很多工作来达到这一点,但我似乎陷入困境。谢谢。
USB Slave Code(C prog。)
while(1)
{
__delay_ms(100);
unsigned char response[4] = {0};
response[0] = 'a';
response[1] = 'b';
response[2] = 'c';
response[3] = 'd';
send_UART(4, response);
}
Android代码
public class MainActivity extends Activity
{
// Developer Notifications
private boolean developer_notifications = true;
// Sensor Constants
public static int temperature;
public static int humidity;
public static int lpg;
public static int alcohol;
// Layout
ListView listView;
// USB
UsbDevice USBDevice_s;
UsbDeviceConnection USBDeviceConnection_s;
UsbEndpoint USBEndpoint_s_control;
UsbEndpoint USBEndpoint_s_data;
UsbEndpoint USBEndpoint_s_data_out = null;
UsbEndpoint USBEndpoint_s_data_in = null;
UsbInterface USBInterface_s_control;
UsbInterface USBInterface_s_data;
UsbManager USBManager_s;
UsbRequest USBRequest_s;
ByteBuffer USBBuffer_s;
public static int USBBuffer_s_length;
private static final byte[] USBBuffer_s_received_data = new byte[]{ (byte)0x00, 0x00, 0x00, 0x00};
//USBBuffer_s_received_data = 0;
// Communication Flags
public static int Sense_Sensor_ID_Request = 0;
public static int Sense_Sensor_ID_Response = 0;
// Thread
private Thread USB_Communication_Handler;
public static int threadflag = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Interface
Model.LoadModel();
listView = (ListView) findViewById(R.id.listView);
String[] ids = new String[Model.Items.size()];
for (int i= 0; i < ids.length; i++)
{ids[i] = Integer.toString(i+1);}
ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids);
listView.setAdapter(adapter);
// Developer Notifications
developer_notifications = true;
if ((developer_notifications))
{Toast.makeText(this,"Developer Notifications Enabled", Toast.LENGTH_LONG).show();}
// Begin USB Configuration | Grab Device List
USBManager_s = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = USBManager_s.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext())
{USBDevice_s = deviceIterator.next();}
Intent Intent_s = getIntent();
String action = Intent_s.getAction();
USBDevice_s = (UsbDevice) Intent_s.getParcelableExtra(UsbManager.EXTRA_DEVICE);
// Upon Device Attachment
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action))
{
initialize_USB();
USB_Communication_Handler();
}
else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
{
USBDevice_s = (UsbDevice) Intent_s.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (USBDevice_s != null)
{
USBDeviceConnection_s = USBManager_s.openDevice(USBDevice_s);
//USBDeviceConnection_s.releaseInterface(USBInterface_s);
USBDeviceConnection_s.close();
Toast.makeText(this,"Device Removed, Interface Released", Toast.LENGTH_LONG).show();
}
return;
}
//Layout / GUI Created - wait for USB data...
temperature = 1; humidity = 63; lpg = 5000; alcohol = 500;
Model.LoadModel();
ItemAdapter adapter2 = new ItemAdapter(this,R.layout.row, ids);
listView.setAdapter(adapter2);
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//Toast.makeText(this, "Exiting", Toast.LENGTH_SHORT)
// .show();
android.os.Process.killProcess(android.os.Process.myPid());
finish();
break;
default:
break;
}
return true;
}
//@Override
public void initialize_USB()
{
USBInterface_s_control = USBDevice_s.getInterface(0);
USBDeviceConnection_s = USBManager_s.openDevice(USBDevice_s);
USBDeviceConnection_s.claimInterface(USBInterface_s_control, true);
if ((developer_notifications))
{Toast.makeText(this,"Sense found ("+USBDevice_s.getDeviceName()+")", Toast.LENGTH_LONG).show();}
// Send Initialization Packets
if (USBDeviceConnection_s != null && USBDeviceConnection_s.claimInterface(USBInterface_s_control, true))
{
// Reset, Clear RX/TX, & Set Baudrate
USBDeviceConnection_s.controlTransfer(0x40, 0, 0, 0, null, 0, 0);
USBDeviceConnection_s.controlTransfer(0x40, 0, 1, 0, null, 0, 0);
USBDeviceConnection_s.controlTransfer(0x40, 0, 2, 0, null, 0, 0);
USBDeviceConnection_s.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);
if ((developer_notifications))
{Toast.makeText(this,"Sense Enumerated & Initialized", Toast.LENGTH_LONG).show();}
}
// Grab Interface | Grab Endpoints | Configure for Data Exchange
USBInterface_s_data = USBDevice_s.getInterface(0);
for (int i = 0; i < USBInterface_s_data.getEndpointCount(); i++)
{
if (USBInterface_s_data.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
{
if (USBInterface_s_data.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
USBEndpoint_s_data_in = USBInterface_s_data.getEndpoint(i);
else
USBEndpoint_s_data_out = USBInterface_s_data.getEndpoint(i);
}
}
USBBuffer_s_length = USBEndpoint_s_data_in.getMaxPacketSize();
USBBuffer_s = ByteBuffer.allocate(USBBuffer_s_length);
if ((developer_notifications))
{Toast.makeText(this,"Ready for Data Exchange", Toast.LENGTH_LONG).show();}
//USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_out, new byte[] { 0x64 }, 1, 0);
}
public void send_data_USB()
{}
public void receive_data_USB()
{}
public void send_data_USB()
{}
public void receive_data_USB()
{}
public void USB_Communication_Handler()
{
USB_Communication_Handler = new Thread()
{
public void run()
{
while(USBBuffer_s_received_data[0] != 'a' || USBBuffer_s_received_data[0] != 'b' ||USBBuffer_s_received_data[0] != 'c' ||USBBuffer_s_received_data[0] != 'd')
{
for(int i = 0 ; i < USBBuffer_s_received_data.length ; i++)
{
USBBuffer_s_received_data[i] = '\0';
}
//USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_in, USBBuffer_s_received_data, 0, 8, 500);
//USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_out, USBBuffer_s_received_data, 0, 8, 500);
USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_in, USBBuffer_s_received_data, 64, 1000);
}
USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_out, USBBuffer_s_received_data, 4, 500);
}
}
};
USB_Communication_Handler.start();
}
}