WO2021093734A1 - 一种usb通讯的实现方法及装置 - Google Patents

一种usb通讯的实现方法及装置 Download PDF

Info

Publication number
WO2021093734A1
WO2021093734A1 PCT/CN2020/127870 CN2020127870W WO2021093734A1 WO 2021093734 A1 WO2021093734 A1 WO 2021093734A1 CN 2020127870 W CN2020127870 W CN 2020127870W WO 2021093734 A1 WO2021093734 A1 WO 2021093734A1
Authority
WO
WIPO (PCT)
Prior art keywords
report
command
data
length
descriptor
Prior art date
Application number
PCT/CN2020/127870
Other languages
English (en)
French (fr)
Inventor
陆舟
于华章
Original Assignee
飞天诚信科技股份有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 飞天诚信科技股份有限公司 filed Critical 飞天诚信科技股份有限公司
Publication of WO2021093734A1 publication Critical patent/WO2021093734A1/zh
Priority to US17/478,829 priority Critical patent/US11663155B2/en

Links

Images

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L1/00Arrangements for detecting or preventing errors in the information received
    • H04L1/0001Systems modifying transmission characteristics according to link quality, e.g. power backoff
    • H04L1/0006Systems modifying transmission characteristics according to link quality, e.g. power backoff by adapting the transmission format
    • H04L1/0007Systems modifying transmission characteristics according to link quality, e.g. power backoff by adapting the transmission format by modifying the frame length
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F13/00Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F13/38Information transfer, e.g. on bus
    • G06F13/42Bus transfer protocol, e.g. handshake; Synchronisation
    • G06F13/4282Bus transfer protocol, e.g. handshake; Synchronisation on a serial bus, e.g. I2C bus, SPI bus
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/14Session management
    • H04L67/146Markers for unambiguous identification of a particular session, e.g. session cookie or URL-encoding
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2213/00Indexing scheme relating to interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F2213/0042Universal serial bus [USB]

