1

我正在尝试在 Linux 下用 CP2102 芯片编写一些与 USB 转换器的通信。谁能帮助我,为什么这不起作用?

ondra@ondra-notebook:~$ lsusb
Bus 001 Device 002: ID 1bcf:2805 Sunplus Innovation Technology Inc.
Bus 003 Device 006: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light
Bus 004 Device 025: ID 0cf3:3005 Atheros Communications, Inc. AR3011 Bluetooth
Bus 008 Device 004: ID 09da:0080 A4 Tech Co., Ltd
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 根集线器
总线 003 设备 001:ID 1d6b:0001 Linux Foundation 1.1 根集线器
总线 004 设备 001:ID 1d6b:0001 Linux Foundation 1.1 根集线器
总线 005 设备 001:ID 1d6b:0001 Linux Foundation 1.1 根集线器
总线 006 设备 001:ID 1d6b:0002 Linux Foundation 2.0 根集线器
总线 007 设备 001:ID 1d6b:0003 Linux Foundation 3.0 根集线器
总线 008 设备 001:ID 1d6b:0002 Linux Foundation 2.0 根集线器
总线 009 设备 001:ID 1d6b: 0003 Linux Foundation 3.0 根集线器


Java代码在这里:

import ch.ntb.usb.*;

public class libUSB_example2 {

/**
 * @param args
 */

static long handle = 0;         //Device-Handle
int retval = 0;                 //used for return values
byte[] dummybyte = new byte[1]; //used by specific write-functions as value to write (empty)

final int I2C_M_RD = 0x01;  
final int STATUS_IDLE        = 0;
final int STATUS_ADDRESS_ACK = 1;
final int STATUS_ADDRESS_NAK = 2;  
final int I2C_TINY_USB_VID = 0x0403;
final int I2C_TINY_USB_PID = 0xc631;
final int USB_TYPE_CLASS = (0x01 << 5);
final int USB_TYPE_VENDOR = (0x02 << 5); 
final int USB_ENDPOINT_IN = 0x80;
final int USB_ENDPOINT_OUT  = 0x00;    
final int USB_CTRL_IN = (USB_TYPE_CLASS | USB_ENDPOINT_IN);
final int USB_CTRL_OUT = (USB_TYPE_CLASS);  
final int CMD_GET_FUNC   = 1;
final int CMD_SET_DELAY  = 2;
final int CMD_GET_STATUS = 3;
final int CMD_I2C_IO     = 4;
final int CMD_I2C_BEGIN  = 1;  
final int CMD_I2C_END    = 2; 



public static void main(String[] args) throws USBException{
    // TODO Auto-generated method stub

    int result = 0;

    libUSB_example2 ex = new libUSB_example2();
    result = ex.UsbInitLinux();

    System.out.println("\n" + "*******************************************************");
    if (result == 0) System.out.println("success");
    if (result == -1) System.out.println("usb-error");
    if (result == -2) System.out.println("write command status failed");
    if (result == -3) System.out.println("read data status failed");
    if (result == -4) System.out.println("cannot open device");
    if (result == -5) System.out.println("no converter found");
    if (result == -6) System.out.println("tiny-usb-Error");
    if (result == -7) System.out.println("parameter-Error");
    if (result == -8) System.out.println("usb claim/release-error");
    System.out.println("*******************************************************" + "\n");


}

    public int UsbInitLinux()  { 

        LibusbJava.usb_init();
        LibusbJava.usb_find_busses();
        LibusbJava.usb_find_devices();
        Usb_Bus bus = LibusbJava.usb_get_busses();
        Usb_Device dev = bus.getDevices();

        for(bus = LibusbJava.usb_get_busses(); bus != null; bus = bus.getNext()){
            for(dev = bus.getDevices(); dev != null; dev = dev.getNext()){
                if(dev.getDescriptor().getIdVendor() == 0x10c4 && dev.getDescriptor().getIdProduct() == -5536){
                    handle = LibusbJava.usb_open(dev);
                    System.out.println("device found");
                    System.out.println(dev.getDescriptor());
                    System.out.println("handle : " + handle + "\n");
                    if(handle == 0)
                          return -4;    
                }
            }
        }
        if(handle <= 0)
            return -5;
        retval = UsbSet(CMD_SET_DELAY, 10);
        if(retval != 0)
            return retval;
        retval = LibusbJava.usb_claim_interface(handle, 0);
        if (retval != 1) 
            return -8;
        return 0;
    }

    public int UsbSet(int cmd, int value) {     
        if(LibusbJava.usb_control_msg(handle, 
               USB_CTRL_IN, CMD_GET_STATUS, 0, 0, dummybyte, 1, 1000)  < 0) {
          return -1;
        }
        return 0;
      }  

}

它的输出是:

找到
设备 Usb_Device_Descriptor idVendor:0x10c4,idProduct:0xea60
句柄:140182634002272


USB错误


4

1 回答 1

1

您使用的CP2102可以通过内核中的cp210x.ko作为虚拟串口访问,也可以直接通过libusb访问设备。

我建议使用 cp210x.ko 驱动程序并将设备作为串行端口打开(例如 /dev/ttyUSB0)。SerialPort然后,您可以使用 Java对象之类的东西作为串行设备与它进行通信。这样做的原因是设备的协议是抽象的,你只需要知道如何编写一些串口代码来发送或接收数据。

如果您想通过直接 USB 访问真正将设备作为 libusb 设备访问,您需要遵循 CP210x 协议。这可以在 Silicon Labs 网站的 AN571 中找到:

http://www.silabs.com/Support%20Documents/TechnicalDocs/AN571.pdf

您的虚拟控制消息很可能是虚假的,因此设备将停止请求,从而产生您看到的错误。您将需要发出接口启用请求(IFC_ENABLE在 AN571 中找到)以开始与设备的任何通信。这将是一个很好的测试控制消息,以确定您是否正在与设备连接。

于 2013-09-20T01:37:40.027 回答