Communication system and method based on USB, electronic equipment and medium

By using batch transmission methods of EP1 OUT, EP1 IN, and EP2 IN endpoints in the USB communication system, and using string mode command packets and reply packets to transmit, the development difficulty and efficiency problems during USB large data transmission are solved, and the development is achieved More efficient communication.

CN118939590APending Publication Date: 2024-11-12HEFEI I TEK OPTOELECTRONICS CO LTD

Patent Information

Application Number
CN202411097267.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-12
Publication Date
2024-11-12

AI Technical Summary

Technical Problem

When the existing USB data transmission method transmits large data volumes, it leads to problems such as increasing development difficulty, reducing system efficiency and increasing resource consumption.

Method used

Adopt a USB-based communication system, and use three bulk endpoints, EP1 OUT, EP1 IN, and EP2 IN, to realize data interaction between USB host and USB device. Through string mode transmission of command packets and reply packets, reduce system resource consumption and improve communication efficiency .

Benefits of technology

It reduces the difficulty of developing communication between USB host and USB device, reduces system communication overhead, and improves system communication efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118939590A_ABST
    Figure CN118939590A_ABST
Patent Text Reader

Abstract

The invention provides a communication system and method based on a USB, electronic equipment and a medium. The system comprises the USB device and the USB host. The method comprises the steps that a USB host issues a command packet, and the command packet is transmitted to USB equipment through an output endpoint; the USB device receives and analyzes the command packet, obtains command information, generates a reply packet, and caches the reply packet to a buffer area of the endpoint 1; wherein the reply packet comprises reply information in a character string mode; and the USB host sends a read request through the endpoint 1, obtains a reply packet and analyzes the reply packet into reply information. Based on three bulk endpoints (batch transmission endpoints) of EP1 OUT (output endpoint), EP1 IN (endpoint 1) and EP2 IN (endpoint 2), the data interaction between the USB host and the USB equipment is realized, and compared with related standard specifications, the realization difficulty of a communication system is reduced, and the system resource consumption is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data communication technology, and in particular to a communication system, method, electronic equipment and medium based on USB. Background Art

[0002] The full name of USB is Universal Serial Bus (USB), which is a master-slave structure system. It can only occur between USB hosts and USB devices. USB hosts and USB devices cannot directly interconnect and exchange data. The USB host is called Host, and the USB slave is called Device (also called USB device). Although USB defines that the basic unit of data transmission on the bus is data packets, data packets cannot be used arbitrarily to transmit data. These different data packets must be organized into transactions according to certain relationships in order to transmit data.

[0003] USB transaction transfer types include four different types: control transfers, bulk transfers, interrupt transfers, and isochronous transfers.

[0004] Among them, control transmission is data transmission through control endpoints. It is a bidirectional transmission used to control access to different parts of USB devices. It is usually used to configure devices, obtain device information, send commands to devices, obtain device status reports, etc. Bulk transmission (block transmission) is data transmission through bulk endpoints (such as output (out) endpoints and input (in) endpoints). It is mainly used to transmit and receive large amounts of data without bandwidth and interval time requirements. Bulk transmission is usually used in occasions with large data volumes and low requirements for data real-time performance, such as USB printers, scanners, and large-capacity storage devices. Interrupt transmission is data transmission through interrupt endpoints. It is a unidirectional transmission and only an input method for the USB host. It is used to transmit a small amount of data at a fixed rate when the USB host requests data from the USB device. For example, USB keyboards and USB mice use this data transmission method. Isochronous transmission is data transmission through isochronous endpoints. It is used to transmit large amounts of data, but it does not guarantee that the data can arrive. Isochronous endpoints are used in devices that can handle data loss and can send and receive data at a stable rate. For example, audio devices and video devices use this transmission method.

[0005] With the continuous development of communication technology, the application and development needs for large data transmission methods are becoming more urgent. In existing technologies, batch transmission (block transmission) is usually used to achieve the transmission of large amounts of data, and is developed based on relevant standards. However, in the transmission method of the transmission standard, a unified transmission format and control command set are defined to ensure the interoperability between cameras and software from different manufacturers, but at the same time, a lot of unnecessary work is generated in the information generation, transmission, analysis and storage during the transmission process, which leads to the negative impact of increased development difficulty, reduced system efficiency and increased system resource consumption.

[0006] Chinese patent CN114741345B provides a USB DMA data transmission device and method. The device includes a memory, a DMA control module, a USB controller and an interface conversion selection module; the USB controller includes endpoint 0, endpoint 1, endpoint 2 and a DMA module, endpoint 0 is used for data interaction with the USB host during enumeration, and endpoint 1 and endpoint 2 are used for data interaction with the host during batch transmission; the DMA control module is used to configure the DMA module during batch transmission with the host, so that the DMA module can realize data interaction between the memory and endpoint 1 and endpoint 2; the memory is connected to the USB controller, and the DMA control module is connected to the DMA module through the interface conversion selection module. The device can greatly improve data transmission efficiency, save MCU resources, and simplify the transmission process of batch transmission. The technical problem solved by this patent is different from that of this solution. Summary of the invention