Definitions

  • the invention relates to the field of electronic products, in particular to a method and device for implementing USB communication.
  • USB HID devices are devices for human-computer interaction, and are a relatively large category, used to control certain aspects of computer operations, such as USB mouse, USB keyboard, USB game joystick, USB touchpad, USB trackball, and telephone Dial-up equipment, VCR remote control and other equipment.
  • the communication between USB HID devices is implemented through the HID (English full name: Human Interface Device, Chinese full name: Human Interface Device) protocol.
  • HID communication protocol has a fixed data packet size for one interaction, and when the length of the transmitted data is greater than the fixed data packet size, multiple interactions are required to complete the data transmission by sub-packaging, and the communication speed is slow.
  • the purpose of the present invention is to overcome the shortcomings of the prior art and provide a method and device for implementing USB communication.
  • the present invention provides a method for implementing USB communication, including:
  • Step S1 When the device receives the command sent by the host computer, it judges the type of the command, if it is an enumeration command, execute step S2, if it is a setting report command, execute step S3, if it is to obtain a report command, execute step S4;
  • Step S2 The device returns the overall information of the enumerated device to the host computer, and returns to step S1;
  • the overall device information includes a report descriptor, and the report descriptor includes a plurality of features, and each feature includes a report identifier and a corresponding The length of the report;
  • Step S3 The device obtains and saves response data according to the setting report command, and returns to step S1;
  • Step S4 The device selects a corresponding report length according to the length of the current response data, obtains a second report identifier corresponding to the report length, generates and sends return data according to the acquired second report identifier and the current response data To the host computer, return to step S1.
  • step S2 includes:
  • the device When the device receives the status acquisition request sent by the host computer, the device returns device status information to the host computer;
  • the device When the device receives the get device descriptor command sent by the host computer, it returns corresponding data according to the get data length in the get device descriptor command; the returned corresponding data is the maximum data supported by the current signal path Packet length response data or complete device descriptor;
  • the device When the device receives the address setting request sent by the upper computer, the device saves the new address in the address setting request and returns a confirmation response to the upper computer;
  • the device When the device receives the device configuration descriptor command sent by the host computer, the device sends complete configuration information to the host computer;
  • the device When the device receives the device string descriptor acquisition command sent by the host computer, the device sends the description character set to the host computer;
  • the device When the device receives the command to obtain the human-machine interface device descriptor sent by the upper computer, the device returns the human-machine interface device descriptor to the upper computer;
  • the device When the device receives a report descriptor command sent by the host computer, the device returns a report descriptor to the host computer; the report descriptor includes multiple characteristics, each of which includes a report identifier and a corresponding Report length
  • the device When the device receives the setting configuration command sent by the host computer, the device is configured.
  • the said device sending complete configuration information to the upper computer when the device receives the command to obtain the device configuration descriptor sent by the upper computer includes: every time the device receives the acquiring device configuration After the descriptor command, the corresponding configuration information is sent to the host computer.
  • step S4 includes: the device selects a report length that is greater than and closest to the length of the current response data, obtains a second feature including the report length in the report descriptor, and obtains the second feature
  • the second report identifier of the obtained second report identifier and the response data are sequentially spliced to generate return data and sent to the upper computer, and step S1 is returned.
  • step S1 includes: when the device receives the command sent by the host computer, it judges the type of the command according to the command header. If the command header is the third data, the type of the command is an enumerated command, and step S2 is executed, such as the command If the header is the first data, the type of the command is a set report command, and step S3 is executed. If the command header is the second data, the type of the command is a get report command, and step S4 is executed.
  • step S3 is specifically: the device searches for the corresponding report length according to the first report identifier in the set report command, and obtains data of the corresponding length from the set report command according to the found report length, Process the acquired data and generate response data, save the response data, and return to step S1.
  • the method further includes: the device obtains the first byte data of the data field in the setting report command to obtain the first report identifier.
  • the present invention also provides a device for realizing USB communication, including:
  • the receiving module is used to receive the commands sent by the upper computer
  • the first judgment module is used to judge the type of the command received by the receiving module, if it is an enumeration command, it triggers the first sending module, if it is a setting report command, it triggers the acquisition and saving module, and if it is a report command it triggers the selection Get the module;
  • the first sending module is configured to return the overall information of the enumerated device to the host computer to trigger the receiving module;
  • the overall device information includes a report descriptor, and the report descriptor includes multiple features, each feature Including the report identifier and the corresponding report length;
  • the acquiring and saving module is configured to acquire and save response data according to the setting report command, and trigger the receiving module;
  • the selection and acquisition module is configured to select a corresponding report length according to the length of the current response data, acquire a second report identifier corresponding to the report length, and generate return data based on the acquired second report identifier and the current response data ;
  • the second sending module is configured to send the return data generated by the selection and acquisition module to the upper computer to trigger the receiving module.
  • the first sending module includes:
  • the first returning unit is configured to return device status information to the host computer when the receiving module receives the status acquisition request sent by the host computer;
  • the second return unit is used to return corresponding data according to the length of the acquired data in the acquisition device descriptor command when the receiving module receives the device descriptor acquisition command sent by the host computer; the return corresponding The data is the response data of the maximum packet length supported by the current signal path or the complete device descriptor;
  • the third return unit is configured to save the new address in the address setting request and return a confirmation response to the upper computer when the receiving module receives the address setting request sent by the upper computer;
  • the fourth return unit is configured to send complete configuration information to the host computer when the receiving module receives the command to obtain the device configuration descriptor sent by the host computer;
  • the fifth return unit is configured to send the description character set to the host computer when the receiving module receives the device string descriptor acquisition command sent by the host computer;
  • the sixth return unit is configured to return the human-machine interface device descriptor to the upper computer when the receiving module receives the command to obtain the human-machine interface device descriptor sent by the upper computer;
  • the seventh return unit is configured to return a report descriptor to the host computer when the receiving module receives the command to obtain the report descriptor sent by the host computer;
  • the report descriptor includes multiple features, each feature Including the report identifier and the corresponding report length;
  • the setting unit is configured to configure the device when the receiving module receives the setting configuration command sent by the host computer.
  • the fourth returning unit is specifically configured to send the corresponding configuration information to the host computer every time the receiving module receives the command to obtain the device configuration descriptor.
  • the selection and acquisition module is specifically configured to select a report length that is greater than and closest to the report length according to the length of the current response data, acquire the second feature including the report length in the report descriptor, and acquire the second feature in the second feature
  • the second report identifier is used to sequentially splice the acquired second report identifier and the response data to generate return data.
  • the first judgment module is specifically configured to judge the type of the command according to the command header when the receiving module receives the command sent by the host computer. If the command header is the third data, the type of the command is an enumeration command, The first sending module is triggered. If the command header is the first data, the command type is a set report command, and the acquisition and saving module is triggered. If the command header is the second data, the command type is the acquisition report command, and the selection acquisition module is triggered.
  • the obtaining and saving module is specifically configured to search for the corresponding report length according to the first report identifier in the set report command, obtain data of the corresponding length from the set report command according to the searched report length, and process the obtaining Data is received and response data is generated, the response data is saved, and the receiving module is triggered.
  • the device further includes:
  • the acquiring module is configured to acquire the first byte data of the data field in the setting report command to obtain the first report identifier, and trigger the acquiring and saving module.
  • the present invention has the following advantages:
  • the technical scheme of the present invention is suitable for the data communication process between the host computer and the device.
  • the length of the interactive data between the host computer and the device is not fixed, that is, the report descriptor sets multiple characteristics.
  • the report lengths in the features are all different.
  • When communicating select the corresponding report length according to the needs, and send the corresponding report identifier to the other party. No matter how long the interactive data is, it can be sent at one time without subcontracting, which makes the data interaction more flexible. The communication speed is faster.
  • FIG. 1 is a flowchart of a method for implementing USB communication according to Embodiment 1 of the present invention
  • FIGS. 2 and 3 are flowcharts of a method for implementing USB communication according to the second embodiment of the present invention.
  • FIG. 4 is a block diagram of a device for implementing USB communication according to the third embodiment of the present invention.
  • the first embodiment of the present invention provides a method for implementing USB communication, as shown in FIG. 1, including:
  • Step S1 When the device receives the command sent by the host computer, it judges the type of the command, if it is an enumeration command, execute step S2, if it is a setting report command, execute step S3, if it is to obtain a report command, execute step S4;
  • step S1 includes: when the device receives the command sent by the host computer, it judges the type of the command according to the command header, if the command header is the third data, the type of the command is an enumerated command, and step S2 is executed, such as the command header If it is the first data, the type of the command is a set report command, and step S3 is executed. If the command header is the second data, the type of the command is a get report command, and step S4 is executed;
  • Step S2 The device returns the overall information of the enumerated device to the upper computer, and returns to step S1;
  • the overall device information includes a report descriptor, the report descriptor includes multiple features, and each feature includes a report identifier and a corresponding report length;
  • Step S3 The device obtains and saves the response data according to the setting report command, and returns to step S1;
  • step S3 is specifically: the device searches for the corresponding report length according to the first report identifier in the set report command, obtains data of the corresponding length from the set report command according to the searched report length, and processes Obtain the data and generate response data, save the response data, and return to step S1;
  • the method before step S3, the method further includes: the device obtains the first byte data of the data field in the setting report command to obtain the first report identifier;
  • Step S4 The device selects the corresponding report length according to the length of the current response data, obtains the second report identifier corresponding to the report length, generates return data according to the acquired second report identifier and the current response data and sends it to the host computer, and returns to step S1 ;
  • step S4 in this embodiment includes: the device selects a report length that is greater than and closest to the length of the current response data, obtains the second feature including the report length in the report descriptor, and obtains the second feature of the second feature.
  • the report identifier is to sequentially splice the acquired second report identifier and the current response data to generate return data and send it to the upper computer, and return to step S1.
  • step S2 includes:
  • the device When the device receives the status acquisition request sent by the host computer, the device returns the device status information to the host computer;
  • the device status information includes restart status information
  • the device When the device receives the get device descriptor command sent by the host computer, it returns the corresponding data according to the get data length in the get device descriptor command;
  • the corresponding data returned is the response data of the maximum data packet length supported by the current signal path or the complete device descriptor
  • the device When the device receives the address setting request sent by the host computer, the device saves the new address in the address setting request and returns a confirmation response to the host computer;
  • the device When the device receives the device configuration descriptor command sent by the host computer, the device sends the complete configuration information to the host computer;
  • the device when the device receives the command to obtain the device configuration descriptor sent by the host computer, the device sends the complete configuration information to the host computer, including: every time the device receives the command to obtain the device configuration descriptor, it will correspond to The configuration information is sent to the host computer;
  • the device When the device receives the device string descriptor command sent by the host computer, the device sends the description character set to the host computer;
  • the device When the device receives the command to obtain the human-machine interface device descriptor sent by the upper computer, the device returns the human-machine interface device descriptor to the upper computer;
  • the device When the device receives the Get Report Descriptor command sent by the upper computer, the device returns the report descriptor to the upper computer;
  • the report descriptor includes multiple characteristics, each of which includes the report identifier and the corresponding report length;
  • the second embodiment of the present invention provides a method for implementing USB communication.
  • the method in this embodiment includes an enumeration process and a data transmission process.
  • the enumeration process is shown in FIG. 2 and includes:
  • Step A1 The host computer monitors the signal voltage of each port through the hub. When the monitoring signal voltage rises, a device is inserted, and step A2 is executed;
  • each of the two signal lines of the hub port of the host computer has a 15k ⁇ pull-down resistor, and each device has a 1.5k ⁇ pull-up resistor at D+.
  • the pull-up resistor of the device increases the signal voltage, and the hub will detect that the device is plugged in;
  • Step A2 The host computer sends a Get_Status request to the device;
  • the hub transmits an interrupt transmission to the upper computer to report the event of device insertion, and the upper computer sends a Get_Status (get status) request to the device through the hub;
  • Step A3 The device returns the connection time between the device and the upper computer to the upper computer;
  • Step A4 The host computer sends a Set_Feature request to the hub, and the hub restarts the device port to establish a signal path between the device and the host computer;
  • the host computer sends a Set_Feature request to the hub, requesting the hub to restart the device port, so that the USB data line of the device is in the reset (RESET) state for at least 10ms, and then establish a connection between the device and the host computer Signal pathway;
  • RESET reset
  • Step A5 The host computer sends a Get_Status request to the device;
  • Step A6 The device returns information to the host computer that the device is still restarting
  • the device when the hub releases the restart state, the device is in the default state, and the device is ready to respond to control transmission through the default process of Endpoint 0 (Endpoint 0), that is, the device now uses the default address 0x00 to start with the host computer.
  • Step A7 The host computer detects the device type through the hub;
  • the host computer uses the hub to determine which signal line (D+ or D-) has a higher voltage when idle to detect whether the device is a low-speed device or a full-speed device.
  • Full-speed and high-speed devices D+ have pull-up resistors, and low-speed devices D- There is a pull-up resistor;
  • Step A8 The host computer sends a Get_Device_Descriptor (get device descriptor) command to the device with the default address;
  • Step A9 The device returns the response data including the maximum packet length supported by the current signal path to the host computer;
  • the host computer sends a Get_Device_Descriptor command to address 0 (corresponding to the default address 0x00) to obtain the maximum data packet length supported by the default control pipeline, and wait for the USB within a preset time.
  • Device's response when receiving the device's response, get the first 8 bytes of data to get the maximum data packet length;
  • Step A10 The host computer sends a Set_Address (set address) request containing the new address to the device;
  • Step A11 The device saves the new address in the Set_Address request and returns a confirmation response to the upper computer;
  • the upper computer assigns a unique address to the device by sending a Set_Address (set address) request to the device; the device reads the Set_Address (set address) request, returns a confirmation response to the upper computer, and saves it.
  • Set_Address set address
  • Step A12 The host computer resends the Get_Device_Descriptor command to the device at the new address;
  • Step A13 The device sends the device descriptor to the upper computer, and the upper computer obtains the overall information of the device;
  • the overall information of the device includes VID (vendor identification), PID (product identification), etc.
  • VID vendor identification
  • PID product identification
  • the values of bDeviceClass (device type), bDeviceSubClass (device subtype), and bDeviceProtocol (device protocol) in the returned device descriptor are all 0;
  • Step A14 The host computer sends a Get_Device_Configuration (get device configuration descriptor) command;
  • the host computer cyclically sends the Get_Device_Configuration (get device configuration descriptor) command to the device to read all the configuration information of the device; the value of bInterFaceClass (interface type) in the interface descriptor of the configuration information is 0x03, indicating that the device is HID (Human Machine Interface Equipment) equipment;
  • Step A15 The device sends the complete configuration information to the upper computer
  • Step A16 The host computer sends a Get_Device_String (get device string descriptor) command to the device;
  • Step A17 The device sends the description character set to the host computer
  • the description character set includes: manufacturer, product description, model and other information;
  • Step A18 The host computer sends a command to obtain the HID (Human Machine Interface Device) descriptor to the device;
  • HID Human Machine Interface Device
  • Step A19 The device returns the HID descriptor to the host computer
  • the HID (Human Interface Device) descriptor indicates that the report descriptor needs to be returned
  • Step A20 The host computer sends a command to obtain the report descriptor to the device;
  • Step A21 The device returns a report descriptor to the host computer
  • ReportID Report ID
  • Report size Report length
  • ReportSize Report length
  • the report descriptor in this embodiment is: the content of the HID (Human Interface Device) report descriptor is as follows:
  • the value of the report identifier is 0x01-0x32.
  • the report identifier is 0x01, the data length of one communication is 0x07, which is 7 bytes; the report identifier is 0x2E, and the data length of one communication is 0x080F whil;
  • Step A22 The upper computer displays the information of the discovered new device
  • the upper computer pop-up window displays the information of the discovered new device, including: manufacturer, product description, model, etc.;
  • Step A23 The host computer sends a Set_Configuration(x) command to the device to configure the device;
  • the host computer loads the USB device driver, it sends a Set_Configuration(x) command (x represents a configuration value other than 0) to the device to request configuration of the device. If the configuration is successful, The USB device enters the "configuration" state, and can transmit data with the client software;
  • the data transmission process as shown in Figure 3 includes:
  • Step B1 The host computer selects the report length according to the length of the data to be sent, and obtains the report identifier corresponding to the report length;
  • the length of the sent data is selected to be greater than the length and the closest report length
  • the length of the data to be sent in this embodiment is 512, and the content is specifically:
  • the format of the sent data is: 00 D6 00 00 00 02 00
  • Data to be updated; the total length of the sent data is: 512+7 519 bytes, and the report length selected according to the total length of the sent data should be greater than 519 and the smallest one ,
  • the length of the report found in this embodiment is 0x020F, that is, 527, and the corresponding report identifier is 0x26;
  • Step B2 The host computer splices the report identifier and the data to be sent to generate a data packet
  • the upper computer fills the report identifier before the data to be sent. If the data length after filling the report identifier is less than the report length corresponding to the report identifier, then fill data (00) at the end of the data to be sent to the report length.
  • the filled data is:
  • Step B3 The upper computer calls the SetFeature() interface to send a setting report command containing data packets to the device;
  • the setting report command sent by the device before calling the SetFeature interface in this embodiment is:
  • Step B4 After the host computer detects that the setting report command is sent, it generates a report command according to the report identifier corresponding to the maximum data length and sends it to the device, and waits for the response returned by the receiving device;
  • the command to obtain the report in this embodiment is: A1 01 32 03 00 00 FF 0F;
  • Step B5 When the device receives the command, it judges the type of the command, if it is to set the report command, execute step B6, if it is to obtain the report command, execute step B8;
  • the device determines the header of the command. If it is 21 09, the command type is a set report command, and if it is A1 01, the command type is a get report command;
  • Step B6 The device obtains the report identifier in the received command, searches for the report length corresponding to the report identifier according to the reported report descriptor, and obtains data of the corresponding length from the command according to the report length;
  • Step B7 The device processes the acquired data and generates response data, waits to receive the data, and returns to step B5;
  • Step B8 The device selects the corresponding report length in the report descriptor according to the length of the response data and obtains the corresponding report identifier, and organizes and generates return data according to the report identifier and the response data;
  • the device selects a value in the report descriptor that is greater than the length of the response data and the closest report length to it;
  • the response data in this embodiment is 00 02 90 00, that is, the length of the response data is 4 bytes, and the obtained report identifier is 0x01.
  • the returned data generated by the organization is: 01 00 02 90 00 00 00 00;
  • Step B9 The device returns the return data generated by the organization to the host computer
  • the returned data in this embodiment is: 01 00 02 90 00 00 00 00;
  • Step B10 The GetFeature() interface of the host computer receives the returned data, obtains the report identifier in the returned data, finds the report length corresponding to the report identifier in the report descriptor, and obtains the response data of the corresponding length from the returned data according to the report length. Respond to the data for processing.
  • the third embodiment of the present invention provides a device for implementing USB communication, as shown in FIG. 4, including:
  • the receiving module 401 is used to receive commands sent by the upper computer
  • the first judging module 402 is used to judge the type of the command received by the receiving module 401, if it is an enumeration command, it triggers the first sending module 403, if it is a setting report command, it triggers the acquisition and saving module 404, and if it is a report command, it triggers the acquisition and storage module 404. Trigger selection and acquisition module 405;
  • the first judgment module 402 is specifically configured to judge the type of the command according to the command header when the receiving module 401 receives the command sent by the host computer. If the command header is the third data, the command type is an enumeration command. , Trigger the first sending module 403, if the command header is the first data, the type of the command is the set report command, trigger the acquisition and save module 404, if the command header is the second data, the command type is the acquisition report command, trigger the selection acquisition module 405;
  • the first sending module 403 is used to return the overall information of the enumerated device to the upper computer to trigger the receiving module 401;
  • the overall device information includes report descriptors, which include multiple characteristics, each of which includes a report identifier and a corresponding report length ;
  • the obtaining and saving module 404 is configured to obtain and save the response data according to the setting report command, and trigger the receiving module 401;
  • the obtaining and saving module 404 is specifically configured to search for the corresponding report length according to the first report identifier in the set report command, obtain data of the corresponding length from the set report command according to the searched report length, and process the obtained data And generate response data, save the response data, and trigger the receiving module 401; optionally, the device of this embodiment further includes: an acquisition module for acquiring the first byte data of the data field in the setting report command to obtain the first report identifier, Trigger the acquiring and saving module 404;
  • the selection and acquisition module 405 is configured to select a corresponding report length according to the length of the current response data, acquire a second report identifier corresponding to the report length, and generate return data based on the acquired second report identifier and current response data;
  • the selection and acquisition module 405 is specifically configured to select the report length that is greater than and the closest according to the length of the current response data, acquire the second feature including the report length in the report descriptor, and acquire the second feature in the second feature.
  • the report identifier which sequentially splices the acquired second report identifier and response data to generate return data;
  • the second sending module 406 is configured to send the return data generated by the selection acquisition module 405 to the upper computer to trigger the receiving module 401.
  • the first sending module 403 includes:
  • the first return unit is used to return device status information to the host computer when the receiving module 401 receives the status acquisition request sent by the host computer;
  • the second return unit is used for when the receiving module 401 receives the device descriptor acquisition command sent by the host computer, it returns the corresponding data according to the acquisition data length in the acquisition device descriptor command; the return corresponding data is the current signal path support The response data of the maximum packet length or the complete device descriptor;
  • the third return unit is used to save the new address in the address setting request and return a confirmation response to the upper computer when the receiving module 401 receives the address setting request sent by the upper computer;
  • the fourth return unit is used to send the complete configuration information to the upper computer when the receiving module 401 receives the command to obtain the device configuration descriptor sent by the upper computer;
  • the fourth return unit is specifically configured to send the corresponding configuration information to the upper computer every time the receiving module 401 receives the device configuration descriptor acquisition command;
  • the fifth return unit is used to send the description character set to the host computer when the receiving module 401 receives the device string descriptor acquisition command sent by the host computer;
  • the sixth return unit is used to return the human-machine interface device descriptor to the upper computer when the receiving module 401 receives the command to obtain the human-machine interface device descriptor sent by the upper computer;
  • the seventh return unit is used to return a report descriptor to the upper computer when the receiving module 401 receives the command to obtain the report descriptor sent by the upper computer;
  • the report descriptor includes multiple features, each of which includes a report identifier and a corresponding report length;
  • the setting unit is used to configure the device when the receiving module 401 receives a setting configuration command sent by the upper computer.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Quality & Reliability (AREA)
  • Information Transfer Systems (AREA)
  • Accessory Devices And Overall Control Thereof (AREA)

