Вобщем нужно было пообщатся по USB. Хотелось чтобы это было через libusb. Гугль по этому ничего внятного не выдал, кроме попыток немцев с прикручиванием онного. У немца получился облом при вызове usb_open , заключающийся в вылете программы при ее вызове, что я и проверил на себе. После дня мучений получилось вызвать путем импорта функции из DLL через Prototype. Я обрадовался, но было уже поздно. Утром попробывал выйти из процедуры обработки, получилась ошибка. Я огорчился. Поотлаживав понял что облом происходит потому, что в стеке остается число после вызова DLL. Можно конечно через асм ему POP сделать, но может можно по нормальному ?
Делалось в 5.0
Сабж:
usbheader.pb
#LIBUSB_PATH_MAX = 512 ; Device And/Or Interface Class codes #USB_CLASS_PER_INTERFACE = 0 ; For DeviceClass #USB_CLASS_AUDIO = 1 #USB_CLASS_COMM = 2 #USB_CLASS_HID = 3 #USB_CLASS_PRINTER = 7 #USB_CLASS_MASS_STORAGE = 8 #USB_CLASS_HUB = 9 #USB_CLASS_DATA = 10 #USB_CLASS_VENDOR_SPEC = $ff ; Descriptor types #USB_DT_DEVICE = $01 #USB_DT_CONFIG = $02 #USB_DT_STRING = $03 #USB_DT_INTERFACE = $04 #USB_DT_ENDPOINT = $05 #USB_DT_HID = $21 #USB_DT_REPORT = $22 #USB_DT_PHYSICAL = $23 #USB_DT_HUB = $29 ; Descriptor sizes per descriptor type #USB_DT_DEVICE_SIZE = 18 #USB_DT_CONFIG_SIZE = 9 #USB_DT_INTERFACE_SIZE = 9 #USB_DT_ENDPOINT_SIZE = 7 #USB_DT_ENDPOINT_AUDIO_SIZE = 9 ; Audio extension #USB_DT_HUB_NONVAR_SIZE = 7 ; All standard descriptors have these 2 fields in common Structure usb_descriptor_header bLength.c bDescriptorType.c EndStructure ; String descriptor Structure usb_string_descriptor bLength.c bDescriptorType.c wData.u[1] EndStructure ; HID descriptor Structure usb_hid_descriptor bLength.c bDescriptorType.c bcdHID.u bCountryCode.c bNumDescriptors.c EndStructure ; Endpoint descriptor #USB_MAXENDPOINTS = 32 Structure usb_endpoint_descriptor bLength.c bDescriptorType.c bEndpointAddress.c bmAttributes.c wMaxPacketSize.u bInterval.c bRefresh.c bSynchAddress.c *extra.c ; Extra descriptors extralen.l EndStructure #USB_ENDPOINT_ADDRESS_MASK = $0f ; in bEndpointAddress #USB_ENDPOINT_DIR_MASK = $80 #USB_ENDPOINT_TYPE_MASK = $03 ; in bmAttributes #USB_ENDPOINT_TYPE_CONTROL = 0 #USB_ENDPOINT_TYPE_ISOCHRONOUS = 1 #USB_ENDPOINT_TYPE_BULK = 2 #USB_ENDPOINT_TYPE_INTERRUPT = 3 ; Interface descriptor #USB_MAXINTERFACES = 32 Structure usb_interface_descriptor bLength.c bDescriptorType.c bInterfaceNumber.c bAlternateSetting.c bNumEndpoints.c bInterfaceClass.c bInterfaceSubClass.c bInterfaceProtocol.c iInterface.c *endpoint.usb_endpoint_descriptor *extra.c ; Extra descriptors extralen.l EndStructure #USB_MAXALTSETTING = 128 ; Hard limit Structure usb_interface *altsetting.usb_interface_descriptor num_altsetting.l EndStructure ; Configuration descriptor information.. #USB_MAXCONFIG = 8 Structure usb_config_descriptor bLength.c bDescriptorType.c wTotalLength.u bNumInterfaces.c bConfigurationValue.c iConfiguration.c bmAttributes.c MaxPower.c *Interface.usb_interface *extra.c ; Extra descriptors extralen.l EndStructure ; Device descriptor Structure usb_device_descriptor bLength.c bDescriptorType.c bcdUSB.u bDeviceClass.c bDeviceSubClass.c bDeviceProtocol.c bMaxPacketSize0.c idVendor.u idProduct.u bcdDevice.u iManufacturer.c iProduct.c iSerialNumber.c bNumConfigurations.c EndStructure Structure usb_ctrl_setup bRequestType.c bRequest.c wValue.u wIndex.u wLength.u EndStructure ; Standard requests #USB_REQ_GET_STATUS = $00 #USB_REQ_CLEAR_FEATURE = $01 ; $02 is reserved #USB_REQ_SET_FEATURE = $03 ; $04 is reserved #USB_REQ_SET_ADDRESS = $05 #USB_REQ_GET_DESCRIPTOR = $06 #USB_REQ_SET_DESCRIPTOR = $07 #USB_REQ_GET_CONFIGURATION = $08 #USB_REQ_SET_CONFIGURATION = $09 #USB_REQ_GET_INTERFACE = $0A #USB_REQ_SET_INTERFACE = $0B #USB_REQ_SYNCH_FRAME = $0C #USB_TYPE_STANDARD = ($00 << 5) #USB_TYPE_CLASS = ($01 << 5) #USB_TYPE_VENDOR = ($02 << 5) #USB_TYPE_RESERVED = ($03 << 5) #USB_RECIP_DEVICE = $00 #USB_RECIP_INTERFACE = $01 #USB_RECIP_ENDPOINT = $02 #USB_RECIP_OTHER = $03 ; Various libusb API related stuff #USB_ENDPOINT_IN = $80 #USB_ENDPOINT_OUT = $00 ; Error codes #USB_ERROR_BEGIN = 500000 ; This is supposed To look weird. This file is generated from autoconf ; And I didn't want to make this too complicated. ; #USB_LE16_TO_CPU(x) ; Device reset types For usb_reset_ex. ; http://msdn.microsoft.com/en-us/library/ff537269%28VS.85%29.aspx ; http://msdn.microsoft.com/en-us/library/ff537243%28v=vs.85%29.aspx #USB_RESET_TYPE_RESET_PORT = (1 << 0) #USB_RESET_TYPE_CYCLE_PORT = (1 << 1) #USB_RESET_TYPE_FULL_RESET = (#USB_RESET_TYPE_CYCLE_PORT | #USB_RESET_TYPE_RESET_PORT) ; Data types ; struct usb_device ; struct usb_bus Structure usb_device *next.usb_device *prev.usb_device filename.c[#LIBUSB_PATH_MAX] *bus.usb_bus descriptor.usb_device_descriptor *config.usb_config_descriptor *dev.l ; Darwin support devnum.c num_children.c *children.usb_device EndStructure Structure usb_bus *next.usb_bus *prev.usb_bus dirname.c[#LIBUSB_PATH_MAX] *devices.usb_device location.l *root_dev.usb_device EndStructure ; Version information, Windows specific Structure dll major.l minor.l micro.l nano.l EndStructure Structure driver major.l minor.l micro.l nano.l EndStructure Structure usb_version dll.dll driver.driver EndStructure Structure usb_dev_handle fd.l *bus.usb_bus *device.usb_device config.l hinterface.l altsetting.l ; Added by RMT so implementations can store other per-open-device Data *impl_info.l EndStructure
USBlibP.pb
EnableExplicit IncludeFile "usbHeader.pb" ;константы и структуры необоходимые для libusb0.dll ; Прототипы функций ; ---------------------------------------------------------------------- ; allready tested and works fine Prototype.l prot_usb_get_version() Prototype.l prot_usb_init() Prototype.l prot_usb_find_busses() Prototype.l prot_usb_find_devices() Prototype.l prot_usb_get_busses() ; ---------------------------------------------------------------------- ; Don't work for me, but don't know why :( Prototype.q prot_usb_open(*dev.usb_device) ;returns *usb_dev_handle Prototype.l prot_usb_close(*dev.usb_dev_handle) ;returns 0 if success Prototype.l prot_usb_set_configuration(*dev.usb_dev_handle, configuration.l) ;returns 0 If success Prototype.l prot_usb_set_altinterface(*dev.usb_dev_handle, alternate.l) Prototype.l prot_usb_resetep(*dev.usb_dev_handle, ep.l) ; usb_resetep is deprecated. Use usb_clear_halt Prototype.l prot_usb_clear_halt(*dev.usb_dev_handle, ep.l) Prototype.l prot_usb_reset(*dev.usb_dev_handle) Prototype.l prot_usb_claim_interface(*dev.usb_dev_handle, hInterface.l) Prototype.l prot_usb_release_interface(*dev.usb_dev_handle, hInterface) Prototype.l prot_usb_control_msg(*dev.usb_dev_handle, requesttype.l, request.l, value.l, index.l, *bytes.c, size.l, timeout.l) Prototype.l prot_usb_get_string(*dev.usb_dev_handle, index.l, langid.l, *buf.c, buflen.i) Prototype.l prot_usb_get_string_simple(*dev.usb_dev_handle, index.l, *buf.c, buflen.i) Prototype.l prot_usb_get_descriptor(*dev.usb_dev_handle, type.c, index.c, *buf.l, size.l) Prototype.l prot_usb_get_descriptor_by_endpoint(*dev.usb_dev_handle, ep.l, type.c, index.c, *buf.l, size.l) Prototype.l prot_usb_bulk_write(*dev.usb_dev_handle, ep.l, *bytes.c, size.l, timeout.l) Prototype.l prot_usb_bulk_read(*dev.usb_dev_handle, ep.l, *bytes.c, size.l, timeout.l) Prototype.l prot_usb_interrupt_write(*dev.usb_dev_handle, ep.l, *bytes.c, size.l, timeout.l) Prototype.l prot_usb_interrupt_read(*dev.usb_dev_handle, ep.l, *bytes.c, size.l, timeout.l) Prototype.l prot_usb_get_driver_np(*dev.usb_dev_handle, hInterface.l, *name.c, namelen.l) Prototype.l prot_usb_detach_kernel_driver_np(*dev.usb_dev_handle, hInterface.l) Global libUsbID = OpenLibrary(#PB_Any, "libusb0.dll") ;Define Function-Pointers for CallFunctionFast If IsLibrary(libUsbID) Global usb_get_version.prot_usb_get_version = GetFunction(libUsbID, "usb_get_version") ;-Функции ядра Global usb_init.prot_usb_init = GetFunction(libUsbID, "usb_init") Global usb_find_busses.prot_usb_find_busses = GetFunction(libUsbID, "usb_find_busses") Global usb_find_devices.prot_usb_find_devices = GetFunction(libUsbID, "usb_find_devices") Global usb_get_busses.prot_usb_get_busses = GetFunction(libUsbID, "usb_get_busses") ;-Device operations Global usb_open.prot_usb_open = GetFunction(libUsbID, "usb_open") Global usb_close.prot_usb_close = GetFunction(libUsbID, "usb_close") Global usb_set_configuration.prot_usb_set_configuration = GetFunction(libUsbID, "usb_set_configuration") Global usb_set_altinterface.prot_usb_set_altinterface = GetFunction(libUsbID, "usb_set_altinterface") Global usb_resetep.prot_usb_resetep = GetFunction(libUsbID, "usb_resetep") Global usb_clear_halt.prot_usb_clear_halt = GetFunction(libUsbID, "usb_clear_halt") Global usb_reset.prot_usb_reset = GetFunction(libUsbID, "usb_reset") Global usb_claim_interface.prot_usb_claim_interface = GetFunction(libUsbID, "usb_claim_interface") Global usb_release_interface.prot_usb_release_interface = GetFunction(libUsbID, "usb_release_interface") ;-Control Transfer Global usb_control_msg.prot_usb_control_msg = GetFunction(libUsbID, "usb_control_msg") Global usb_get_string.prot_usb_get_string = GetFunction(libUsbID, "usb_get_string") Global usb_get_string_simple.prot_usb_get_string_simple = GetFunction(libUsbID, "usb_get_string_simple") Global usb_get_descriptor.prot_usb_get_descriptor = GetFunction(libUsbID, "usb_get_descriptor") Global usb_get_descriptor_by_endpoint.prot_usb_get_descriptor_by_endpoint = GetFunction(libUsbID, "usb_get_descriptor_by_endpoint") ;-Bulk Transfer Global usb_bulk_write.prot_usb_bulk_write = GetFunction(libUsbID, "usb_bulk_write") Global usb_bulk_read.prot_usb_bulk_read = GetFunction(libUsbID, "usb_bulk_read") ;-Interrupt Transfer Global usb_interrupt_write.prot_usb_interrupt_write = GetFunction(libUsbID, "usb_interrupt_write") Global usb_interrupt_read.prot_usb_interrupt_read = GetFunction(libUsbID, "usb_interrupt_read") ;-Non Portable Global usb_get_driver_np.prot_usb_get_driver_np = GetFunction(libUsbID, "usb_get_driver_np") Global usb_detach_kernel_driver_np.prot_usb_detach_kernel_driver_np = GetFunction(libUsbID, "usb_detach_kernel_driver_np") EndIf DisableExplicit
TestUSB.pb
;first of all, we have to call usb_init(). If result is > 0 than we can use the libusb0.dll
IncludeFile "USBlibP.pb" ;Functions we can call from libusb0.dll
; Variables
Global *usb_busses.usb_bus
Global *usb_device.usb_device
Global *usb_dev_handle.usb_dev_handle
#MY_VID = $0483
#MY_PID = $FFF0
;// Device configuration And Interface id.
#MY_CONFIG = 1
#MY_INTF = 0
;// Device endpoint(s)
#EP_IN = $81
#EP_OUT = $01
;/ Device of bytes To transfer.
#BUF_SIZE = 64
Procedure showBussesAndDevicesWithInformation()
Debug "Anzahl Busse: " + Str(usb_find_busses())
Debug "Anzahl Devices: " + Str(usb_find_devices())
*usb_busses = usb_get_busses()
While *usb_busses
Debug ""
Debug "Busname: " + PeekS(@*usb_busses\dirname)
*devices.usb_device = *usb_busses\devices
*usb_device = *usb_busses\devices
While *devices
Debug "Filename: " + PeekS(@*devices\filename)
Debug "devnum: " + Str(*devices\devnum)
Debug "dev: " + Str(*devices\dev)
Debug "num_children: " + Str(*devices\num_children)
Debug "children: " + Str(*devices\children)
If *devices\descriptor
Debug ""
Debug "Device Descriptor:"
Debug " bcdDevice: " + Hex(*devices\descriptor\bcdDevice)
Debug " bcdUSB: " + Hex(*devices\descriptor\bcdUSB)
Debug " bDescriptorType: " + Hex(*devices\descriptor\bDescriptorType)
Debug " bDeviceClass: " + Hex(*devices\descriptor\bDeviceClass)
Debug " bDeviceProtocol: " + Hex(*devices\descriptor\bDeviceProtocol)
Debug " bDeviceSubClass: " + Hex(*devices\descriptor\bDeviceSubClass)
Debug " bLength: " + Hex(*devices\descriptor\bLength)
Debug " bMaxPacketSize0: " + Hex(*devices\descriptor\bMaxPacketSize0)
Debug " bNumConfigurations: " + Hex(*devices\descriptor\bNumConfigurations)
Debug " idProduct: " + RSet(Hex(*devices\descriptor\idProduct), 4, "0")
Debug " idVendor: " + RSet(Hex(*devices\descriptor\idVendor), 4, "0")
Debug " iManufacturer: " + Hex(*devices\descriptor\iManufacturer)
Debug " iProduct: " + Hex(*devices\descriptor\iProduct)
Debug " iSerialNumber: " + Hex(*devices\descriptor\iSerialNumber)
EndIf
If *devices\config
Debug ""
Debug "Device Config:"
Debug " bConfigurationValue: " + Hex(*devices\config\bConfigurationValue)
Debug " bDescriptorType: " + Hex(*devices\config\bDescriptorType)
Debug " bLength: " + Hex(*devices\config\bLength)
Debug " bmAttributes: " + Hex(*devices\config\bmAttributes)
Debug " bNumInterfaces: " + Hex(*devices\config\bNumInterfaces)
Debug " extra: " + Hex(*devices\config\extra)
Debug " extralen: " + Hex(*devices\config\extralen)
Debug " iConfiguration: " + Hex(*devices\config\iConfiguration)
Debug " MaxPower: " + Hex(*devices\config\MaxPower)
Debug " wTotalLength: " + Hex(*devices\config\wTotalLength)
If *devices\config\Interface
Debug ""
Debug " Interface:"
Debug " num_altsetting: " + Hex(*devices\config\Interface\num_altsetting)
If *devices\config\Interface\altsetting
Debug ""
Debug " altsetting:"
Debug " bAlternateSetting: " + Hex(*devices\config\Interface\altsetting\bAlternateSetting)
Debug " bDescriptorType: " + Hex(*devices\config\Interface\altsetting\bDescriptorType)
Debug " bInterfaceClass: " + Hex(*devices\config\Interface\altsetting\bInterfaceClass)
Debug " bInterfaceNumber: " + Hex(*devices\config\Interface\altsetting\bInterfaceNumber)
Debug " bInterfaceProtocol: " + Hex(*devices\config\Interface\altsetting\bInterfaceProtocol)
Debug " bInterfaceSubClass: " + Hex(*devices\config\Interface\altsetting\bInterfaceSubClass)
Debug " bLength: " + Hex(*devices\config\Interface\altsetting\bLength)
Debug " extra: " + Hex(*devices\config\Interface\altsetting\extra)
Debug " extralen: " + Hex(*devices\config\Interface\altsetting\extralen)
Debug " iInterface: " + Hex(*devices\config\Interface\altsetting\iInterface)
Debug " bNumEndpoints: " + Hex(*devices\config\Interface\altsetting\bNumEndpoints)
If *devices\config\Interface\altsetting\endpoint
Debug ""
Debug " endpoint: "
Debug " bDescriptorType: " + Hex(*devices\config\Interface\altsetting\endpoint\bDescriptorType)
Debug " bEndpointAddress: " + Hex(*devices\config\Interface\altsetting\endpoint\bEndpointAddress)
Debug " bInterval: " + Hex(*devices\config\Interface\altsetting\endpoint\bInterval)
Debug " bLength: " + Hex(*devices\config\Interface\altsetting\endpoint\bLength)
Debug " bmAttributes: " + Hex(*devices\config\Interface\altsetting\endpoint\bmAttributes)
Debug " bRefresh: " + Hex(*devices\config\Interface\altsetting\endpoint\bRefresh)
Debug " bSynchAddress: " + Hex(*devices\config\Interface\altsetting\endpoint\bSynchAddress)
Debug " extra: " + Hex(*devices\config\Interface\altsetting\endpoint\extra)
Debug " extralen: " + Hex(*devices\config\Interface\altsetting\endpoint\extralen)
Debug " wMaxPacketSize: " + Hex(*devices\config\Interface\altsetting\endpoint\wMaxPacketSize)
EndIf
EndIf
EndIf
EndIf
*devices = *devices\next
Wend
*usb_busses = *usb_busses\Next
Wend
EndProcedure
Procedure OpenAndCloseDevice2()
If *usb_device = 0
ProcedureReturn 0
EndIf
*usb_dev_handle = 0
usb_find_busses()
usb_find_devices()
*usb_busses = usb_get_busses()
While *usb_busses
*devices.usb_device = *usb_busses\devices
*usb_device = *usb_busses\devices
While *devices
If *devices\descriptor\idVendor=#MY_VID And *devices\descriptor\idProduct=#MY_PID
Debug "Совпали VID\PID"
Debug ""
Debug "idProduct: " + RSet(Hex(*usb_device\descriptor\idProduct), 4, "0")
Debug "idVendor: " + RSet(Hex(*usb_device\descriptor\idVendor), 4, "0")
Debug ""+Str(@*usb_device)
Debug SizeOf(usb_device)
;UsbOppenn.Prot_UsbOppenn = GetFunction(libUsbID, "usb_open")
;*usb_dev_handle = UsbOppenn(*usb_device)
;ProcedureReturn 0
*usb_dev_handle = usb_open(*usb_device)
;usb_open(*usb_device)
Debug ""
Debug "Open and Close Device"
Debug *usb_dev_handle
Debug usb_close(*usb_dev_handle)
ProcedureReturn 0
EndIf
*devices = *devices\next
Wend
*usb_busses = *usb_busses\Next
Wend
EndProcedure
Procedure OpenAndCloseDevice()
If *usb_device = 0
ProcedureReturn 0
EndIf
Debug ""
Debug "idProduct: " + RSet(Hex(*usb_device\descriptor\idProduct), 4, "0")
Debug "idVendor: " + RSet(Hex(*usb_device\descriptor\idVendor), 4, "0")
Debug ""
*usb_dev_handle = usb_open(*usb_device)
;usb_open(*usb_device)
Debug ""
Debug "Open and Close Device"
Debug *usb_dev_handle
Debug usb_close(*usb_dev_handle)
EndProcedure
Procedure libUSB0()
If usb_init()
showBussesAndDevicesWithInformation()
*usb_version.usb_version = usb_get_version()
Debug ""
Debug "DLL Version: " + Str(*usb_version\dll\major) + "." + Str(*usb_version\dll\minor) + "." + Str(*usb_version\dll\micro) + "." + Str(*usb_version\dll\nano)
Debug "Driver Version: " + Str(*usb_version\driver\major) + "." + Str(*usb_version\driver\minor) + "." + Str(*usb_version\driver\micro) + "." + Str(*usb_version\driver\nano)
OpenAndCloseDevice2()
;this method won't work, but why?
;OpenAndCloseDevice()
CloseLibrary(libUsbID)
EndIf
EndProcedure
libUSB0()Отредактировано Alexsvc (24.01.2013 15:02:53)
)