Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1511265
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: 项目管理

2014-08-05 10:59:36

1. 源码参考这里 
2. 修改的Makefile

点击(此处)折叠或打开

  1. HAVEUSB = 1
  2. OPENWRT = 1

  3. ifeq ($(OPENWRT), 1)
  4.     #CC = ~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
  5.     CC = ~/OpenWrt-Toolchain-ar71xx-for-mips_r2-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
  6.   #CC = ~/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc
  7.     CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib

  8. else
  9.     CC = gcc
  10. endif

  11. CFLAGS += -Wall -O2
  12. CFLAGS1 += -fPIC -shared        #仅so 文件需要

  13. ifeq ($(HAVEUSB), 1)
  14.     LFLAGS += -lusb
  15. endif

  16. #可执行文件名和相关的obj文件
  17. #so的名称必须和c中代码对应
  18. SHAREOBJ = lualibusb.so
  19. SRCS += lualibusb.c

  20. #以下无需修改
  21. OBJS = $(SRCS:.c=.o)

  22. all: SO_FILE

  23. SO_FILE: $(OBJS)
  24.     $(CC) $(CFLAGS) $(CFLAGS1) $(SRCS) -o $(SHAREOBJ) $(LFLAGS)

  25. .PHONY: clean
  26. clean:
  27.     @echo "cleanning project"
  28.     $(RM) *.a $(OBJS) *~ *.so *.lo $(SHAREOBJ)
  29.     @echo "clean completed"
3. 修改后的lua的测试代码

点击(此处)折叠或打开

  1. --[[
  2. libusb-0.1.12的lua调用例程
  3. Authors: Jorge Visca, Andres Aguirre
  4. MINA - INCO - Facultad de Ingeniería - UDELAR
  5. http://luaforge.net/projects/lualibusb
  6. ]]--

  7. print("--> Hello LUA, qiushui_007 test!")

  8. --assert(package.loadlib("Debug/libluausb.so","luaopen_libusb"))()
  9. local libusb = require "lualibusb"
  10. --libusb.find_busses()
  11. --libusb.find_devices()

  12. print ("\n------------------------------------")
  13. print ("-- A few exported constants:")
  14. print ("USB_CLASS_HUB", USB_CLASS_HUB)
  15. print ("USB_DT_STRING", USB_DT_STRING)

  16. --[[
  17. print ("\n------------------------------------")
  18. print ("-- Dumping libusb:", libusb)
  19. for k, _ in pairs(libusb) do
  20.     print (k)
  21. end
  22. ]]--

  23. local buses = libusb.get_busses()
  24. local last_device

  25. print ("\n------------------------------------")
  26. print ("-- Dumping buses&devices:", buses)
  27. for dirname, bus in pairs(buses) do
  28.     --print (k)
  29.     local devices = libusb.get_devices(bus)
  30.     for filename, device in pairs(devices) do

  31.         local descriptor = libusb.device_descriptor(device)
  32.         print ("Bus: " .. dirname, "Device: " .. filename,
  33.                 "VID:PID : " .. string.format("%04x:%04x", descriptor.idVendor, descriptor.idProduct)) --, "desc:", descriptor.iProduct, s or err)
  34.         last_device = device

  35.     end

  36. end

  37. print ("\n------------------------------------")
  38. print ("-- Device descriptor:", last_device)
  39. local descriptor = libusb.device_descriptor(last_device)
  40. for k, v in pairs(descriptor) do
  41.     print (k, v)
  42. end

  43. print ("\n------------------------------------")
  44. print ("-- Device configuration:", last_device)
  45. local config = libusb.device_config(last_device)
  46. for k, v in pairs(config) do
  47.     print (k, v)
  48. end

  49. print ("\n------------------------------------")
  50. print ("-- Interface settings:", last_device)
  51. local altsettings = libusb.interface_alt_settings(config.interface)
  52. for k, v in pairs(altsettings) do
  53.     print ("+ Alternative", k, v)
  54.     for ka, va in pairs(v) do
  55.         print (ka, va)
  56.     end
  57. end

4. 将so和lua文件拷贝到WR703N上, 运行结果
4.1 U口不接任何设备