[0007] The present invention provides a USB-based communication system, method, electronic device and medium, which can solve at least one of the above technical problems.

[0008] To achieve the above object, the present invention proposes the following technical solutions: A USB-based communication system, comprising a USB device, USB devices include: A USB transmission module, including endpoint 1, endpoint 2 and output endpoint, and the USB transmission module is used to realize data interaction between a USB host and a USB device; Endpoint 1 is used to transmit the reply packet of the USB device to the USB host; Endpoint 1 has a buffer in the IN direction for caching the reply packet; Endpoint 2 is used to transmit the collected data of the USB device to the USB host; Endpoint 2 has an IN direction buffer for caching the collected data; The output endpoint is used to transmit the command packet of the USB host to the USB device; the output endpoint is provided with a buffer in the OUT direction for caching the command packet; The command packet includes the byte length data of the command packet and the command information in the character string mode; the reply packet includes the reply information in the character string mode.

[0009] Furthermore, the USB device further comprises: The parsing module is used to receive the command packet from the USB host, parse the command packet, and obtain the command information; The execution module is used to execute the command and generate a reply packet according to the command information, output the reply packet to the buffer of endpoint 1; send the acquisition signal to the acquisition module; The acquisition module is used to acquire data and output the acquired data to the buffer of endpoint 2 according to the acquisition signal; or to stop acquiring data according to the acquisition signal.

[0010] Furthermore, it also includes: a USB host, which is used to generate a command packet, obtain a reply packet from the USB device and parse it into reply information, and obtain collected data from the USB device.

[0011] Based on the same inventive concept, the present invention also proposes a USB-based communication method, comprising: The USB host sends a command packet and transmits the command packet to the USB device through the output endpoint; wherein the command packet includes the byte length data of the command packet and the command information in the string mode; The USB device receives and parses the command packet, obtains command information, generates a reply packet, and caches it in the buffer of endpoint 1; wherein the reply packet includes reply information in a string mode; The USB host sends a read request through endpoint 1, obtains the reply packet, and parses it into reply information.

[0012] Furthermore, the generation of the reply packet includes: based on the coding table, the USB device parses the command packet into command information in a string mode, determines whether the command packet is correct, and if correct, executes the instruction based on the command information, and generates the instruction execution result value as the reply information; otherwise, generates an error status value as the reply information.

[0013] Furthermore, it also includes: the USB device receives and parses the command packet, obtains the command information, generates a reply packet and collects data; the reply packet is cached in the buffer of endpoint 1, and the collects data is cached in the buffer of endpoint 2; the USB host sends a read request through endpoint 1, obtains the reply packet, and parses it into reply information; the USB host sends a read request through endpoint 2 to obtain the collects data.

[0014] Furthermore, the generation of the reply packet and the collection data includes: based on the coding table, the USB device parses the command packet as command information in a string mode, and determines whether the command packet is correct; if correct, based on the command information, executes the instruction, generates the instruction execution result value as the reply information, and if the instruction execution result value is a successful execution result value, generates the collection data or stops the collection; if incorrect, generates an error status value as the reply information.

[0015] Furthermore, judging whether the command packet is correct includes: judging whether the command packet format is correct and judging whether the command information is legal. If the command packet format is correct and the command information is legal, the command packet is correct; otherwise, it is incorrect. Judging the command packet format includes judging whether the command packet contains byte length data of the command packet and command information in a character string pattern.

[0016] The present invention also proposes an electronic device, including a processor and a memory, wherein the memory is used to store a computer program, the computer program includes program instructions, and the processor is configured to call the program instructions to execute the USB-based communication method as described above.

[0017] On the other hand, the present invention further provides a computer-readable storage medium, comprising a computer program, wherein when the computer program is executed by a processor, the USB-based communication method as described above is implemented.

[0018] The beneficial effects of the present invention are as follows: The present application proposes a USB-based communication system, which realizes data interaction between a USB host and a USB device based on three bulk endpoints (bulk transfer endpoints) of EP1 OUT (output endpoint), EP1 IN (endpoint 1), and EP2 IN (endpoint 2). Compared with relevant standard specifications, the system reduces the difficulty of implementing the communication system and reduces system resource consumption.

[0019] In the USB-based communication method proposed in this application, EP1 OUT (output endpoint) transmits a command packet containing only the command packet byte length and string command information, EP1 IN (endpoint 1) transmits a reply packet containing only string reply information, and EP2 IN (endpoint 2) transmits collected data that does not include a header packet or a tail packet. Compared with relevant standard specifications, this reduces system communication overhead and improves system communication efficiency.

