gpt4 book ai didi

java - 没有 USB 设备识别 libusbJava/ubuntu 13.04

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:12 25 4
gpt4 key购买 nike

我正在尝试在 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 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 007 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 008 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 009 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub


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;
}

}

它的输出是:

device found
Usb_Device_Descriptor idVendor: 0x10c4, idProduct: 0xea60
handle : 140182634002272


usb-error


最佳答案

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

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

如果您想真正将设备作为具有直接 USB 访问权限的 libusb 设备进行访问,则需要遵循 CP210x 协议(protocol)。这可以在 AN571 中的 Silicon Labs 网站上找到:

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

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

关于java - 没有 USB 设备识别 libusbJava/ubuntu 13.04,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18853406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com