Abstract

本发明公开一种USB通讯的实现方法及装置,该方法包括:步骤S1:当设备接收到上位机发送的命令时,判断命令的类型,如为枚举命令则向所述上位机返回枚举设备总体信息,返回步骤S1;所述设备总体信息包括报告描述符,所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;如为设置报告命令则根据所述设置报告命令获取响应数据并保存,返回步骤S1;如为获取报告命令则根据当前响应数据的长度选择相应的报告长度,获取与所述报告长度对应第二报告标识,根据获取的所述第二报告标识和所述当前响应数据生成返回数据并发送给所述上位机,返回步骤S1。本发明技术方案使数据交互更灵活,通讯速度更快。

Description

一种USB通讯的实现方法及装置
本申请要求于2019年11月11日提交中国专利局、申请号为201911092405.1、申请名称为“一种USB通讯的实现方法及装置”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。
技术领域
本发明涉及电子产品领域,尤其涉及一种USB通讯的实现方法及装置。
背景技术
USB HID类设备属于人机交互操作的设备,是一个比较大的类,用于控制计算机操作的某些方面,如USB鼠标、USB键盘、USB游戏操作杆、USB触摸板、USB轨迹球、电话拨号设备、VCR遥控等等设备。USB HID类设备之间的通讯通过HID(英文全称:Human Interface Device,中文全称:人机接口设备)协议来实现。现有技术中,HID通讯协议一次交互数据包大小固定,发送数据的长度大于固定的数据包大小时,需要分包多次交互才能完成数据发送,通讯速度慢。
发明内容
本发明的目的是为了克服现有技术的不足,提供一种USB通讯的实现方法及装置。
本发明提供了一种USB通讯的实现方法,包括:
步骤S1:当设备接收到上位机发送的命令时,判断命令的类型,如为枚举命令则执行步骤S2,如为设置报告命令则执行步骤S3,如为获取报告命令则执行步骤S4;
步骤S2:所述设备向所述上位机返回枚举设备总体信息,返回步骤S1;所述设备总体信息包括报告描述符,所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
步骤S3:所述设备根据所述设置报告命令获取响应数据并保存,返回步骤S1;
步骤S4:所述设备根据当前响应数据的长度选择相应的报告长度,获取与所述报告长度对应第二报告标识,根据获取的所述第二报告标识和所述当前响应数据生成返回数据并发送给所述上位机,返回步骤S1。
进一步地,所述步骤S2包括:
当所述设备接收到所述上位机发送的获取状态请求时所述设备给所述上位机返回设备状态信息;
当所述设备接收到所述上位机发送的获取设备描述符命令时根据所述获取设备描述符命令中的获取数据长度返回相应的数据;所述返回相应的数据为当前信号通路支持的最大数据包长度的响应数据或完整的设备描述符;
当所述设备接收到所述上位机发送的设置地址请求时所述设备保存所述设置地址请求中的新地址并给所述上位机返回确认响应;
当所述设备接收到所所上位机发送的获取设备配置描述符命令时所述设备将完整 配置信息发送给所述上位机;
当所述设备接收到所述上位机发送的获取设备字符串描述符命令,所述设备将描述字符集发送给所述上位机;
当所述设备接收到所述上位机发送的获取人机接口设备描述符命令,所述设备给所述上位机返回人机接口设备描述符;
当所述设备接收到所述上位机发送的获取报告描述符命令时所述设备给所述上位机返回报告描述符;所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
当所述设备接收到所述上位机发送的设置配置命令时对所述设备进行配置。
进一步地,所述当所述设备接收到所述上位机发送的获取设备配置描述符命令时所述设备将完整配置信息发送给所述上位机,包括:所述设备每次接收到获取设备配置描述符命令后,将对应的配置信息发送给所述上位机。
进一步地,所述步骤S4包括:所述设备根据当前响应数据的长度选择大于且最接近的报告长度,在报告描述符中获取包含所述报告长度的第二特征,获取所述第二特征中的第二报告标识,将获取的所述第二报告标识和所述响应数据顺序拼接生成返回数据并发送给所述上位机,返回步骤S1。
进一步地,所述步骤S1包括:当设备接收到上位机发送的命令时,根据命令头判断命令的类型,如命令头为第三数据则命令的类型为枚举命令,执行步骤S2,如命令头为第一数据则命令的类型为设置报告命令,执行步骤S3,如命令头为第二数据则命令的类型为获取报告命令,执行步骤S4。
进一步地,所述步骤S3具体为:所述设备根据所述设置报告命令中的第一报告标识查找对应的报告长度,根据查找到的报告长度从所述设置报告命令中获取对应长度的数据,处理获取到数据并生成响应数据,保存所述响应数据,返回步骤S1。
进一步地,所述步骤S3之前还包括:所述设备获取所述设置报告命令中数据域的第一字节数据得到所述第一报告标识。
本发明又提供了一种USB通讯的实现装置,包括:
接收模块,用于接收上位机发送的命令;
第一判断模块,用于判断所述接收模块接收到的命令的类型,如为枚举命令则触发第一发送模块,如为设置报告命令则触发获取保存模块,如为获取报告命令则触发选择获取模块;
所述第一发送模块,用于向所述上位机返回枚举设备总体信息,触发所述接收模块;所述设备总体信息包括报告描述符,所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
所述获取保存模块,用于根据所述设置报告命令获取响应数据并保存,触发所述接收模块;
所述选择获取模块,用于根据当前响应数据的长度选择相应的报告长度,获取与所述报告长度对应第二报告标识,根据获取的所述第二报告标识和所述当前响应数据生成返回数据;
第二发送模块,用于将所述选择获取模块生成的返回数据发送给所述上位机,触 发所述接收模块。
进一步地,所述第一发送模块包括:
第一返回单元,用于当所述接收模块接收到所述上位机发送的获取状态请求时,给所述上位机返回设备状态信息;
第二返回单元,用于当所述接收模块接接收到所述上位机发送的获取设备描述符命令时,根据所述获取设备描述符命令中的获取数据长度返回相应的数据;所述返回相应的数据为当前信号通路支持的最大数据包长度的响应数据或完整的设备描述符;
第三返回单元,用于当所述接收模块接接收到所述上位机发送的设置地址请求时,保存所述设置地址请求中的新地址并给所述上位机返回确认响应;
第四返回单元,用于当所述接收模块接接收到所所上位机发送的获取设备配置描述符命令时,将完整配置信息发送给所述上位机;
第五返回单元,用于当所述接收模块接接收到所述上位机发送的获取设备字符串描述符命令时,将描述字符集发送给所述上位机;
第六返回单元,用于当所述接收模块接收到所述上位机发送的获取人机接口设备描述符命令时,给所述上位机返回人机接口设备描述符;
第七返回单元,用于当所述接收模块接收到所述上位机发送的获取报告描述符命令时,给所述上位机返回报告描述符;所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
设置单元,用于当所述接收模块接收到所述上位机发送的设置配置命令时对所述设备进行配置。
进一步地,所述第四返回单元具体用于当所述接收模块每次接收到获取设备配置描述符命令后,将对应的配置信息发送给所述上位机。
进一步地,所述选择获取模块具体用于根据当前响应数据的长度选择大于且最接近的报告长度,在报告描述符中获取包含所述报告长度的第二特征,获取所述第二特征中的第二报告标识,将获取的所述第二报告标识和所述响应数据顺序拼接生成返回数据。
进一步地,所述第一判断模块具体用于当所述接收模块接收到上位机发送的命令时,根据命令头判断命令的类型,如命令头为第三数据则命令的类型为枚举命令,触发第一发送模块,如命令头为第一数据则命令的类型为设置报告命令,触发获取保存模块,如命令头为第二数据则命令的类型为获取报告命令,触发选择获取模块。
进一步地,所述获取保存模块具体用于根据所述设置报告命令中的第一报告标识查找对应的报告长度,根据查找到的报告长度从所述设置报告命令中获取对应长度的数据,处理获取到数据并生成响应数据,保存所述响应数据,触发所述接收模块。
进一步地,所述装置还包括:
获取模块,用于获取所述设置报告命令中数据域的第一字节数据得到所述第一报告标识,触发所述获取保存模块。
本发明与现有技术相比,具有以下优点:
本发明技术方案适用于上位机与设备之间的数据通讯过程,在枚举过程中设置上位机与设备之间的交互数据的长度是不固定的,即报告描述符设置多个特征,每个特 征中的报告长度均不同,通讯时根据需要选择相应的报告长度,将对应的报告标识发送给对方,无论交互数据有多长均可一次发送完成而无需进行分包,使数据交互更灵活,通讯速度更快。
附图说明
图1为本发明实施例一提供的一种USB通讯的实现方法流程图;
图2和图3为本发明实施例二提供的一种USB通讯的实现方法流程图;
图4为本发明实施例三提供的一种USB通讯的实现装置方框图。
具体实施方式
下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。
实施例一
本发明实施例一提供一种USB通讯的实现方法,如图1所示,包括:
步骤S1:当设备接收到上位机发送的命令时,判断命令的类型,如为枚举命令则执行步骤S2,如为设置报告命令则执行步骤S3,如为获取报告命令则执行步骤S4;
可选的,步骤S1包括:当设备接收到上位机发送的命令时,根据命令头判断命令的类型,如命令头为第三数据则命令的类型为枚举命令,执行步骤S2,如命令头为第一数据则命令的类型为设置报告命令,执行步骤S3,如命令头为第二数据则命令的类型为获取报告命令,执行步骤S4;
步骤S2:设备向上位机返回枚举设备总体信息,返回步骤S1;
在本实施例中,设备总体信息包括报告描述符,报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
步骤S3:设备根据设置报告命令获取响应数据并保存,返回步骤S1;
可选的,步骤S3具体为:所述设备根据所述设置报告命令中的第一报告标识查找对应的报告长度,根据查找到的报告长度从所述设置报告命令中获取对应长度的数据,处理获取到数据并生成响应数据,保存所述响应数据,返回步骤S1;
优选的,在本实施例中,步骤S3之前还包括:设备获取设置报告命令中数据域的第一字节数据得到第一报告标识;
步骤S4:设备根据当前响应数据的长度选择相应的报告长度,获取与报告长度对应的第二报告标识,根据获取的第二报告标识和当前响应数据生成返回数据并发送给上位机,返回步骤S1;
具体的,本实施例中的步骤S4包括:设备根据当前响应数据的长度选择大于且最接近的报告长度,在报告描述符中获取包含报告长度的第二特征,获取第二特征中的第二报告标识,将获取的第二报告标识和当前响应数据顺序拼接生成返回数据并发送 给上位机,返回步骤S1。
在本实施例中,步骤S2包括:
当设备接收到上位机发送的获取状态请求时设备给上位机返回设备状态信息;
可选的,设备状态信息包括重启状态信息;
当设备接收到上位机发送的获取设备描述符命令时根据获取设备描述符命令中的获取数据长度返回相应的数据;
具体的,在本实施例中,返回相应的数据为当前信号通路支持的最大数据包长度的响应数据或完整的设备描述符;
当设备接收到上位机发送的设置地址请求时,设备保存设置地址请求中的新地址并给上位机返回确认响应;
当设备接收到所所上位机发送的获取设备配置描述符命令时设备将完整配置信息发送给上位机;
在本实施例中,当设备接收到所所上位机发送的获取设备配置描述符命令时设备将完整配置信息发送给上位机,包括:设备每次接收到获取设备配置描述符命令后,将对应的配置信息发送给上位机;
当设备接收到上位机发送的获取设备字符串描述符命令,设备将描述字符集发送给上位机;
当设备接收到上位机发送的获取人机接口设备描述符命令,设备给上位机返回人机接口设备描述符;
当设备接收到上位机发送的获取报告描述符命令时设备给上位机返回报告描述符;报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
当设备接收到上位机发送的设置配置命令时对设备进行配置。
实施例二
本发明实施例二提供一种USB通讯的实现方法,本实施例方法包括枚举过程和数据传输过程,枚举过程如图2所示,包括:
步骤A1:上位机通过集线器通过监视每个端口的信号电压,当监视信号电压升高则有设备插入,执行步骤A2;
具体的,上位机的集线器端口的两根信号线的每一根都有15kΩ的下拉电阻,而每一个设备在D+都有一个1.5kΩ的上拉电阻。当用USB线将上位机和设备接通后,设备的上拉电阻使信号电压升高,集线器就会检测到有设备插入;
步骤A2:上位机给设备发送Get_Status(获取状态)请求;
在本实施例中,集线器通过中断传输给上位机报告有设备插入的事件,上位机通过集线器给设备发送Get_Status(获取状态)请求;
步骤A3:设备给上位机返回设备与上位机的连接时间;
步骤A4:上位机给集线器发送Set_Feature(设置特征)请求,集线器重启设备端口,建立一个设备与上位机之间的信号通路;
在本实施例中,上位机给集线器发送一个Set_Feature(设置特征)请求,请求集线器来重启设备端口,使得设备的USB数据线处于重启(RESET)状态至少10ms, 然后建立一个设备与上位机之间的信号通路;
步骤A5:上位机给设备发送Get_Status(获取状态)请求;
步骤A6:设备给上位机返回设备仍然处于重启状态信息;
在本实施例中,当集线器释放了重启状态,设备就处于默认状态了,设备已经准备好通过Endpoint 0(端点0)的默认流程响应控制传输,即设备现在使用默认地址0x00与上位机开始进行通信;
步骤A7:上位机通过集线器检测设备类型;
具体的,上位机通过集线器测定哪根信号线(D+或D-)在空闲时有更高的电压来检测设备是低速设备还是全速设备,全速和高速设备D+有上拉电阻,低速设备D-有上拉电阻;
步骤A8:上位机向默认地址的设备发送Get_Device_Descriptor(获取设备描述符)命令;
步骤A9:设备将包含当前信号通路支持的最大数据包长度的响应数据返回给上位机;
具体的,在本实施例中,上位机向address 0(对应默认地址0x00)发送Get_Device_Descriptor(获取设备描述符)命令,获取缺省控制管道所支持的最大数据包长度,在预设时间内等待USB设备的响应;当接收到设备的响应时获取前8个字节数据得到最大数据包长度;
步骤A10:上位机给设备发送包含新地址的Set_Address(设置地址)请求;
步骤A11:设备保存Set_Address(设置地址)请求中的新地址并给上位机返回确认响应;
具体的,在本实施例中,上位机通过给设备发送一个Set_Address(设置地址)请求来分配一个唯一的地址给设备;设备读取Set_Address(设置地址)请求,给上位机返回确认响应,并保存新的地址;
步骤A12:上位机向新地址的设备重新发送Get_Device_Descriptor(获取设备描述符)命令;
步骤A13:设备将设备描述符发送给上位机,上位机获取设备的总体信息;
例如,设备的总体信息包括VID(厂商标识)、PID(产品标识)等,返回的设备描述符中bDeviceClass(设备类型)、bDeviceSubClass(设备子类型)、bDeviceProtocol(设备协议)的值均为0;
步骤A14:上位机发送Get_Device_Configuration(获取设备配置描述符)命令;
具体的,上位机向设备循环发送Get_Device_Configuration(获取设备配置描述符)命令,以读取设备的全部配置信息;其中配置信息的接口描述符中bInterFaceClass(接口类型)的值为0x03,表明该设备为HID(人机接口设备)设备;
步骤A15:设备将完整配置信息发送给上位机;
步骤A16:上位机给设备发送Get_Device_String(获取设备字符串描述符)命令;
步骤A17:设备将描述字符集发送给上位机;
具体的,描述字符集包括:厂商、产品描述、型号等信息;
步骤A18:上位机给设备发送获取HID(人机接口设备)描述符命令;
步骤A19:设备给上位机返回HID描述符;
其中HID(人机接口设备)描述符中指示需要返回报告描述符;
步骤A20:上位机给设备发送获取报告描述符命令;
步骤A21:设备给上位机返回报告描述符;
具体的,报告描述符中有多个Feature(特征),不同Feature(特征)通过ReportID(报告标识)区分,且每个Feature(特征)的Report size(报告长度)不同;其中ReportSize(报告长度)表示可发送的数据长度和可接收的响应长度,包括最小长度(0x01)和最大长度(0xFFFF),即不同的数据长度,有一个对应的ReprotID(报告标识);
例如,本实施例中的报告描述符为:HID(Human Interface Device)报告描述符的内容如下:
Figure PCTCN2020127870-appb-000001
Figure PCTCN2020127870-appb-000002
Figure PCTCN2020127870-appb-000003
在上述报告描述符中,报告标识的值为0x01-0x32。其中报告标识为0x01,一次通信的数据长度为0x07,即7个字节;报告标识为0x2E,一次通信的数据长度为0x080F......;
步骤A22:上位机显示发现的新设备信息;
具体的,上位机弹出窗口展示发现新设备的信息,包括:产商、产品描述、型号等;
步骤A23:上位机给设备发送Set_Configuration(x)(设置配置)命令,对设备进行配置;
具体的,在本实施例中,上位机加载了USB设备驱动以后,给设备发送Set_Configuration(x)(设置配置)命令(x代表非0的配置值)请求对该设备进行配置,如果配置成功,USB设备进入“配置”状态,并可以和客户软件进行数据传输;
数据传输过程如图3所示包括:
步骤B1:上位机根据待发送数据的长度选择报告长度,并获取该报告长度对应的报告标识;
具体的,其中发送数据的长度选择大于该长度且最接近的报告长度;
例如,本实施例中的待发送数据长度为512,内容具体为:
55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555 55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555;
发送数据的格式为:00 D6 00 00 00 02 00||待更新数据;发送数据总长度为:512+7=519字节,根据发送数据总长度选择的报告长度应该大于519且为最小的一个,本实施例中查找到的报告长度为0x020F即527,对应的报告标识为0x26;
步骤B2:上位机将报告标识和待发送数据拼接生成数据包;
具体的,在本实施例中,上位机在待发送数据前填充报告标识,如填充报告标识后的数据长度小于该报告标识对应的报告长度,则在待发送数据尾部填充数据(00)至报告长度。例如,本实施例中,填充后的数据为:
26 00 D6 00 00 00 02 00
55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555550000000000000000;
步骤B3:上位机调用SetFeature()接口向设备发送包含数据包的设置报告命令;
例如,本实施例中调用SetFeature接口先设备发送的的设置报告命令为:
21 09 26 03 00 00 0F 02
26 00 D6 00 00 00 02 00
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555550000000000000000;
步骤B4:当上位机检测到设置报告命令发送完成之后,根据最大数据长度对应的报告标识生成获取报告命令并发送给设备,等待接收设备返回的响应;
例如,本实施例中获取报告命令为:A1 01 32 03 00 00 FF 0F;
步骤B5:当设备接收到命令时,并判断命令的类型,如为设置报告命令则执行步骤B6,如为获取报告命令则执行步骤B8;
具体的,在本实施例中,设备判断命令的头部,如为21 09则命令类型为设置报告命令,如为A1 01则命令类型为获取报告命令;
步骤B6:设备获取接收到的命令中的报告标识,根据上报的报告描述符查找报告标识对应的报告长度,根据报告长度从命令中获取对应长度的数据;
步骤B7:设备处理获取到的数据并生成响应数据,等待接收数据,返回步骤B5;
步骤B8:设备根据响应数据的长度选择报告描述符中相应的报告长度并获取对应的报告标识,根据报告标识和响应数据组织生成返回数据;
具体的,设备选择报告描述符中大于响应数据的长度且与其最接近的报告长度的值;
例如,本实施例中的响应数据为00 02 90 00,即响应数据的长度为4字节,获取的报告标识为0x01即可,组织生成的返回数据为:01 00 02 90 00 00 00 00;
步骤B9:设备将组织生成的返回数据返回给上位机;
例如,本实施例中的返回数据为:01 00 02 90 00 00 00 00;
步骤B10:上位机GetFeature()接口接收返回数据,获取返回数据中的报告标识,在报告描述符中查找报告标识对应的报告长度,根据报告长度从返回数据中获取对应长度的响应数据,对该响应数据进行处理。
实施例三
本发明实施例三提供一种USB通讯的实现装置,如图4所示,包括:
接收模块401,用于接收上位机发送的命令;
第一判断模块402,用于判断接收模块401接收到的命令的类型,如为枚举命令则触发第一发送模块403,如为设置报告命令则触发获取保存模块404,如为获取报告命令则触发选择获取模块405;
在本实施例中,第一判断模块402具体用于当接收模块401接收到上位机发送的命令时,根据命令头判断命令的类型,如命令头为第三数据则命令的类型为枚举命令,触发第一发送模块403,如命令头为第一数据则命令的类型为设置报告命令,触发获取保存模块404,如命令头为第二数据则命令的类型为获取报告命令,触发选择获取模块405;
第一发送模块403,用于向上位机返回枚举设备总体信息,触发接收模块401;设 备总体信息包括报告描述符,报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
获取保存模块404,用于根据设置报告命令获取响应数据并保存,触发接收模块401;
在本实施例中,获取保存模块404具体用于根据设置报告命令中的第一报告标识查找对应的报告长度,根据查找到的报告长度从设置报告命令中获取对应长度的数据,处理获取到数据并生成响应数据,保存响应数据,触发接收模块401;可选的,本实施例的装置还包括:获取模块,用于获取设置报告命令中数据域的第一字节数据得到第一报告标识,触发获取保存模块404;
选择获取模块405,用于根据当前响应数据的长度选择相应的报告长度,获取与报告长度对应第二报告标识,根据获取的第二报告标识和当前响应数据生成返回数据;
在本实施例中,选择获取模块405具体用于根据当前响应数据的长度选择大于且最接近的报告长度,在报告描述符中获取包含报告长度的第二特征,获取第二特征中的第二报告标识,将获取的第二报告标识和响应数据顺序拼接生成返回数据;
第二发送模块406,用于将选择获取模块405生成的返回数据发送给上位机,触发接收模块401。
具体的,在本实施例中,第一发送模块403包括:
第一返回单元,用于当接收模块401接收到上位机发送的获取状态请求时,给上位机返回设备状态信息;
第二返回单元,用于当接收模块401接接收到上位机发送的获取设备描述符命令时,根据获取设备描述符命令中的获取数据长度返回相应的数据;返回相应的数据为当前信号通路支持的最大数据包长度的响应数据或完整的设备描述符;
第三返回单元,用于当接收模块401接接收到上位机发送的设置地址请求时,保存设置地址请求中的新地址并给上位机返回确认响应;
第四返回单元,用于当接收模块401接接收到所所上位机发送的获取设备配置描述符命令时,将完整配置信息发送给上位机;
在本实施例中,第四返回单元具体用于当接收模块401每次接收到获取设备配置描述符命令后,将对应的配置信息发送给上位机;
第五返回单元,用于当接收模块401接接收到上位机发送的获取设备字符串描述符命令时,将描述字符集发送给上位机;
第六返回单元,用于当接收模块401接收到上位机发送的获取人机接口设备描述符命令时,给上位机返回人机接口设备描述符;
第七返回单元,用于当接收模块401接收到上位机发送的获取报告描述符命令时,给上位机返回报告描述符;报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
设置单元,用于当接收模块401接收到上位机发送的设置配置命令时对设备进行配置。
以上所述,仅为本发明较佳的具体实施方式,但本发明的保护范围并不局限于此,任何熟悉本技术领域的技术人员在本发明公开的技术范围内,可轻易想到的变化或替 换,都应涵盖在本发明的保护范围之内。因此,本发明的保护范围应该以权利要求的保护范围为准。