[0020] In addition, the command information generated by the USB host and the reply information generated by the USB device in this application adopt a string mode, in which the part less than 4 will be filled with 0x00; because the general string ends with NULL, there will be no additional burden on the information parsing work.

[0021] In summary, the present application proposes a lightweight USB-based communication method, which reduces the development difficulty of USB host and USB device communication, reduces system communication overhead, and improves system communication efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] Figure 1 This is a schematic diagram of the principle of USB bulk transfer; Figure 2 It is a transmission schematic diagram of the USB-based communication system in the present invention; Figure 3 is a transmission schematic diagram of embodiment 1 of the present invention; Figure 4 is a transmission schematic diagram of embodiment 2 of the present invention; Figure 5 This is a transmission diagram of Example 3 of the present invention. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments.

[0024] First, some related concepts are briefly introduced to better understand the present application.

[0025] Batch transfer: Data transmission through bulk endpoints (such as output (out) endpoints and input (in) endpoints) is mainly used to transmit and receive large amounts of data without bandwidth and interval time requirements. It can ensure that no data is lost, but it does not guarantee that the transmission will be completed within a specific time. If there is not enough space on the USB bus to send the entire bulk data packet, it will be divided into multiple data packets for transmission. Bulk transmission is usually used in situations where the amount of data is large and the real-time requirements for data are not high, such as USB printers, scanners, mass storage devices, etc. Bulk transmission can occupy any available data bandwidth.

[0026] Bulk IN: Bulk transfer in the IN direction, that is, the USB Host reads the data in the USB Device.

[0027] Bulk OUT: Bulk transfer in the OUT direction, that is, the USB Host stores data in the USB Device.

[0028] An endpoint is the smallest unit in a USB device that can send and receive data. It supports unidirectional or bidirectional data transmission. A specific endpoint can only work in one transmission mode. Usually, the endpoint working in a certain mode is called an endpoint. For example, control endpoint, bulk endpoint, interrupt endpoint, isochronous endpoint, etc.

[0029] Currently, USB bulk / block transfer modes are as follows: Only full-speed and high-speed devices can use bulk transfer, low-speed mode does not support bulk transfer. High-speed mode is fixed at 512 bytes; the maximum packet length of full-speed mode can be selected from 8, 16, 32, and 64 bytes.

[0030] The maximum length of a SuperSpeed ​​device data packet is 1024 bytes, and a bulk transfer endpoint shall set the maximum data packet payload size to 1024 bytes in its endpoint descriptor. The SuperSpeed ​​device data packet also specifies the burst size that the endpoint can receive or send to the SuperSpeed ​​bus. The burst size allowed for bulk endpoints shall be in the range of 1 to 16 bytes. Bulk transfer endpoints must transmit data payloads with a data field less than or equal to 1024 bytes. If a bulk transfer has more data than that, all data payloads in the burst transaction must be 1024 bytes in length, except for the last data payload of the burst, and the SuperSpeed ​​device data packet may contain the remaining data.

[0031] If the amount of data to be transmitted is greater than the maximum packet length supported by the endpoint, the USB host controller will divide the data into multiple bulk packets according to the maximum packet length for transmission. The length of the last bulk transmission can be less than or equal to the maximum packet length.

[0032] like Figure 1 As shown, bulk transmission data stream transmission includes IN transmission and OUT transmission, which correspond to data reading and writing respectively, and it is also divided into three stages, namely token stage, data stage and handshake stage.

[0033] Batch output transactions: The host first sends an OUT token packet, which contains the device address and port number. Then, it sends a DATA packet (based on the data toggle bit, determine the type of DATA packet). At this time, the device whose address and endpoint match will receive this data packet. Then the host switches to the receiving mode and waits for the device to return a handshake packet.

[0034] After the device decodes the token packet and data packet accurately and has enough buffer to save the data, it will use an ACK handshake packet or a NYET handshake packet to respond to the host (only the high-speed mode has a NYET handshake packet, indicating that the data is successfully received this time, but there is no ability to receive the next transmission).

[0035] If there is not enough buffer to save the data, a NAK handshake packet is returned to tell the host that there is no buffer available and the host will retry the bulk output transaction at a later time. If the device detects that the data is correct but the endpoint is in a suspended state, a STALL handshake packet is returned.

[0036] If the device detects an error, it does not respond and lets the host wait until it times out.

[0037] Batch input transactions: The host first sends an IN token packet, which also contains the device address and device number. The host then switches to the data receiving state and waits for the device to return data. If the device detects an error, it does not respond and the host waits for a timeout.