点击(此处)折叠或打开

  1. root@OpenWrt:/xutest# lua libusb.lua
  2. --> Hello LUA, qiushui_007

  3. ------------------------------------
  4. -- A few exported constants:
  5. USB_CLASS_HUB 9
  6. USB_DT_STRING 3

  7. ------------------------------------
  8. -- Dumping buses&devices: table: 0x45e260
  9. Bus: 001 Device: 001 VID:PID : 1d6b:0002

  10. ------------------------------------
  11. -- Device descriptor: userdata: 0x466dd8
  12. bDeviceProtocol 1
  13. bMaxPacketSize0 64
  14. bDeviceSubClass 0
  15. bLength 18
  16. bNumConfigurations 1
  17. bcdUSB 512
  18. iSerialNumber 1
  19. iProduct 2
  20. bcdDevice 771
  21. iManufacturer 3
  22. bDescriptorType 1
  23. idProduct 2
  24. bDeviceClass 9
  25. idVendor 7531

  26. ------------------------------------
  27. -- Device configuration: userdata: 0x466dd8
  28. bmAttributes 224
  29. iConfiguration 0
  30. MaxPower 0
  31. extra
  32. bConfigurationValue 1
  33. wTotalLength 25
  34. bDescriptorType 2
  35. bLength 9
  36. bNumInterfaces 1
  37. interface userdata: 0x45ef00

  38. ------------------------------------
  39. -- Interface settings: userdata: 0x466dd8
  40. + Alternative 0 table: 0x465518
  41. bInterfaceSubClass 0
  42. bInterfaceClass 9
  43. bLength 9
  44. bNumEndpoints 1
  45. bInterfaceNumber 0
  46. extra
  47. iInterface 0
  48. bDescriptorType 4
  49. bInterfaceProtocol 0
  50. endpoint userdata: 0x464460
  51. bAlternateSetting 0
  52. root@OpenWrt:/xutest

4.2 接上CR95HF, VID = 0x0483, PID = 0xd0d0

点击(此处)折叠或打开

  1. root@OpenWrt:/xutest# lua libusb.lua
  2. --> Hello LUA, qiushui_007

  3. ------------------------------------
  4. -- A few exported constants:
  5. USB_CLASS_HUB 9
  6. USB_DT_STRING 3

  7. ------------------------------------
  8. -- Dumping buses&devices: table: 0x539260
  9. Bus: 001 Device: 001 VID:PID : 1d6b:0002
  10. Bus: 001 Device: 009 VID:PID : 0483:d0d0

  11. ------------------------------------
  12. -- Device descriptor: userdata: 0x541dd8
  13. bDeviceProtocol 0
  14. bMaxPacketSize0 64
  15. bDeviceSubClass 0
  16. bLength 18
  17. bNumConfigurations 1
  18. bcdUSB 512
  19. iSerialNumber 3
  20. iProduct 2
  21. bcdDevice 512
  22. iManufacturer 1
  23. bDescriptorType 1
  24. idProduct 53456
  25. bDeviceClass 0
  26. idVendor 1155

  27. ------------------------------------
  28. -- Device configuration: userdata: 0x541dd8
  29. bmAttributes 128
  30. iConfiguration 0
  31. MaxPower 100
  32. extra
  33. bConfigurationValue 1
  34. wTotalLength 41
  35. bDescriptorType 2
  36. bLength 9
  37. bNumInterfaces 1
  38. interface userdata: 0x539f00

  39. ------------------------------------
  40. -- Interface settings: userdata: 0x541dd8
  41. + Alternative 0 table: 0x540880
  42. bInterfaceSubClass 0
  43. bInterfaceClass 3
  44. bLength 9
  45. bNumEndpoints 2
  46. bInterfaceNumber 1
  47. extra !
  48. iInterface 14
  49. bDescriptorType 4
  50. bInterfaceProtocol 0
  51. endpoint userdata: 0x53ad28
  52. bAlternateSetting 0
  53. root@OpenWrt:/xutest

4.3 接上DS9490R, VID = 0x04fa, PID = 0x2490

点击(此处)折叠或打开

  1. root@OpenWrt:/xutest# lua libusb.lua
  2. --> Hello LUA, qiushui_007

  3. ------------------------------------
  4. -- A few exported constants:
  5. USB_CLASS_HUB 9
  6. USB_DT_STRING 3

  7. ------------------------------------
  8. -- Dumping buses&devices: table: 0x619260
  9. Bus: 001 Device: 001 VID:PID : 1d6b:0002
  10. Bus: 001 Device: 010 VID:PID : 04fa:2490

  11. ------------------------------------
  12. -- Device descriptor: userdata: 0x621dd8
  13. bDeviceProtocol 255
  14. bMaxPacketSize0 8
  15. bDeviceSubClass 255
  16. bLength 18
  17. bNumConfigurations 1
  18. bcdUSB 256
  19. iSerialNumber 0
  20. iProduct 0
  21. bcdDevice 2
  22. iManufacturer 0
  23. bDescriptorType 1
  24. idProduct 9360
  25. bDeviceClass 255
  26. idVendor 1274

  27. ------------------------------------
  28. -- Device configuration: userdata: 0x621dd8
  29. bmAttributes 224
  30. iConfiguration 0
  31. MaxPower 240
  32. extra
  33. bConfigurationValue 1
  34. wTotalLength 129
  35. bDescriptorType 2
  36. bLength 9
  37. bNumInterfaces 1
  38. interface userdata: 0x619f00

  39. ------------------------------------
  40. -- Interface settings: userdata: 0x621dd8
  41. + Alternative bInterfaceSubClass 255
  42. lua: libusb.lua:66: bad argument #1 to 'pairs' (table expected, got number)
  43. stack traceback:
  44.         [C]: in function 'pairs'
  45.         libusb.lua:66: in main chunk
  46.         [C]: ?
  47. root@OpenWrt:/xutest



阅读(4087) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~