Claims (14)

  1. 一种USB通讯的实现方法,其特征在于,包括:
    步骤S1:当设备接收到上位机发送的命令时,判断命令的类型,如为枚举命令则执行步骤S2,如为设置报告命令则执行步骤S3,如为获取报告命令则执行步骤S4;
    步骤S2:所述设备向所述上位机返回枚举设备总体信息,返回步骤S1;所述设备总体信息包括报告描述符,所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
    步骤S3:所述设备根据所述设置报告命令获取响应数据并保存,返回步骤S1;
    步骤S4:所述设备根据当前响应数据的长度选择相应的报告长度,获取与所述报告长度对应第二报告标识,根据获取的所述第二报告标识和所述当前响应数据生成返回数据并发送给所述上位机,返回步骤S1。
  2. 如权利要求1所述的方法,其特征在于,所述步骤S2包括:
    当所述设备接收到所述上位机发送的获取状态请求时所述设备给所述上位机返回设备状态信息;
    当所述设备接收到所述上位机发送的获取设备描述符命令时根据所述获取设备描述符命令中的获取数据长度返回相应的数据;所述返回相应的数据为当前信号通路支持的最大数据包长度的响应数据或完整的设备描述符;
    当所述设备接收到所述上位机发送的设置地址请求时所述设备保存所述设置地址请求中的新地址并给所述上位机返回确认响应;
    当所述设备接收到所所上位机发送的获取设备配置描述符命令时所述设备将完整配置信息发送给所述上位机;
    当所述设备接收到所述上位机发送的获取设备字符串描述符命令,所述设备将描述字符集发送给所述上位机;
    当所述设备接收到所述上位机发送的获取人机接口设备描述符命令,所述设备给所述上位机返回人机接口设备描述符;
    当所述设备接收到所述上位机发送的获取报告描述符命令时所述设备给所述上位机返回报告描述符;所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
    当所述设备接收到所述上位机发送的设置配置命令时对所述设备进行配置。
  3. 如权利要求2所述的方法,其特征在于,所述当所述设备接收到所述上位机发送的获取设备配置描述符命令时所述设备将完整配置信息发送给所述上位机,包括:所述设备每次接收到获取设备配置描述符命令后,将对应的配置信息发送给所述上位机。
  4. 如权利要求1所述的方法,其特征在于,所述步骤S4包括:所述设备根据当前响应数据的长度选择大于且最接近的报告长度,在报告描述符中获取包含所述报告长度的第二特征,获取所述第二特征中的第二报告标识,将获取的所述第二报告标识和所述响应数据顺序拼接生成返回数据并发送给所述上位机,返回步骤S1。
  5. 如权利要求1所述的方法,其特征在于,所述步骤S1包括:当设备接收到上位机发送的命令时,根据命令头判断命令的类型,如命令头为第三数据则命令的类型 为枚举命令,执行步骤S2,如命令头为第一数据则命令的类型为设置报告命令,执行步骤S3,如命令头为第二数据则命令的类型为获取报告命令,执行步骤S4。
  6. 如权利要求1所述的方法,其特征在于,所述步骤S3具体为:所述设备根据所述设置报告命令中的第一报告标识查找对应的报告长度,根据查找到的报告长度从所述设置报告命令中获取对应长度的数据,处理获取到数据并生成响应数据,保存所述响应数据,返回步骤S1。
  7. 如权利要求6所述的方法,其特征在于,所述步骤S3之前还包括:所述设备获取所述设置报告命令中数据域的第一字节数据得到所述第一报告标识。
  8. 一种USB通讯的实现装置,其特征在于,包括:
    接收模块,用于接收上位机发送的命令;
    第一判断模块,用于判断所述接收模块接收到的命令的类型,如为枚举命令则触发第一发送模块,如为设置报告命令则触发获取保存模块,如为获取报告命令则触发选择获取模块;
    所述第一发送模块,用于向所述上位机返回枚举设备总体信息,触发所述接收模块;所述设备总体信息包括报告描述符,所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
    所述获取保存模块,用于根据所述设置报告命令获取响应数据并保存,触发所述接收模块;
    所述选择获取模块,用于根据当前响应数据的长度选择相应的报告长度,获取与所述报告长度对应第二报告标识,根据获取的所述第二报告标识和所述当前响应数据生成返回数据;
    第二发送模块,用于将所述选择获取模块生成的返回数据发送给所述上位机,触发所述接收模块。
  9. 如权利要求8所述的装置,其特征在于,所述第一发送模块包括:
    第一返回单元,用于当所述接收模块接收到所述上位机发送的获取状态请求时,给所述上位机返回设备状态信息;
    第二返回单元,用于当所述接收模块接接收到所述上位机发送的获取设备描述符命令时,根据所述获取设备描述符命令中的获取数据长度返回相应的数据;所述返回相应的数据为当前信号通路支持的最大数据包长度的响应数据或完整的设备描述符;
    第三返回单元,用于当所述接收模块接接收到所述上位机发送的设置地址请求时,保存所述设置地址请求中的新地址并给所述上位机返回确认响应;
    第四返回单元,用于当所述接收模块接接收到所所上位机发送的获取设备配置描述符命令时,将完整配置信息发送给所述上位机;
    第五返回单元,用于当所述接收模块接接收到所述上位机发送的获取设备字符串描述符命令时,将描述字符集发送给所述上位机;
    第六返回单元,用于当所述接收模块接收到所述上位机发送的获取人机接口设备描述符命令时,给所述上位机返回人机接口设备描述符;
    第七返回单元,用于当所述接收模块接收到所述上位机发送的获取报告描述符命 令时,给所述上位机返回报告描述符;所述报告描述符包括多个特征,每个特征包括报告标识和对应的报告长度;
    设置单元,用于当所述接收模块接收到所述上位机发送的设置配置命令时对所述设备进行配置。
  10. 如权利要求9所述的装置,其特征在于,所述第四返回单元具体用于当所述接收模块每次接收到获取设备配置描述符命令后,将对应的配置信息发送给所述上位机。
  11. 如权利要求8所述的装置,其特征在于,所述选择获取模块具体用于根据当前响应数据的长度选择大于且最接近的报告长度,在报告描述符中获取包含所述报告长度的第二特征,获取所述第二特征中的第二报告标识,将获取的所述第二报告标识和所述响应数据顺序拼接生成返回数据。
  12. 如权利要求8所述的装置,其特征在于,所述第一判断模块具体用于当所述接收模块接收到上位机发送的命令时,根据命令头判断命令的类型,如命令头为第三数据则命令的类型为枚举命令,触发第一发送模块,如命令头为第一数据则命令的类型为设置报告命令,触发获取保存模块,如命令头为第二数据则命令的类型为获取报告命令,触发选择获取模块。
  13. 如权利要求8所述的装置,其特征在于,所述获取保存模块具体用于根据所述设置报告命令中的第一报告标识查找对应的报告长度,根据查找到的报告长度从所述设置报告命令中获取对应长度的数据,处理获取到数据并生成响应数据,保存所述响应数据,触发所述接收模块。
  14. 如权利要求13所述的装置,其特征在于,所述装置还包括:
    获取模块,用于获取所述设置报告命令中数据域的第一字节数据得到所述第一报告标识,触发所述获取保存模块。