[0038] If there is a device whose address matches the endpoint at this time and no error is detected, the device will respond: if the device has data to return, it will either put a data packet on the bus (the specific data packet type depends on the data toggle bit); if the device has no data to return, it will directly use a NAK handshake packet to respond to the host; if the endpoint is in a suspended state, the device will return a STALL handshake packet.

[0039] If the host receives the data packet sent by the device and decodes it correctly, it responds to the device with an ACK handshake packet.

[0040] If the host detects an error, it will not respond and the device will detect a timeout. The host will not send a NAK handshake packet to refuse to receive a data packet. After receiving the NAK handshake packet, the host knows that the device has no data to return temporarily and will retry the input transaction at a later time.

[0041] For the last IN transaction of a bulk transfer: If the length of the data received by the USB host is less than the maximum packet length supported by the endpoint, the USB host considers that the data has been received. If the length of the data received by the USB host is equal to the maximum packet length supported by the endpoint, an additional packet with 0 data is required to tell the USB host that the data has been received.

[0042] In addition, a PING token packet is added to the USB2.0 high-speed device, which does not send data but directly waits for the device's handshake packet. Therefore, the PING transaction only has a token packet and a handshake packet.

[0043] like Figure 2 As shown, the present application proposes a USB-based communication system, including: a USB device and a USB host.

[0044] USB devices include: USB transmission module, used for batch transmission of communication data between USB host and USB device, realizing data interaction between USB host and USB device; The USB transmission module includes endpoint 1, endpoint 2 and output endpoint; Endpoint 1 is used to transmit the reply packet of the USB device to the USB host; Endpoint 1 has a buffer in the IN direction for caching the reply packet; Endpoint 2 is used to transmit the collected data of the USB device to the USB host; Endpoint 2 has an IN direction buffer for caching the collected data; The output endpoint is used to transmit the command packet of the USB host to the USB device; the output endpoint is provided with a buffer in the OUT direction for caching the command packet; The command packet includes the byte length data of the command packet and the command information in the character string mode; the reply packet includes the reply information in the character string mode.

[0045] In this embodiment, the USB transmission module is provided with EP1 OUT as an output endpoint, EP1 IN as endpoint 1, and EP2IN as endpoint 2. EP1 OUT is provided with a buffer in the OUT direction for caching command packets to prevent command packets from being lost; EP1 IN is provided with a buffer in the IN direction for caching reply packets; EP2 IN is provided with a buffer in the IN direction for caching collected data.

[0046] USB devices also include: The parsing module is used to receive the command packet from the USB host, parse the command packet, and obtain the command information; The execution module is used to execute the command and generate a reply packet according to the command information, output the reply packet to the buffer of endpoint 1; send the acquisition signal to the acquisition module; The acquisition module is used to acquire data and output the acquired data to the buffer of endpoint 2 according to the acquisition signal; or to stop acquiring data according to the acquisition signal.

[0047] In this embodiment, the USB device is provided with a register, and the register is used to store parameters of the USB device.

[0048] In this embodiment, the parameters of the USB device can be read or modified, and the USB device has the function of collecting data.

[0049] The USB host is used to generate command packets, obtain reply packets from the USB device and parse them into reply information, and obtain collected data from the USB device.

[0050] Based on the same inventive concept, the present invention also proposes a USB-based communication method suitable for Figure 2 The communication system shown.

[0051] The USB host sends a command packet and transmits the command packet to the USB device through the output endpoint. The command packet includes the byte length data of the command packet and the command information in string mode. The specific format is as follows:

[0052] Based on the different command information in the command packet, the following two situations may occur: Scenario 1: The USB device receives and parses the command packet, obtains the command information, generates a reply packet, and caches it in the buffer of endpoint 1; wherein the reply packet includes reply information in a string mode; the USB host sends a read request through endpoint 1, obtains the reply packet, and parses it into reply information.

[0053] Among them, generating a reply packet includes: based on the coding table, the USB device parses the command packet into command information in a string mode, and determines whether the command packet is correct. If correct, based on the command information, the instruction is executed, and the instruction execution result value is generated as the reply information; otherwise, an error status value is generated as the reply information.

[0054] Scenario 2: The USB device receives and parses the command packet, obtains the command information, generates a reply packet and collects data; the reply packet is cached in the buffer of endpoint 1, and the collected data is cached in the buffer of endpoint 2; the USB host sends a read request through endpoint 1, obtains the reply packet, and parses it into reply information; the USB host sends a read request through endpoint 2 to obtain the collected data.

[0055] Among them, generating a reply packet and collecting data includes: based on the coding table, the USB device parses the command packet as command information in a string mode, and determines whether the command packet is correct. If correct, based on the command information, the instruction is executed, and the instruction execution result value is generated as the reply information. If the instruction execution result value is a successful execution result value, the collection data is generated or the collection is stopped; if incorrect, an error status value is generated as the reply information.

