带健康设备的串口转 USB Android 应用程序

Posted

技术标签:

【中文标题】带健康设备的串口转 USB Android 应用程序【英文标题】:Serial to USB Android application with health device 【发布时间】:2012-10-01 07:31:16 【问题描述】:

我正在创建一个 android 应用程序,让我的 Galaxy 平板电脑通过串行到 USB 连接与健康设备进行通信。

我实现的代码不起作用! OUT 和 IN 通信都没有开始

有人知道吗?

public void recordData(View _view)
    text = (TextView)findViewById(R.id.textView1);





    manager=(UsbManager) getSystemService(Context.USB_SERVICE);

    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);


    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    while(deviceIterator.hasNext())
        UsbDevice device = deviceIterator.next();
        manager.requestPermission(device, mPermissionIntent);
        text.setText(text.getText()+"\n"+device.getDeviceName());
    





private UsbManager manager=null;

private boolean forceClaim = true;
private UsbDeviceConnection connection;
private UsbEndpoint input = null,output=null; 
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() 

        public void onReceive(Context context, Intent intent) 
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) 
                synchronized (this) 
                    isRecording=true;
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) 
                        if(device != null)
                          //call method to set up device communication
                            UsbInterface intf = device.getInterface(0);
                            connection = manager.openDevice(device); 
                            connection.claimInterface(intf, forceClaim);

                            //connection settings
                            int op= connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset   //0x40
                            int op2= connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0);//clear Rx  
                            int op3= connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
                            int op3b=  connection.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0);//control flow

                            int op4= connection.controlTransfer(0x40, 0x03, 0x001A, 0, null, 0, 0);// baud rate 115200
                            int op5= connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0);//8 bit

                            int endPts = intf.getEndpointCount();

                            for(int e = 0; e < endPts; e++)
                                UsbEndpoint endpoint = intf.getEndpoint(e);
                                endpoint.getAttributes();

                                if( endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
                                    if(endpoint.getDirection() == UsbConstants.USB_DIR_IN)
                                        input = endpoint;
                                        Log.d("Endpoint", "Got input");

                                    else if(endpoint.getDirection() == UsbConstants.USB_DIR_OUT)
                                        output = endpoint;
                                        Log.d("Endpoint", "Got output");
                                                           
                                
                            



                            text.setText(text.getText()+"\nOut= "+String.valueOf(output.getEndpointNumber()));
                            text.setText(text.getText()+"\nIn= "+String.valueOf(input.getEndpointNumber()));


                            byte [] buffer = 77; // M\n in ascii
                            for (int i = 0; i < buffer.length; ++i)
                            
                                connection.bulkTransfer(output, new byte[] buffer[i], 1, 0);
                            



                            read();

                       
                     
                    else 
                        //error
                    
                
            
        
    ;

//read thread
    private void read() 
        Runnable r = new Runnable() 
            byte[] buffer = new byte[64];
            byte[] buffer2;
            public void run() 
                while (isRecording) 
                    
                        int op = connection.bulkTransfer(input, buffer, 64, 0);

                        if (op > 2) 
                            buffer2 = new byte[op];
                            for (int i = 0; i < op - 2; ++i) 
                                buffer2[i] = buffer[i+2];
                                text.setText(text.getText()+"\n"+String.valueOf(buffer2[i]));
                            

                        
                    

                
            
        ;
        Thread t = new Thread(r);
        t.start();

非常感谢!

【问题讨论】:

您在 Logcat 中看到了哪些错误消息? 【参考方案1】:

在尝试发送字节之前,您必须先控制事务:

用途:

// for writing to USB with 9600 baudrate
connection.controlTransfer(0x00000000, 0x03, 0x4138, 0, null, 0, 0);

// for reading from USB with 9600 baudrate
connection.controlTransfer(0x00000080, 0x03, 0x4138, 0, null, 0, 0);

参考:

http://developer.android.com/guide/topics/connectivity/usb/host.html http://developer.android.com/reference/android/hardware/usb/UsbConstants.html

【讨论】:

我有一个条形码扫描仪,它有一个批量传输 IN 端点。每当我尝试进行 controlTransfer 调用时,它总是返回-1。这是一个已知问题吗?我无法从该设备读取任何内容。 我认为您必须定义用于读取的合适设备类型,查看下面的链接可能会帮助您解决问题link

以上是关于带健康设备的串口转 USB Android 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

Android OTG之USB转串口模块通讯

Android USB转串口通信开发基本流程

android串口消息

Android项目实战(四十五):Usb转串口通讯(CH34xUARTDriver)

usb转串口驱动怎么安装 usb转串口驱动安装异常的解决方法

一个USB串口分成多个USB串口的原理是啥