PCT/CN2020/127870 2019-11-11 2020-11-10 一种usb通讯的实现方法及装置 WO2021093734A1 (zh)

Priority Applications (1)

Application Number Priority Date Filing Date Title
US17/478,829 US11663155B2 (en) 2019-11-11 2021-09-17 Method and apparatus for realizing USB communication

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201911092405.1A CN110830168B (zh) 2019-11-11 2019-11-11 一种usb通讯的实现方法及装置
CN201911092405.1 2019-11-11

Related Child Applications (1)

Application Number Title Priority Date Filing Date
US17/478,829 Continuation US11663155B2 (en) 2019-11-11 2021-09-17 Method and apparatus for realizing USB communication

Publications (1)

Publication Number Publication Date
WO2021093734A1 true WO2021093734A1 (zh) 2021-05-20

Family

ID=69553656

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/127870 WO2021093734A1 (zh) 2019-11-11 2020-11-10 一种usb通讯的实现方法及装置

Country Status (3)

Country Link
US (1) US11663155B2 (zh)
CN (1) CN110830168B (zh)
WO (1) WO2021093734A1 (zh)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110830168B (zh) 2019-11-11 2021-11-02 飞天诚信科技股份有限公司 一种usb通讯的实现方法及装置
CN112000374B (zh) * 2020-07-13 2024-01-02 深圳市智微智能软件开发有限公司 应用于安卓系统的usb触摸屏更换方法、装置、设备及介质
CN117544334A (zh) * 2022-08-08 2024-02-09 黄建邦 设备接入方法、数据交换方法、装置、设备及存储介质

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7660611B1 (en) * 2004-03-25 2010-02-09 Cypress Semiconductor Corporation Wireless human interface device packet compression system and method for reducing power consumption
CN102012886A (zh) * 2010-10-14 2011-04-13 深圳市文鼎创数据科技有限公司 基于hid协议的通讯方法、装置及系统
CN110830168A (zh) * 2019-11-11 2020-02-21 飞天诚信科技股份有限公司 一种usb通讯的实现方法及装置