[0056] In the above two situations, judging whether the command packet is correct includes: judging whether the command packet format is correct and judging whether the command information is legal. If the command packet format is correct and the command information is legal, the command packet is correct, otherwise, it is incorrect.

[0057] Wherein, judging the command packet format includes judging whether the command packet contains byte length data of the command packet and command information of a character string mode; if both exist, judging that the command packet format is correct, otherwise, it is incorrect.

[0058] The specific format of the reply information is as follows:

[0059] The above two situations also include: after receiving the command packet, the USB device sends feedback information to the USB host; the feedback information includes an ACK signal and a NAK signal; and the USB host receives the ACK signal and sends a read request to the USB device. Example 1

[0060] like Figure 3 As shown, this application takes the writing of information to a USB device as an example.

[0061] If the value 0x00000001 is written into the register address 0x11223344 of the USB device, its specific implementation method is explained.

[0062] Step 1: The USB host sends a command packet and transmits the command packet to the USB device through EP1 OUT (output endpoint). The command packet includes the byte length data of the command packet and the command information in string mode.

[0063] The USB host performs command packet assembly work, the content is as follows:

[0064] The string content format of the command information can be customized according to development and debugging requirements. The format shown in the present invention is not the only format. It only requires the USB host and the USB device to maintain a unified convention.

[0065] For example, the character string content of the command information may be "11223344WRTE00000001" or "11223344WRTE00000001", and the same writing meaning as the above example ("WRTE1122334400000001") can also be parsed.

[0066] Based on the encoding table, the byte length data of the command packet and the command information of the string mode are encoded into a command packet and transmitted to the USB device through EP1 OUT (output endpoint). Preferably, an ASCII encoding table or other universal encoding tables can be used, or the encoding table can be set according to user habits.

[0067] This implementation provides an example of information writing to a USB device, which belongs to scenario one.

[0068] Step 2: The USB device receives the command packet and caches it in the EP1 OUT buffer to prevent loss.

[0069] After receiving the command packet, the USB device sends feedback information to the USB host; the feedback information includes ACK signal and NAK signal.

[0070] The parsing module of the USB device parses the command packet into command information in a string mode based on the encoding table.

[0071] The execution module of the USB device determines whether the command packet is correct. If it is correct, it executes the instruction based on the command information and generates the instruction execution result value as the reply information; otherwise, it generates an error status value as the reply information. Based on the encoding table, the reply information is encoded into a reply packet and output to the buffer of EP1 IN (endpoint 1); wherein the reply packet includes the reply information in the string mode.

[0072] The determining whether the command packet is correct includes determining whether the command packet format is correct and determining whether the command information is legal. If the command packet format is correct and the command information is legal, the command packet is correct; otherwise, it is incorrect.

[0073] Wherein, judging the command packet format includes judging whether the command packet contains byte length data of the command packet and command information of a character string mode; if both exist, judging that the command packet format is correct, otherwise, it is incorrect.

[0074] In this embodiment, after the USB device parses and obtains the command information, it determines whether the command packet is correct to determine whether the instruction can be executed based on the command information.

[0075] The specific judgment is as follows: If the command packet format is incorrect, the command packet is incorrect and an error status value is generated as a reply message. If the string content is generated as "2000" (error status value), it means that the command packet does not contain the byte length data of the command packet or the command information of the string mode, or both do not exist.

[0076] If the command packet format is correct, that is, the command packet contains the byte length data of the command packet and the command information in the string mode, then it is determined whether the command information is legal.

[0077] The situations where the command information is illegal include: the byte length of the command information in the byte length of the command packet is incorrect or a parameter value written in the command exceeds the parameter range in the USB device, etc. In this case, the string content is generated as "3000" (error status value) as the reply information.

[0078] If the command packet format is correct and the command information is legal, the command is executed based on the command information and the command execution result value is generated as the reply information.

[0079] Based on the command information, the value 0x00000001 is written into the register address 0x11223344 of the USB device, that is, the command is successfully executed, and the command execution result value "1000" is generated as the reply information. The specific string format is as follows:

[0080] Note: If the length of the encoded string is not a multiple of 4, it will be padded with 0x00 at the end; because the general string ends with NULL, there will be no additional burden on the USB host's parsing work.

[0081] If an unexpected error occurs when executing a USB device command, such as a USB device failure or power outage, and the command fails to be successfully executed, a command execution result value "4000" is generated as a reply message.

[0082] The string content here is only for illustration. The specific string content format can be customized according to development and debugging requirements. The form shown in the present invention is not the only form. It is only necessary for the USB host and USB device to maintain a unified convention.

