PDA

View Full Version : اتصال به پورت usb



vahid_d_0101
شنبه 23 مرداد 1389, 14:29 عصر
با سلام
من با برنامه میکرو بیسیک برای میکرو کنترلر pic یه برنامه نوشتم که یه اعدادی را به پورت usb میفرسته حالا میخوام ببینم چه طوری باید داخل کامپیوتر اون اعداد را ببینم و دریافت کنم
نوع ارتباط هم HID هست که یه برنامه داخل همین فروم بوده پیدا کردم ولی کار نکرد
اینم ماژول ارتباط با یو اس پس برای میکرو بیسیک هست اگه نیاز هست خود سورس برنامه ای که با میکرو بیسیک هم نوشتم بگزارم

module USBdsc
include "HIDconstant"
' File Version 1.01
'
'************************************************* *****************************




'************************************************* *****************************
' The number of bytes in each report,
' calculated from Report Size and Report Count in the report descriptor
'************************************************* *****************************
const HID_INPUT_REPORT_BYTES = 1
const HID_OUTPUT_REPORT_BYTES = 1

const HID_FEATURE_REPORT_BYTES = 2
'************************************************* *****************************
' Byte constants
'************************************************* *****************************
const NUM_ENDPOINTS = 2
const ConfigDescr_wTotalLength = USB_CONFIG_DESCRIPTOR_LEN + USB_INTERF_DESCRIPTOR_LEN + USB_HID_DESCRIPTOR_LEN + (NUM_ENDPOINTS * USB_ENDP_DESCRIPTOR_LEN)
const HID_ReportDesc_len = 47

const Low_HID_ReportDesc_len = HID_ReportDesc_len
const High_HID_ReportDesc_len = HID_ReportDesc_len >> 8

const Low_HID_PACKET_SIZE = HID_PACKET_SIZE
const High_HID_PACKET_SIZE = HID_PACKET_SIZE >> 8