Family Cites Families (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN100409150C (zh) * 2006-09-07 2008-08-06 北京飞天诚信科技有限公司 一种提高hid设备通讯速度的方法
TW200943188A (en) * 2008-04-01 2009-10-16 Sunplus Innovation Technology Inc A method for automatically detecting an operating system on a USB device
US7873764B2 (en) * 2008-08-15 2011-01-18 Video Products, Inc. Switch with enhanced human interface device support
CN102331943B (zh) * 2011-09-08 2014-09-17 威盛电子股份有限公司 在线更新存储器系统与方法
CN102638397B (zh) * 2012-03-21 2014-12-10 华为终端有限公司 一种usb设备通信传输方法、设备及系统
CN105184566B (zh) * 2015-06-16 2018-07-17 飞天诚信科技股份有限公司 一种智能密钥设备的工作方法
CN108027759B (zh) * 2015-09-25 2022-05-03 英特尔公司 传感器生命周期管理系统
CN106406967A (zh) * 2016-11-15 2017-02-15 恒鸿达信息技术有限公司 一种基于stm32的usb在线升级方法及系统

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7660611B1 (en) * 2004-03-25 2010-02-09 Cypress Semiconductor Corporation Wireless human interface device packet compression system and method for reducing power consumption
CN102012886A (zh) * 2010-10-14 2011-04-13 深圳市文鼎创数据科技有限公司 基于hid协议的通讯方法、装置及系统
CN110830168A (zh) * 2019-11-11 2020-02-21 飞天诚信科技股份有限公司 一种usb通讯的实现方法及装置

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
WANG YUN: "Research and Implementation of Infrared Touch Screen Based on HID Class", CHINESE MASTER’S THESES FULL-TEXT DATABASE, 6 June 2014 (2014-06-06), XP055811988 *

Also Published As

Publication number Publication date
US20220004519A1 (en) 2022-01-06
CN110830168A (zh) 2020-02-21
US11663155B2 (en) 2023-05-30
CN110830168B (zh) 2021-11-02

Similar Documents

Publication Publication Date Title
WO2021093734A1 (zh) 一种usb通讯的实现方法及装置
US7984318B2 (en) Apparatus and method to support USB enumeration of a bus powered handheld device
US20080276012A1 (en) Driver Loading via a PnP Device
US9804858B2 (en) System for re-enumeration of USB 3.0 compatible peripheral devices
WO2000049507A1 (en) Method and apparatus for extending the range of the universal serial bus protocol
WO2012100724A1 (zh) 在pcie总线上传输报文的方法、设备和系统
US20090019192A1 (en) Usb transmission system and related method for accessing data in a first usb specification with a speed of a second usb specification
EP1775652A1 (en) Apparatus and method to support USB enumeration of a BUS powered handheld device
JP5242558B2 (ja) Usb割込みエンドポイントの共有
CN109918320B (zh) 一种usb设备的控制方法、装置及kvm控制系统
WO2015013981A1 (zh) 一种图像显示方法和装置
CN110069288B (zh) Usb设备共享方法、装置及系统
CN101316203B (zh) 嵌入式ip终端网络连接状态监测方法
JP2010020757A (ja) Usbケーブルを使った遠隔デスクトップ制御システムおよびその方法
WO2023011067A1 (zh) 充电需求适配方法、电子设备及相关产品
US8996771B1 (en) System and method for communication via universal serial bus
CN107153456A (zh) Otg连接控制方法、otg连接控制装置及电子设备
WO2022166698A1 (zh) 充电控制方法、装置、电子设备及充电器
TWI250413B (en) Universal serial bus (USB) system with single port and host controller thereof
CN112416829A (zh) 数据传输到hmi的方法以及usb hid设备
JP2017010174A (ja) デバイス安全性検知装置、デバイス安全性検知方法、及びプログラム
CN114371978B (zh) 安卓获取数据的方法、设备、装置和系统
JP2002190848A (ja) インターフェース適合化装置
CN100424617C (zh) 网络连接的管理方法及电子装置
CN101539893A (zh) 快速打开网页的输入装置和快速打开网页的方法

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20886284

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20886284

Country of ref document: EP

Kind code of ref document: A1