[0083] Step 3: The USB host receives the ACK signal, initiates a read request to the USB device through EP1 IN, and waits for the USB device to transmit the reply packet to the USB host through EP1 IN.

[0084] The USB host obtains the reply packet and parses the reply packet into reply information in string mode based on the encoding table. Example 2

[0085] like Figure 4 As shown, this application takes the information reading of a USB device as an example.

[0086] If the value of the register address 0x11223344 in the USB device is read, the specific implementation method is explained.

[0087] Step 1: The USB host sends a command packet and transmits the command packet to the USB device through EP1 OUT (output endpoint). The command packet includes the byte length data of the command packet and the command information in string mode.

[0088] The USB host performs command packet assembly work, the content is as follows:

[0089] The string content format of the command information can be customized according to development and debugging requirements. The format shown in the present invention is not the only format. It only requires the USB host and the USB device to maintain a unified convention.

[0090] For example, the command string content of the command information is "11223344READ", which can also be parsed to obtain the same read meaning as the above example ("READ11223344").

[0091] Based on the encoding table, the byte length data of the command packet and the command information of the string mode are encoded into a command packet and transmitted to the USB device through EP1 OUT. Preferably, an ASCII encoding table or other universal encoding tables can be used, or the encoding table can be set according to user habits.

[0092] This implementation provides an example of information reading from a USB device, which belongs to scenario one.

[0093] Step 2: The USB device receives the command packet and caches it in the EP1 OUT buffer to prevent loss.

[0094] After receiving the command packet, the USB device sends feedback information to the USB host; the feedback information includes ACK signal and NAK signal.

[0095] The parsing module of the USB device parses the command packet into command information in string mode.

[0096] The execution module of the USB device determines whether the command packet is correct. If it is correct, it executes the instruction based on the command information and generates the instruction execution result value as the reply information; otherwise, it generates an error status value as the reply information. Based on the encoding table, the reply information is encoded into a reply packet and output to the buffer of EP1 IN (endpoint 1); wherein the reply packet includes the reply information in the string mode.

[0097] The determining whether the command packet is correct includes determining whether the command packet format is correct and determining whether the command information is legal. If the command packet format is correct and the command information is legal, the command packet is correct; otherwise, it is incorrect.

[0098] Wherein, judging the command packet format includes judging whether the command packet contains byte length data of the command packet and command information of a character string mode; if both exist, judging that the command packet format is correct, otherwise, it is incorrect.

[0099] In this embodiment, after the USB device parses and obtains the command information, it determines whether the command packet is correct to determine whether the instruction can be executed based on the command information.

[0100] The specific judgment is as follows: If the command packet format is incorrect, the command packet is incorrect and an error status value is generated as the reply information. If the string content is generated as "2000" (error status value), it means that the command packet does not contain the byte length data of the command packet or the command information of the string mode, or both do not exist.

[0101] If the command packet format is correct, that is, the command packet contains the byte length data of the command packet and the command information in the string mode, then it is determined whether the command information is legal.

[0102] The situations where the command information is illegal include: the byte length of the command information in the byte length of the command packet is incorrect or a certain parameter read by the command does not exist in the USB device, etc. In this case, the string content is generated as "3000" (error status value) as the reply information.

[0103] If the command packet format is correct and the command information is legal, the command is executed based on the command information and the command execution result value is generated as the reply information.

[0104] Based on the command information, read the value of the register address 0x11223344 in the USB device (assuming the register value is 0x00000001), that is, if the command is successfully executed, the register value 0x00000001 is obtained as the command execution result value and as the reply information. The specific contents are as follows:

[0105] Note: If the length of the encoded string is not a multiple of 4, it will be padded with 0x00 at the end; because the general string ends with NULL, there will be no additional burden on the USB host's parsing work.

[0106] If an unexpected error occurs when executing a USB device command, such as a USB device failure or power outage, and the command fails to be successfully executed, a command execution result value "4000" is generated as a reply message.

[0107] The string content here is only for illustration. The specific string content format can be customized according to development and debugging requirements. The form shown in the present invention is not the only form. It is only necessary for the USB host and USB device to maintain a unified convention.

[0108] Step 3: The USB host receives the ACK signal, initiates a read request to the USB device through EP1 IN, and waits for the USB device to transmit the reply packet to the USB host through EP1 IN.

[0109] The USB host obtains the reply packet and parses the reply packet into reply information in string mode based on the encoding table. Example 3

[0110] like Figure 5 As shown, this application takes the data collection and transmission of a USB device as an example to illustrate its specific implementation method.

[0111] Before implementing the method for data acquisition and transmission of a USB device as described in Example 3, the method for writing information to a USB device as described in Example 1 can be used to modify the parameters of the USB device; and / or the method for reading information from a USB device as described in Example 2 can be used to obtain parameter information of the USB device to complete the preparation work for USB device acquisition.

