Summary of the invention
The purpose of the embodiment of the invention is to provide a kind of recognition methods of USB device, be intended to solve the read-write that USB device of the prior art needs install driver or could realize usb data by signature authentication, complex operation, and also need to write corresponding driving program, the problem of consumption wealth consuming time during by signature authentication.
The embodiment of the invention is achieved in that a kind of recognition methods of USB device, and described method comprises the steps:
When having detected the USB device insertion, read the information of the USB device of insertion;
The information of the described USB device that reads and the protocol class drive information that Windows operating system carries are compared, described USB device is discerned;
When the protocol class drive information that carries when the information and the Windows operating system of the described USB device that reads was complementary, the USB device of then judging described insertion was the equipment of plug and play, to realize the read-write of usb data.
Another purpose of the embodiment of the invention is to provide a kind of recognition system of USB device, and described system comprises:
Information reading module is used for reading the information of the USB device of insertion when having detected the USB device insertion;
The identification comparing module is used for the information of described USB device that described information reading module is read and the protocol class drive information that Windows operating system carries and compares, and described USB device is discerned; And
Determination module, when being used for the information of the described USB device that reads when described information reading module and protocol class drive information that Windows operating system carries and being complementary, the USB device of then judging described insertion is the equipment of plug and play, to realize the read-write of usb data.
Another purpose of the embodiment of the invention is to provide a kind of transmission implementation method of usb data, and described method comprises the steps:
When the USB device that recognizes insertion is the equipment of plug and play, calls first function of Windows operating system and open the successful USB device of described identification;
The read write command program of user-defined usb data is written to buffer zone;
Call second functions control of Windows operating system usb data is carried out read-write operation.
Another purpose of the embodiment of the invention is to provide a kind of transmission of usb data to realize system, and described system comprises:
First calling module is used for when the USB device that recognizes insertion is the equipment of plug and play, calls first function of Windows operating system and opens the successful USB device of described identification;
Writing module is used for the read write command program of user-defined usb data is written to buffer zone; And
Second calling module, second functions control that is used to call Windows operating system is carried out read-write operation to usb data.
In embodiments of the present invention, when having detected the USB device insertion, read the information of the USB device of insertion; The information of the described USB device that reads and the protocol class drive information that Windows operating system carries are compared, described USB device is discerned; When the protocol class drive information that carries when the information and the Windows operating system of the described USB device that reads is complementary, the USB device of then judging described insertion is the equipment of plug and play, to realize the read-write of usb data, thereby the exploitation that not need fully to realize carrying out driver can realize being user-friendly to the transmission of arbitrary data between USB device and the PC.
Embodiment
In order to make purpose of the present invention, technical scheme and advantage clearer,, the present invention is further elaborated below in conjunction with drawings and Examples.Should be appreciated that specific embodiment described herein only in order to explanation the present invention, and be not used in qualification the present invention.
In embodiments of the present invention, when having detected the USB device insertion, read the information of the USB device of insertion; The information of the described USB device that reads and the protocol class drive information that Windows operating system carries are compared, described USB device is discerned; When the protocol class drive information that carries when the information and the Windows operating system of the described USB device that reads was complementary, the USB device of then judging described insertion was the equipment of plug and play, to realize the read-write of usb data.
Fig. 1 shows the realization flow of the recognition methods of the USB device that the embodiment of the invention provides, and its concrete step is as described below:
In step S101, when having detected the USB device insertion, read the information of the USB device of insertion.
In embodiments of the present invention, in the information of USB device, the protocol class driver that Windows operating system is carried is written to the information of described USB device in advance in advance, makes USB device have the function of protocol class.Wherein, the protocol class driver that this Windows operating system carries can be a MassStorage Class protocol class program, can certainly be other protocol class drivers that Windows operating system carries, USB Video Class etc. for example, do not repeat them here, following is that embodiment is described with Mass Storage Class protocol class program also.
In embodiments of the present invention, above-mentioned USB device can be the equipment that possesses USB interface, for example USB interface-based is real-time data acquisition industrial system equipment, USB interface-based electronic handwriting pen product, USB interface-based game machine telepilot, USB interface-based recording system equipment and USB interface-based data analysis instrument etc., do not repeat them here, followingly describe yet as embodiment.
In step S102, the information of the USB device that reads and the protocol class drive information that Windows operating system carries are compared, described USB device is discerned.
In embodiments of the present invention, this comparison process is promptly to the identifying of the USB device inserted, judge whether this USB device is the equipment that has write Mass Storage Class protocol class program, judge promptly whether the USB device of this insertion is Mass Storage Class protocol class equipment.
In step S103, judge whether success of the identification of USB device, be execution in step S104 then, otherwise execution in step S105.
In step S104, when the protocol class drive information that carries when the information and the Windows operating system of the described USB device that reads was complementary, the USB device of then judging described insertion was the equipment of plug and play, to realize the read-write of usb data.
In embodiments of the present invention, when the protocol class drive information that carries when the information and the Windows operating system of the described USB device that reads is complementary, the USB device of then judging described insertion is the equipment of plug and play, do not need to point out the USB driver of any this equipment of user installation, can use application program to realize the read-write of usb data.
In step S105, when the protocol class drive information that carries when the information and the Windows operating system of the described USB device that reads does not match, prompting user installation driver.
In embodiments of the present invention, after the protocol class driver that in advance Windows operating system is carried is written to the information of described USB device, need in the protocol class driver of USB device and Windows operating system application program, write the read write command program of user-defined usb data respectively.
In embodiments of the present invention, said method embodiment be Windows operating system implement on the basis of integrated USBMass Storage Class class host-host protocol, still comprised multiple sub-protocol in the Mass Storage Class protocol class, in this embodiment, adopt the transmission channel of Bulk Only host-host protocol as usb data, the communication of Bulk Only host-host protocol is with command block packet (Command BlockWrapper, CBW), it has 31 bytes, and its detailed form is as follows:
In the last table, the data of the 0th byte to the 14 bytes are to express the data of this CBW package informatin, and wherein the value of dCBWSignature section is fixed as 43425355h, represents that this packet is a CBW packet;
The dCBWTag section is the label section of this bag, the PC main frame send comprise this label section CBW to USB device, equally also require USB device to return same label data section and give the PC main frame, if label data is not corresponding then make mistakes;
The length of the dCBWDataTransferLength section data that to be the PC main frame send or receive to equipment to USB device, this data length be by the CBW order determine that it will transmit by BULK OUT end points or BULKIN end points;
The bmCBWFlags section is the message segment that comprises data transfer direction, the 7th direction that has determined data transmission of bmCBWFlags, if 0, the direction of representative data is to USB device from the PC main frame, if 1, then the representative data direction is to the PC main frame from USB device;
The bCBWLUN section refers to the Logic Unit Number of USB device, and USB device can be supported a plurality of logical blocks, and therefore, it is the order of carrying out at which logical block that the PC main frame must be specified this CBW order;
For the data in the above CBW information field, do not need to carry out any modification, keep the expressed meaning of former CBW;
From the 15th byte to the 30 bytes is exactly CBWCB (CBW Command Block) command field, and this field is that DEVICE needs real recognition command and exectorial field.
Following table has provided the structure of the CBWCB of a standard commands FORMAT UNIT:
Can see by last table, the 0th byte is Operation Code field, it is the SCSI-2 order, the byte of back all is the parameter of this order, its form is by each specific instructions decision, the Operation Code value that for example goes up the described FORMAT UNIT order of table is 04h, and the byte of back all is the parameter of FORMATUNIT.
All USB flash disks all meet Mass Storage Class quasi-protocol, and the SCSI-2 order that USB flash disk is mainly used is as shown in the table:
Command Description (order implication) |
Command Value (bid value) |
Format?Unit |
04h |
Inquiry |
12h |
Mode?Select |
55h |
Mode?Sense |
5Ah |
Prevent-Allow?Medium?Removal |
1Eh |
Read(10) |
28h |
Read(12) |
A8h |
Read?Capacity |
25h |
Read?Format?Capacities |
23h |
Request?Sense |
03h |
Rezero |
01h |
Seek(10) |
2Bh |
Send?Diagnostic |
1Dh |
Start-Stop?Unit |
1Bh |
Test?Unit?Ready |
00h |
Verify |
2Fh |
Write(10) |
2Ah |
Write(12) |
AAH |
Write?and?Verify |
2Eh |
The standard commands that described these orders of last table are USB flash disk work, can see that these orders take the space of a byte, therefore, in this byte, can hold from 0 to 255 totally 256 orders, therefore, except the order of these standards, add some self-defining orders and use, also in USB device information, add the program of identification custom command simultaneously, self-defining order is carried out for USB device.
In this embodiment, Operation Code field use this value of 0xf1 as equipment exclusive order, certainly, Operation Code field can be selected to be worth arbitrarily, order all is not fine as long as this value is not SCSI-2, and following table is our self-defining CBWCB structure:
Because Operation Code is 0xF1, this value is not the value of the SCSI-2 order of any standard, therefore this order can never be discerned by USB flash disk, and this orders to determine whether it is to belong to its exclusive order and the information of my USB device relies on judgement just.
After USB device identified and is its exclusive order, the content of beginning interpreted command, CustomizeCommand field were to set the command function that needs are realized according to requirement of client, for example upload, download, delete, dispose or the like.Address field, Value field and State Symbol field all are CustomizeCommand custom feature order parameters needed.
The above-mentioned concrete implementation detail that only provides for the embodiment of the invention, this not in order to the restriction the present invention.
Fig. 2 shows the structured flowchart of the recognition system of the USB device that the embodiment of the invention provides, and for convenience of explanation, has only provided the part relevant with the embodiment of the invention among the figure.
Information reading module 11 reads the information of the USB device of insertion when having detected the USB device insertion; The information of the described USB device that identification comparing module 12 reads described information reading module 11 and the protocol class drive information that Windows operating system carries compare, and described USB device is discerned; When the information of the described USB device that determination module 13 reads when described information reading module 11 and the protocol class drive information that Windows operating system carries are complementary, the USB device of then judging described insertion is the equipment of plug and play, to realize the read-write of usb data.
In embodiments of the present invention, the protocol class driver that in advance Windows operating system carried of first writing module 14 is written to the information of described USB device; Second writing module 15 writes the read write command program of user-defined usb data respectively in the protocol class driver of USB device and Windows operating system application program; Wherein, described protocol class driver is a Mass Storage Class protocol class program.
In embodiments of the present invention, when the information of the described USB device that reads when described information reading module 11 and the protocol class drive information that Windows operating system carries do not match, reminding module 16 prompting user installation drivers.
Above-mentioned only is a system embodiment of the present invention, and wherein, the function of each module of this system does not repeat them here as described in the above-mentioned method embodiment, but not in order to restriction the present invention.
Fig. 3 shows the realization flow of the transmission implementation method of the usb data that the embodiment of the invention provides, and its concrete step is as described below:
In step S301, when the USB device that recognizes insertion is the equipment of plug and play, calls first function of Windows operating system and open the successful USB device of described identification, wherein, this first function can be the windows api function.
In the embodiment of the invention, on the basis of the technical scheme that the first method embodiment provides, when USB device is discerned successfully, need call the related function of Windows and open this USB device, so that carry out the operations such as read-write of usb data, certainly, this usb data also comprises custom data.
Wherein, can adopt CreateFile to open the equipment that PC just now successfully recognizes, form is as follows:
HANDLE?CreateFile(
LPCTSTR lpFileName, the pointer of // sensing filename
DWORD dwDesiredAccess, // access module (Writing/Reading)
DWORD dwShareMode, // shared model
LPSECURITY_ATTRIBUTES lpSecurity Attributes, // point to
The pointer of // security attribute
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attribute
HANDLE hTemplateFile // be used for xcopy handle
);
Wherein, because equipment is Mass Storage Class kind equipment, so lpFileName directly selects drive to get final product.
Above-mentioned only is one embodiment of the present of invention, this not in order to the restriction the present invention.
In step S302, the read write command program of user-defined usb data is written to the buffer zone of second function, wherein, second function can be the read-write api function.
In step S303, call second functions control of Windows operating system usb data is carried out read-write operation.
In embodiments of the present invention, after equipment was successfully opened, we can use the DeviceIoControl function to send self-defining CBW order to equipment, and the concrete form of DeviceIoControl is as follows:
BOOL?DeviceIoControl(
HANDLE hDevice, // equipment handle
DWORD dwIoControlCode, // control code
LPVOID lpInBuffer, // input data buffer pointer
DWORD nInBufferSize, // input data buffer length
LPVOID lpOutBuffer, // data output buffer district pointer
DWORD nOutBufferSize, // data output buffer section length
LPDWORD lpBytesReturned, // output data physical length element length
LPOVERLAPPED lpOverlapped // overlapping processing structured fingers
);
This shows, can just can realize the transmission of USB self-defining data by Windows api function DeviceIoControl.
The transmission that Fig. 4 shows the usb data that the embodiment of the invention provides realizes the structured flowchart of system, for convenience of explanation, has only provided the part relevant with the embodiment of the invention among the figure.
First calling module 21 calls first function of Windows operating system and opens the successful USB device of described identification when the USB device that recognizes insertion is the equipment of plug and play; Writing module 22 is written to buffer zone with the read write command program of user-defined usb data; Second calling module 23 calls second functions control of Windows operating system usb data is carried out read-write operation.
Above-mentioned only is a system embodiment of the present invention, and method embodiment as shown in Figure 3 is described for the function of its each module, this not in order to the restriction the present invention.
In embodiments of the present invention, when having detected the USB device insertion, read the information of the USB device of insertion; The information of the described USB device that reads and the protocol class drive information that Windows operating system carries are compared, described USB device is discerned; When the protocol class drive information that carries when the information and the Windows operating system of the described USB device that reads is complementary, the USB device of then judging described insertion is the equipment of plug and play, do not need to point out the USB driver that any this equipment is installed, can use application program to realize the read-write of usb data, thereby the exploitation that not need fully to realize carrying out driver can realize being user-friendly to the transmission of arbitrary data between USB device and the PC.
The above only is preferred embodiment of the present invention, not in order to restriction the present invention, all any modifications of being done within the spirit and principles in the present invention, is equal to and replaces and improvement etc., all should be included within protection scope of the present invention.