我正在开发一个程序,该程序允许我在我的 Android 手机和使用 FTDI 芯片 (FT232R) 进行 USB 通信的微控制器之间交换数据。到目前为止,我已经成功创建了一个线程,允许我在不影响应用程序其余部分的情况下执行 USB 通信。此外,我已经成功地能够初始化和发送数据到 FTDI 设备,但我似乎无法从设备中读取数据。我已将 USB 设备设置为每 100 毫秒发送一个 4 字节数据包,以查看是否可以读取数据包并将其回显到总线上。但是,我无法读取数据。
在设备端,我每 100 毫秒发送一个四字节 ASCII 数据包“a''b''c''d'。在主机端,更具体地说是 USB 线程,我正在尝试查看是否可以接收此数据。我正在读取 while 循环内的数据,该循环正在检查接收缓冲区中的第一个位置是否等于正在发送的任何字符(a、b、c 或 d)。我这样做的原因是因为我怀疑数据是混乱的。在 while 循环内,每次都会清除缓冲区。然后调用 bulkTransfer 函数来尝试接收数据,如果我理解正确,它将接收数据,直到它接收到 64 个字节或经过 1000 毫秒(超时)。然后,我通过检查缓冲区第一个元素中的 4 个字符中的任何一个来检查是否收到了任何发送的数据。如果我检测到一个字符,则退出 while 循环并将前四个字符从缓冲区发送到总线上。然而,我用逻辑分析仪监控总线,我似乎永远无法检测到来自主机的任何传输,这表明我没有接收到数据。任何帮助将不胜感激。我已经做了很多工作来达到这一点,但我似乎被困住了。谢谢你。我已经做了很多工作来达到这一点,但我似乎被困住了。谢谢你。我已经做了很多工作来达到这一点,但我似乎被困住了。谢谢你。
USB 从属代码 (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);
}
安卓代码
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();
}
}