[0112] Alternatively, the implementation methods described in Examples 1 and 2 are not used, and only the implementation method for data acquisition and transmission described in Example 3 is used.

[0113] Assume that the register address for controlling acquisition in the USB device is 0x22446688. When the register value is 1, the USB device performs acquisition and generates acquisition data. When the register value is 0, the USB device stops acquiring.

[0114] If you want to send a command to the USB device to collect data, the command packet content is to write the register value 1 to the register address 0x22446688 in the USB device; if you want to send a command to the USB device to stop collecting data, the command packet content is to write the register value 0 to the register address 0x22446688 in the USB device.

[0115] Step 1: The USB host sends a command packet and transmits the command packet to the USB device through EP1 OUT (output endpoint). The command packet includes the byte length data of the command packet and the command information in string mode.

[0116] The USB host performs command packet assembly, and the command packet content is:

[0117] The string content format of the command information can be customized according to development and debugging requirements. The format shown in the present invention is not the only format. It only requires the USB host and the USB device to maintain a unified convention.

[0118] Based on the coding table, the byte length data of the command packet and the command information of the string mode are encoded into a command packet and transmitted to the USB device through EP1 OUT. Preferably, an ASCII coding table or other universal coding tables can be used, or the coding table can be set according to user habits.

[0119] This implementation provides an example of data collection from a USB device, which belongs to scenario 2.

[0120] Step 2: The USB device receives the command packet and caches it in the EP1 OUT buffer to prevent loss.

[0121] After receiving the command packet, the USB device sends feedback information to the USB host; the feedback information includes ACK signal and NAK signal.

[0122] The parsing module of the USB device parses the command packet into command information in a string mode based on the encoding table.

[0123] The execution module of the USB device determines whether the command packet is correct. If it is correct, it executes the instruction based on the command information and generates an instruction execution result value as reply information. If the instruction execution result value is a successful execution result value, the acquisition module generates acquisition data or stops acquisition; if it is incorrect, an error status value is generated as reply information.

[0124] Based on the encoding table, the reply information is encoded into a reply packet, and the reply packet is output to the buffer of EP1 IN (endpoint 1); wherein the reply packet includes the reply information in the string mode.

[0125] The determining whether the command packet is correct includes determining whether the command packet format is correct and determining whether the command information is legal. If the command packet format is correct and the command information is legal, the command packet is correct; otherwise, it is incorrect.

[0126] Wherein, judging the command packet format includes judging whether the command packet contains byte length data of the command packet and command information of a character string mode; if both exist, judging that the command packet format is correct, otherwise, it is incorrect.

[0127] In this embodiment, after the USB device parses and obtains the command information, it determines whether the command packet is correct to determine whether the instruction can be executed based on the command information.

[0128] The specific judgment is as follows: If the command packet format is incorrect, the command packet is incorrect and an error status value is generated as a reply message. If the string content is generated as "2000" (error status value), it means that the command packet does not contain the byte length data of the command packet or the command information of the string mode, or both do not exist.

[0129] If the command packet format is correct, that is, the command packet contains the byte length data of the command packet and the command information in the string mode, then it is determined whether the command information is legal.

[0130] The situations where the command information is illegal include: the byte length of the command information in the byte length of the command packet is incorrect or a certain parameter read by the command does not exist in the USB device, etc. In this case, the string content is generated as "3000" (error status value) as the reply information.

[0131] If the command packet format is correct and the command information is legal, the command is executed based on the command information and the command execution result value is generated as the reply information.

[0132] Based on the command information, the value 1 is written into the register address 0x22446688 of the USB device, that is, the command is successfully executed, and the command execution result value "1000" is generated as the reply information.

[0133] The specific content of the reply information is as follows:

[0134] Note: If the length of the encoded string is not a multiple of 4, it will be padded with 0x00 at the end; because the general string ends with NULL, there will be no additional burden on the USB host's parsing work.

[0135] If the writing is successful, the USB device execution module sends a collection signal to the collection module, and the collection signal indicates that data collection has started; The acquisition module generates acquisition data according to the acquisition signal and outputs the acquisition data to the buffer of EP2 IN (endpoint 2).

[0136] If the value written successfully is 0, the execution module sends a collection signal, and the collection signal indicates that the collection is stopped.

[0137] The acquisition module stops acquisition according to the acquisition signal.

[0138] If an unexpected error occurs when executing a USB device command, such as a USB device failure or power outage, and the command fails to be successfully executed, a command execution result value "4000" is generated as a reply message.

[0139] The string content here is only for illustration. The specific string content format can be customized according to development and debugging requirements. The form shown in the present invention is not the only form. It is only necessary for the USB host and USB device to maintain a unified convention.