'************************************************* *****************************
' Descriptor Tables
'************************************************* *****************************
const DescTables as byte[USB_DEVICE_DESCRIPTOR_ALL_LEN*2] = (

' Device Descriptor
USB_DEVICE_DESCRIPTOR_LEN, 0, ' bLength - Length of Device descriptor (always 0x12)
USB_DEVICE_DESCRIPTOR_TYPE, 0, ' bDescriptorType - 1 = DEVICE descriptor
0x00, 0, ' bcdUSB - USB revision 2.00 (low byte)
0x02, 0, ' (high byte)
0x00, 0, ' bDeviceClass - Zero means each interface operates independently (class code in the interface descriptor)
0x00, 0, ' bDeviceSubClass
0x00, 0, ' bDeviceProtocol
EP0_PACKET_SIZE, 0, ' bMaxPacketSize0 - maximum size of a data packet for a control transfer over EP0
0x34, 0, ' idVendor - Vendor ID (low byte)
0x12, 0, ' (high byte)
0x01, 0, ' idProduct - Product ID (low byte)
0x00, 0, ' (high byte)
0x01, 0, ' bcdDevice - ( low byte)
0x00, 0, ' (high byte)
0x01, 0, ' iManufacturer - String1
0x02, 0, ' iProduct - String2
0x00, 0, ' iSerialNumber - ( None )
0x01, 0, ' bNumConfigurations - 1

' Configuration Descriptor
USB_CONFIG_DESCRIPTOR_LEN, 0, ' bLength - Length of Configuration descriptor (always 0x09)
USB_CONFIG_DESCRIPTOR_TYPE, 0, ' bDescriptorType - 2 = CONFIGURATION descriptor
ConfigDescr_wTotalLength, 0, ' wTotalLength - Total length of this config. descriptor plus the interface and endpoint descriptors that are part of the configuration.
0x00, 0, ' ( high byte)
0x01, 0, ' bNumInterfaces - Number of interfaces
0x01, 0, ' bConfigurationValue - Configuration Value
0x00, 0, ' iConfiguration - String Index for this configuration ( None )
0xA0, 0, ' bmAttributes - attributes - "Bus powered" and "Remote wakeup"
50, 0, ' MaxPower - bus-powered draws 50*2 mA from the bus.

' Interface Descriptor
USB_INTERF_DESCRIPTOR_LEN, 0, ' bLength - Length of Interface descriptor (always 0x09)
USB_INTERFACE_DESCRIPTOR_TYPE, 0, ' bDescriptorType - 4 = INTERFACE descriptor
0x00, 0, ' bInterfaceNumber - Number of interface, 0 based array
0x00, 0, ' bAlternateSetting - Alternate setting
NUM_ENDPOINTS, 0, ' bNumEndPoints - Number of endpoints used in this interface
0x03, 0, ' bInterfaceClass - assigned by the USB
0x00, 0, ' bInterfaceSubClass - Not A boot device
0x00, 0, ' bInterfaceProtocol - none
0x00, 0, ' iInterface - Index to string descriptor that describes this interface ( None )

' HID Descriptor
USB_HID_DESCRIPTOR_LEN, 0, ' bLength - Length of HID descriptor (always 0x09)
USB_HID_DESCRIPTOR_TYPE, 0, ' bDescriptorType - 0x21 = HID descriptor
0x01, 0, ' HID class release number (1.01)
0x01, 0,
0x00, 0, ' Localized country code (none)
0x01, 0, ' # of HID class descriptor to follow (1)
0x22, 0, ' Report descriptor type (HID)
Low_HID_ReportDesc_len, 0,
High_HID_ReportDesc_len, 0,

' EP1_RX Descriptor
USB_ENDP_DESCRIPTOR_LEN, 0, ' bLength - length of descriptor (always 0x07)
USB_ENDPOINT_DESCRIPTOR_TYPE, 0, ' bDescriptorType - 5 = ENDPOINT descriptor
0x81, 0, ' bEndpointAddress - In, EP1
USB_ENDPOINT_TYPE_INTERRUPT, 0, ' bmAttributes - Endpoint Type - Interrupt
Low_HID_PACKET_SIZE, 0, ' wMaxPacketSize - max packet size - low order byte
High_HID_PACKET_SIZE, 0, ' - max packet size - high order byte
1, 0, ' bInterval - polling interval (1 ms)

' EP1_TX Descriptor
USB_ENDP_DESCRIPTOR_LEN, 0, ' bLength - length of descriptor (always 0x07)
USB_ENDPOINT_DESCRIPTOR_TYPE, 0, ' bDescriptorType - 5 = ENDPOINT descriptor
0x01, 0, ' bEndpointAddress - Out, EP1
USB_ENDPOINT_TYPE_INTERRUPT, 0, ' bmAttributes - Endpoint Type - Interrupt
Low_HID_PACKET_SIZE, 0, ' wMaxPacketSize - max packet size - low order byte
High_HID_PACKET_SIZE, 0, ' - max packet size - high order byte
1, 0, ' bInterval - polling interval (1 ms)

' HID_Report Descriptor
0x06, 0, ' USAGE_PAGE (Vendor Defined)
0xA0, 0,
0xFF, 0,
0x09, 0, ' USAGE ID (Vendor Usage 1)
0x01, 0,
0xA1, 0, ' COLLECTION (Application)
0x01, 0,
' The Input report
0x09, 0, ' USAGE ID - Vendor defined
0x03, 0,
0x15, 0, ' LOGICAL_MINIMUM (0)
0x00, 0,
0x26, 0, ' LOGICAL_MAXIMUM (255)
0x00, 0,
0xFF, 0,
0x75, 0, ' REPORT_SIZE (8)
0x08, 0,
0x95, 0, ' REPORT_COUNT (2)
HID_INPUT_REPORT_BYTES, 0,
0x81, 0, ' INPUT (Data,Var,Abs)
0x02, 0,
' The Output report
0x09, 0, ' USAGE ID - Vendor defined
0x04, 0,
0x15, 0, ' LOGICAL_MINIMUM (0)
0x00, 0,
0x26, 0, ' LOGICAL_MAXIMUM (255)
0x00, 0,
0xFF, 0,
0x75, 0, ' REPORT_SIZE (8)
0x08, 0,
0x95, 0, ' REPORT_COUNT (2)
HID_OUTPUT_REPORT_BYTES, 0,
0x91, 0, ' OUTPUT (Data,Var,Abs)
0x02, 0,
' The Feature report
0x09, 0, ' USAGE ID - Vendor defined
0x05, 0,
0x15, 0, ' LOGICAL_MINIMUM (0)
0x00, 0,
0x26, 0, ' LOGICAL_MAXIMUM (255)
0x00, 0,
0xFF, 0,
0x75, 0, ' REPORT_SIZE (8)
0x08, 0,
0x95, 0, ' REPORT_COUNT (2)
HID_FEATURE_REPORT_BYTES, 0,
0xB1, 0, ' FEATURE (Data,Var,Abs)
0x02, 0,
' End Collection
0xC0, 0 ' END_COLLECTION
)
'************************************************* *****************************
const LangIDDescr as byte[8] = (
0x04, 0,
USB_STRING_DESCRIPTOR_TYPE, 0,
0x09, 0, ' LangID (0x0409) - Low
0x04, 0 ' - High
)
'************************************************* *****************************
const ManufacturerDescr as byte[48] = (
24, 0,
USB_STRING_DESCRIPTOR_TYPE, 0,
"V", 0, 0, 0,
"e", 0, 0, 0,
"n", 0, 0, 0,
"d", 0, 0, 0,
"o", 0, 0, 0,
"r", 0, 0, 0,
"-", 0, 0, 0,
"N", 0, 0, 0,
"a", 0, 0, 0,
"m", 0, 0, 0,
"e", 0, 0, 0
)
'************************************************* *****************************
const ProductDescr as byte[48] = (
24, 0,
USB_STRING_DESCRIPTOR_TYPE, 0,
"æ", 0, 0, 0,
"Í", 0, 0, 0,
"í", 0, 0, 0,
"Ï", 0, 0, 0,
" ", 0, 0, 0,
"Ï", 0, 0, 0,
"å", 0, 0, 0,
"Þ", 0, 0, 0,
"Ç", 0, 0, 0,
"ä", 0, 0, 0,
"í", 0, 0, 0
)
'************************************************* *****************************
const StrUnknownDescr as byte[4] = (
2, 0,
USB_STRING_DESCRIPTOR_TYPE, 0
)
'************************************************* *****************************







'************************************************* *****************************
' Initialization Function
'************************************************* *****************************
implements
sub procedure InitUSBdsc
dim dummy as byte volatile register

'************************************************* *****************************
' Byte constants
'************************************************* *****************************
dummy = NUM_ENDPOINTS
dummy = ConfigDescr_wTotalLength
dummy = HID_ReportDesc_len
dummy = Low_HID_ReportDesc_len
dummy = High_HID_ReportDesc_len
dummy = Low_HID_PACKET_SIZE
dummy = High_HID_PACKET_SIZE
dummy = HID_INPUT_REPORT_BYTES
dummy = HID_OUTPUT_REPORT_BYTES
dummy = HID_FEATURE_REPORT_BYTES
dummy = @DescTables
dummy = @LangIDDescr
dummy = @ManufacturerDescr
dummy = @LangIDDescr
dummy = @ProductDescr
dummy = @StrUnknownDescr
end sub
end.

vahid_d_0101
دوشنبه 25 مرداد 1389, 16:41 عصر
یعنی هیچکی تا حالا با پورت یو اس پی کار نکرده