[0140] Step 3: The USB host receives the ACK signal, initiates a read request to the USB device through EP1 IN, and waits for the USB device to transmit the reply packet to the USB host through EP1 IN.

[0141] The USB host obtains the reply packet and parses the reply packet into a string mode reply message based on the encoding table. If the USB host parses the reply message as "1000", it means that the USB device successfully executes the acquisition command.

[0142] The USB host initiates a read request to the USB device through EP2 IN, and waits for the USB device to transfer the collected data to the USB host through EP2 IN.

[0143] The present invention also proposes an electronic device, including a processor and a memory, wherein the memory is used to store a computer program, the computer program includes program instructions, and the processor is configured to call the program instructions to execute the USB-based communication method as described above.

[0144] The present invention further provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the USB-based communication method described above is implemented.

[0145] The technical features of the above embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0146] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A USB-based communication system, comprising: The USB device is characterized in that the USB device comprises: A USB transmission module, including endpoint 1, endpoint 2 and output endpoint, and the USB transmission module is used to realize data interaction between a USB host and a USB device; Endpoint 1 is used to transmit the reply packet of the USB device to the USB host; Endpoint 1 has a buffer in the IN direction for caching the reply packet; Endpoint 2 is used to transmit the collected data of the USB device to the USB host; Endpoint 2 has an IN direction buffer for caching the collected data; The output endpoint is used to transmit the command packet of the USB host to the USB device; the output endpoint is provided with a buffer in the OUT direction for caching the command packet; The command packet includes the byte length data of the command packet and the command information in the character string mode; the reply packet includes the reply information in the character string mode.

2. The USB-based communication system according to claim 1, characterized in that: USB devices also include: The parsing module is used to receive the command packet from the USB host, parse the command packet, and obtain the command information; The execution module is used to execute the command and generate a reply packet according to the command information, output the reply packet to the buffer of endpoint 1; send the acquisition signal to the acquisition module; The acquisition module is used to acquire data and output the acquired data to the buffer of endpoint 2 according to the acquisition signal; or to stop acquiring data according to the acquisition signal.

3. The USB-based communication system according to claim 1, characterized in that: Also includes: The USB host is used to generate command packets, obtain reply packets from the USB device and parse them into reply information, and obtain collected data from the USB device.

4. A communication method based on USB, characterized in that: include: The USB host sends a command packet and transmits the command packet to the USB device through the output endpoint; wherein the command packet includes the byte length data of the command packet and the command information in the string mode; The USB device receives and parses the command packet, obtains command information, generates a reply packet, and caches it in the buffer of endpoint 1; wherein the reply packet includes reply information in a string mode; The USB host sends a read request through endpoint 1, obtains the reply packet, and parses it into reply information.

5. The USB-based communication method according to claim 4, characterized in that: The generating of the reply packet includes: based on the coding table, the USB device parses the command packet into command information in a string mode, determines whether the command packet is correct, and if correct, executes the instruction based on the command information, generates the instruction execution result value as the reply information; otherwise, generates an error status value as the reply information.

6. The USB-based communication method according to claim 4, characterized in that: Also includes: The USB device receives and parses the command packet, obtains the command information, generates a reply packet and collects data; The reply packet is cached in the buffer of endpoint 1, and the collected data is cached in the buffer of endpoint 2; The USB host sends a read request through endpoint 1, obtains the reply packet, and parses it into reply information; The USB host sends a read request through endpoint 2 to obtain the collected data.

7. The USB-based communication method according to claim 6, characterized in that: The generation of the reply packet and the acquisition data includes: based on the coding table, the USB device parses the command packet as command information in a string mode, and determines whether the command packet is correct; if correct, based on the command information, executes the instruction, generates the instruction execution result value as the reply information, and if the instruction execution result value is the execution success result value, generates the acquisition data or stops the acquisition; if incorrect, generates the error status value as the reply information.

8. The USB-based communication method according to claim 5 or 7, characterized in that: Determining whether the command packet is correct includes: determining whether the command packet format is correct and determining whether the command information is legal. If the command packet format is correct and the command information is legal, the command packet is correct; otherwise, it is incorrect. Determining the command packet format includes determining whether the command packet contains byte length data of the command packet and command information in a character string mode.

9. An electronic device, characterized in that: It comprises a processor and a memory, wherein the memory is used to store a computer program, the computer program comprises program instructions, and the processor is configured to call the program instructions to execute the USB-based communication method as described in any one of claims 4-8.

10. A computer-readable storage medium comprising a computer program, characterized in that: When the computer program is executed by a processor, the USB-based communication method as described in any one of claims 4 to 8 is implemented.

Citation Information

Patent Citations

  • A USB DMA data transmission device and method

    CN114741345B

Cited By

  • Printer USB connection method, system and device and storage medium

    CN120892000A