Printer USB connection method, system and device and storage medium
By configuring the printer's USB interface as a standard HID device and transmitting data under the HID protocol framework using a custom data structure, the compatibility and cost issues of printer USB connection are resolved. This enables plug-and-play functionality without the need for driver installation, improving device stability and user experience.
Patent Information
- Application Number
- CN202511404322.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-29
- Publication Date
- 2025-11-04
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Existing printers require specific drivers to be installed when connected via USB, leading to compatibility issues and high costs.
Configure the printer's USB interface as a standard HID device, and utilize the operating system's built-in universal driver, combined with a custom data structure, to distinguish and transmit control commands and printing data within the HID protocol framework, achieving cross-platform plug-and-play functionality without the need to install any dedicated drivers.
It enables plug-and-play functionality without the need for manual driver installation, improving device compatibility and connection stability, reducing development and support costs, and ensuring continuity and stability in complex environments.
Smart Images

Figure CN120892000A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, system, device and storage medium for connecting a printer via USB. Background Technology
[0002] In the field of printer and computer connectivity, the Universal Serial Bus (USB) interface has become one of the most common connection methods due to its plug-and-play and high-speed transmission characteristics. As a critical information output device, the stability and ease of use of a printer's connection directly affect work efficiency and user experience in both office and home environments.
[0003] Currently, when printers connect to computers via USB, the common technical solution is to install a dedicated USB device driver on the computer. This driver acts as a bridge between hardware and software, responsible for converting printing commands issued by the operating system into control signals and data formats that the printer can recognize. Improvements have been proposed in existing technologies, such as pre-storing driver files in the printer's built-in memory, allowing the device to be recognized as a storage device upon connection so that the user can install the driver.
[0004] However, the above solutions still rely on installing and running specific driver software in the host operating system, which still presents compatibility and high cost issues. Summary of the Invention
[0005] The purpose of this application is to provide a printer USB connection method, system, device and storage medium, which aims to solve the problems of compatibility and high cost caused by the need to install specific drivers when connecting a traditional printer via USB.
[0006] To achieve the above objectives, this application adopts the following technical solution: This application provides a method for connecting a printer via USB. The method includes: configuring the printer's USB interface as an HID device that can be recognized by the operating system by setting a USB device descriptor, a USB interface descriptor, a human interface device (HID) descriptor, and an endpoint descriptor, and generating HID configuration information; establishing a driverless connection between the HID device and the computer based on the HID configuration information, and generating a communication-ready state; and based on the communication-ready state and the HID protocol framework, distinguishing between control commands and print data through a custom data structure to realize data interaction between the printer and the computer.
[0007] The printer USB connection method provided in this application configures the printer's USB interface as a standard HID device, enabling mainstream operating systems to directly recognize and prepare the communication foundation using their built-in general drivers. This avoids the need to develop, install, and maintain dedicated drivers for specific systems. The driverless connection established based on this HID configuration achieves a reliable plug-and-play link. Users can complete device mounting without any manual installation, simplifying the usage process and eliminating the risk of connection failure due to driver incompatibility. Furthermore, by leveraging the HID protocol framework and custom data structures to distinguish and transmit control commands and print data, the accuracy and efficiency of command parsing and data exchange are ensured in an environment without the need for additional drivers. Ultimately, this frees the printer from dependence on specific drivers when connecting via USB across platforms, improving device compatibility and connection stability while reducing development and support costs.
[0008] In some embodiments, the above-described method of configuring the printer's USB interface as an HID device recognizable by the operating system and generating HID configuration information by setting a USB device descriptor, a USB interface descriptor, an HID descriptor, and an endpoint descriptor includes: setting a manufacturer identification code and a product identification code in the USB device descriptor to generate unique printer identification information; based on the unique printer identification information, setting the interface class field of the USB interface descriptor to the HID class interface code and setting the interface protocol field to the generic HID protocol code to generate interface configuration information; and setting the description type field of the HID descriptor to a preset type value representing an HID class descriptor according to the interface configuration information to form HID configuration information.
[0009] Based on this, this application provides the operating system with a clear and standard basis for device identification by accurately configuring device identifiers and interface types, ensuring that the printer can be consistently and correctly identified as an HID device by various systems, laying the foundation for subsequent driverless connection.
[0010] In some embodiments, the printer USB connection method provided in this application may further include: configuring at least one input endpoint and at least one output endpoint according to HID configuration information, generating endpoint configuration results, wherein the input endpoint and the output endpoint adopt interrupt transmission type or batch transmission type; wherein the input endpoint is used to receive data from the computer, and the output endpoint is used to send the data processed by the printer to the computer.
[0011] Based on this, this application provides a stable and efficient bidirectional channel for data interaction under the HID protocol framework by flexibly configuring interrupt or batch transmission endpoints, ensuring that printing instructions and status data can be transmitted in a timely and reliable manner, and improving the robustness of the communication process.
[0012] In some embodiments, the above-described data interaction between the printer and the computer, based on the communication readiness state and the HID protocol framework and distinguishing between control commands and print data through a custom data structure, includes: receiving data packets from the computer through an input endpoint based on the endpoint configuration result; parsing the data packets, distinguishing between control command fields and print data fields according to the custom data structure, and generating a parsing result; executing printer operations according to the parsing result, and returning status information to the computer through an output endpoint.
[0013] Based on this, this application achieves intelligent separation and processing of mixed data streams by parsing custom data packets on the basis of driverless connection, thereby accurately executing print control commands and processing print data, ensuring the integrity and reliability of the entire printing task process without a dedicated driver.
[0014] In some embodiments, the parsing of the data packet described above, distinguishing between the control command field and the print data field according to a custom data structure, and generating a parsing result, includes: determining the current transmission status based on the control command field, generating a status determination result, the status determination result including the control command status and the print data status; if the status determination result is the control command status, converting the control command field into an internal control signal of the printer; if the status determination result is the print data status, extracting the print data field as print content data.
[0015] Based on this, this application introduces a state machine mechanism to judge and branch the transmission state, thereby achieving efficient and error-free conversion and extraction of control signals and printing data, ensuring precise synchronization between the printer's internal operation and the host command, and improving data processing efficiency and accuracy.
[0016] In some embodiments, the printer USB connection method provided in this application may further include: monitoring the connection status during data interaction and generating a status monitoring result; if the status monitoring result is abnormal, switching the endpoint transmission type or re-initializing the HID configuration according to the abnormality type and generating a recovery operation instruction; executing the recovery operation instruction to restore data transmission and recording error information.
[0017] Based on this, this application enhances the fault tolerance and self-healing capability of the connection system by monitoring the connection status in real time and automatically triggering a recovery mechanism in case of anomalies, effectively ensuring the continuity and stability of printing tasks in complex usage environments and improving the user experience.
[0018] In some embodiments, the above-mentioned custom data structure includes: a data structure for device information, the data structure including a request type field, a request field, a value field, an index field, and a length field, the value field being used to transmit printer control commands and print data transmission commands.
[0019] Based on this, this application provides an efficient and flexible command and data encoding transmission scheme by defining a structured data format and using specific fields to transmit instructions, which enables complex printer control logic to be implemented on the general HID protocol and simplifies the parsing process.
[0020] This application provides a printer USB connection system, which includes: a generation unit, configured to configure the printer's USB interface as an HID device recognizable by the operating system by setting a USB device descriptor, a USB interface descriptor, an HID descriptor, and an endpoint descriptor, and generate HID configuration information; an establishment unit, configured to establish a driverless connection between the HID device and the computer based on the HID configuration information, and generate a communication-ready state; and a processing unit, configured to distinguish between control commands and print data through a custom data structure based on the communication-ready state and the HID protocol framework, thereby realizing data interaction between the printer and the computer.
[0021] In some embodiments, the above-mentioned generation unit is specifically used to: set a manufacturer identification code and a product identification code in the USB device descriptor to generate printer unique identification information; based on the printer unique identification information, set the interface class field of the USB interface descriptor to HID class interface code and set the interface protocol field to general HID protocol code to generate interface configuration information; and according to the interface configuration information, set the description type field of the HID descriptor to a preset type value representing the HID class descriptor to form HID configuration information.
[0022] In some embodiments, the above-mentioned generating unit is further configured to configure at least one input endpoint and at least one output endpoint according to HID configuration information, and generate endpoint configuration results, wherein the input endpoint and the output endpoint adopt interrupt transmission type or batch transmission type; wherein the input endpoint is used to receive data from the computer, and the output endpoint is used to send the data processed by the printer to the computer.
[0023] In some embodiments, the above processing unit is specifically used to: receive data packets from a computer through an input endpoint based on the endpoint configuration result; parse the data packets, distinguish between control command fields and print data fields according to a custom data structure, and generate a parsing result; perform printer operations according to the parsing result, and return status information to the computer through an output endpoint.
[0024] In some embodiments, the above processing unit is specifically used to: determine the current transmission status based on the control command field, generate a status determination result, the status determination result including the control command status and the print data status; if the status determination result is the control command status, convert the control command field into an internal control signal of the printer; if the status determination result is the print data status, extract the print data field as print content data.
[0025] In some embodiments, the generating unit is further configured to: monitor the connection status during data interaction and generate a status monitoring result; if the status monitoring result is abnormal, switch the endpoint transmission type or re-initialize the HID configuration according to the abnormality type and generate a recovery operation instruction; the processing unit is further configured to execute the recovery operation instruction to restore data transmission and record error information.
[0026] In some embodiments, the above-mentioned custom data structure includes: a data structure for device information, the data structure including a request type field, a request field, a value field, an index field, and a length field, the value field being used to transmit printer control commands and print data transmission commands.
[0027] This application provides an electronic device, including: a processor; a memory for storing processor-executable instructions; wherein the processor is configured to execute instructions to implement the printer USB connection method described above.
[0028] This application provides a computer-readable storage medium storing instructions that, when executed on a terminal, cause the terminal to perform the printer USB connection method described above.
[0029] This application provides a computer program product containing instructions that, when executed by a computer, cause the computer to perform the printer USB connection method described above.
[0030] This application provides a chip including a processor and a communication interface, the communication interface and the processor being coupled together, the processor being used to run computer programs or instructions to implement the printer USB connection method described above.
[0031] Specifically, the chip provided in this application embodiment also includes a memory for storing computer programs or instructions. Attached Figure Description
[0032] To more clearly illustrate the technical solutions of the embodiments of this application, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0033] Figure 1 A flowchart illustrating a printer USB connection method provided in this application embodiment; Figure 2 A structural diagram of a printer USB connection system provided in an embodiment of this application; Figure 3This is a structural diagram of an electronic device provided in an embodiment of this application. Detailed Implementation
[0034] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0035] In the description of this application, it should be understood that the terms "upper," "lower," "left," "right," "front," "rear," "inner," and "outer," etc., indicate the orientation or positional relationship based on the orientation or relative positional relationship shown in the accompanying drawings. They are used only for the convenience of describing this application and for simplification, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this application. Unless otherwise specified, the above-mentioned orientational descriptions can be flexibly set in practical applications, provided that the relative positional relationships shown in the accompanying drawings are satisfied.
[0036] The terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this application, unless otherwise stated, "a plurality of" means two or more.
[0037] In the description of this application, it should be noted that, unless otherwise expressly specified and limited, the terms "installation," "connection," "linking," and "communication" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection. They can refer to a direct connection or an indirect connection through an intermediate medium, or a connection within two components. Those skilled in the art can understand the specific meaning of the above terms in this application according to the specific circumstances.
[0038] In some embodiments, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, article, or apparatus that includes that element.
[0039] In some embodiments, the words "exemplary" or "for example" are used to indicate that something is an example, illustration, or illustration. Any embodiment or design described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of the words "exemplary" or "for example" is intended to present the relevant concepts in a specific manner.
[0040] In the description of this specification, specific features, structures, materials, or characteristics may be combined in any suitable manner in one or more embodiments or examples.
[0041] With the diversification of operating system versions and the continuous updates of computer hardware platforms, ensuring that printers can be quickly and accurately identified and work stably in various complex environments has become a technical issue of ongoing concern in this field.
[0042] Existing printer USB interfaces connect to computers primarily by installing and running specific driver software on the host operating system. This fails to fundamentally eliminate the dependence on drivers and has the following drawbacks: First, due to significant differences in operating system kernel architecture and hardware support mechanisms, developers must develop, test, and maintain drivers for each platform, resulting in high development costs and lengthy development cycles. Second, users often encounter problems such as driver installation failures, version incompatibility, and digital signature errors during actual use, leading to complex installation processes and poor user experience. Furthermore, while the solution of built-in storage for driver files simplifies the acquisition process, it still requires installation steps and may fail in environments with strict access control or offline environments.
[0043] Therefore, there is an urgent need in this field for a printer connection technology that can completely eliminate the driver installation process and achieve seamless cross-platform connectivity.
[0044] Against this backdrop, to address the compatibility and high cost issues arising from the need to install specific drivers when connecting printers via USB in related technologies, this application provides a printer USB connection method, system, device, and storage medium. By configuring the printer's USB interface as a standard HID device and leveraging its inherent advantages of built-in operating system support, combined with a custom data structure to distinguish and transmit control commands and print data within the HID protocol framework, cross-platform plug-and-play printing functionality is achieved without the need to install any dedicated drivers.
[0045] The following is a reference. Figure 1 The printer USB connection method provided in the embodiments of this application is described.
[0046] Figure 1This is a flowchart of a printer USB connection method provided in an embodiment of this application. The subject executing this method can be an electronic device or various devices / modules in the electronic device, such as integrated circuits or chips. This embodiment of the application does not specifically limit this.
[0047] For example, such as Figure 1 As shown, the printer USB connection method provided in this application embodiment may include the following S101 to S103: S101. By setting the USB device descriptor, USB interface descriptor, HID descriptor, and endpoint descriptor, the printer's USB interface is configured as an HID device that can be recognized by the operating system, and HID configuration information is generated.
[0048] In this embodiment, a USB device descriptor is a standardized data structure used to uniquely identify printer hardware devices. It contains core identification information such as device manufacturer information and product model, and is the basis for the operating system to identify the device.
[0049] For example, a common printer model used in office scenarios has a unique vendor identification (VID) and product identification (PID) configured in its USB device descriptor. For instance, the VID is set to 0x5A6B (representing the printer manufacturer), and the PID is set to 0x7C8D (representing the specific printer model). Through this unique combination, the operating system can accurately distinguish this printer from USB devices of other brands or models, avoiding identification confusion.
[0050] In this embodiment, the USB interface descriptor refers to a descriptive structure that defines the function type and communication protocol of the USB interface, and is used to inform the operating system of the interface's function category and interaction rules.
[0051] For example, the interface class field (such as bInterfaceClass) of the USB interface descriptor can be set to 3 (preset HID class interface code) to explicitly identify the interface as an HID class interface; at the same time, the interface protocol field (such as bInterfaceProtocol) can be set to 0 (general HID protocol code) to indicate that it follows the general HID communication protocol; the interface subclass field (such as bInterfaceSubClass) can also be set to 0 (general HID subclass code) to further refine the interface class.
[0052] For example, in a home setting, when a user connects a printer to their home computer, the system can directly identify it as an HID device that supports the universal protocol by reading the interface descriptor, without the need for additional matching of a dedicated driver.
[0053] In this embodiment, the HID descriptor is a structure that specifically describes the characteristics of HID-type devices, including information such as device type identifier and protocol version. It is the key for the operating system to confirm the HID attributes of the device.
[0054] For example, the HID description type field of the HID descriptor can be set to the hexadecimal value 0x21 (preset type value) to explicitly mark it as an HID class descriptor, while also including an HID report descriptor.
[0055] For example, in public printing service scenarios, the printer's HID report descriptor defines the data transmission format, ensuring that mobile devices of different users can parse data according to uniform rules.
[0056] In this embodiment, the endpoint descriptor is a structure that defines the attributes of a USB data transmission endpoint, including information such as endpoint address, transmission type, and maximum data packet length, and is the core of establishing an actual data channel.
[0057] For example, data transmission and reception can be performed at the interrupt endpoint of an HID-type device, configuring at least one input endpoint and one output endpoint. For instance, the input endpoint address of a printer can be set to 0x81, and the output endpoint address can be set to 0x01, with a maximum packet length of 64 bytes for both, ensuring efficient transmission of print commands and data.
[0058] In some embodiments, the manufacturer identification code and product identification code can be set in the USB device descriptor first to generate unique printer identification information.
[0059] For example, when printer manufacturers produce printers, they burn unique VID and PID information into each printer. For instance, VID=0x1122 and PID=0x3344 for high-speed office printers, and VID=0x1122 and PID=0x5566 for portable home printers. The operating system can accurately locate the device model by reading these two identifiers.
[0060] Furthermore, based on the printer's unique identification information, the interface class field of the USB interface descriptor is set to the HID class interface code, and the interface protocol field is set to the general HID protocol code to generate interface configuration information.
[0061] For example, after confirming the printer's unique identifier, the printer firmware can automatically assign the interface class field (such as bInterfaceClass) of the USB interface descriptor to 3 and the interface protocol field (such as bInterfaceProtocol) to 0, ensuring that the operating system can recognize the interface as an HID device that supports the common protocol after reading it.
[0062] Furthermore, based on the interface configuration information, the description type field of the HID descriptor is set to a preset type value representing the HID class descriptor, thus forming the HID configuration information.
[0063] For example, based on the HID attribute configured in the interface, the description type of the HID descriptor can be set to 0x21, and the HID protocol version (such as 0x0100, representing the HID 1.0 protocol) can be written, so that the operating system can confirm that the device conforms to the standard HID specification, thereby calling the built-in general HID driver.
[0064] Thus, by accurately configuring device identifiers and interface types, this application provides the operating system with a clear and standard basis for device identification, ensuring that the printer can be consistently and correctly identified as an HID device by various systems, laying a solid foundation for subsequent driverless connection.
[0065] S102. Based on the HID configuration information, establish a driverless connection between the HID device and the computer, and generate a communication-ready state.
[0066] In some embodiments, when a user connects a printer configured according to the above steps to a computer via a USB cable, the computer's USB host controller detects the new device connection and begins the enumeration process. The operating system's USB kernel driver stack reads the device descriptor, configuration descriptor, interface descriptor, HID descriptor, and endpoint descriptor in sequence. When it recognizes the interface type field (e.g., bInterfaceClass=0x03) and descriptor type field (e.g., bDescriptorType=0x21) in the HID descriptor, it automatically binds the built-in generic HID class driver to the device interface.
[0067] For example, in Device Manager, the HID device will be displayed under the "Human Interface Devices" category as "HID-Compatible Device" or a similar name. At this time, the operating system will allocate the necessary system resources (such as device objects and pipeline bandwidth) to the device and provide the application programming interface (API) to the application to access the HID device, indicating that the driverless connection has been successfully established, the communication link has entered a ready state, and data interaction can begin.
[0068] Specifically, in an office environment, after connecting the printer to the computer via USB, the system can automatically read the HID configuration information, recognize it as an HID device, directly call the built-in driver, complete the initialization, and the Task Manager will display "USB Human Interface Device" normally connected, generating a "Communication Ready" status.
[0069] In a home environment, when the printer is connected to the laptop via USB, no driver installation is required, and the printer will be displayed directly in System Preferences as connected.
[0070] S103. Based on the communication ready state and HID protocol framework, control commands and printing data are distinguished through a custom data structure to realize data interaction between the printer and the computer.
[0071] In this embodiment, the custom data structure includes a data structure for device information. This data structure includes a request type field, a request field, a value field, an index field, and a length field.
[0072] For example, a device information (such as DEVICE_INFO) structure can be defined to encapsulate the communication protocol. This structure mimics the data structure of a USB control transfer request, but it transmits data via HID reports or interrupt / bulk transfer endpoints. Its member variables include: USBbmRequestType(uint8_t): The request type field (bmRequestType) defines the characteristics of the request (data transfer direction, request type, receiver). For example, it can be fixed to 0xC0, indicating that this is a device-to-host, vendor-specific request with the device as the receiver.
[0073] USBbRequest(uint8_t): The request field (bRequest) specifies the specific request code. For example, it can be fixed at 0x40, representing a manufacturer-defined input request initiated by the host to obtain device information.
[0074] USBwValues(uint16_t): Value field (wValue), the meaning of which varies depending on the specific request. In this solution, this field is given a core function: to transmit specific printer control commands (such as printing, paper feeding, setting parameters) or as a mode switching instruction (such as indicating that subsequent data is pure print data).
[0075] USBwIndex(uint16_t): Index field (wIndex), commonly used to specify the index of an interface or endpoint, or its low byte can be expanded into a subcommand word or parameter.
[0076] USBwLength(uint16_t): The length field (wLength) indicates the length of data the host expects to receive from the device during the data phase, or the length of data the host wants to send in the setup request.
[0077] In this embodiment, the value field is used to transmit printer control commands and print data transmission commands.
[0078] For example, host computer software (such as a computer-side application) can send a setup report request via the HID API, or send a data packet containing a structure containing device information (such as DEVICE_INFO) on an interrupt output / bulk output endpoint. Different functions can be achieved by assigning different predefined values to the USB value field.
[0079] For example, assigning a value to the start print command (such as CMD_PRINT_START: 0x0001) indicates starting a print job; assigning a value to the paper feed command (such as CMD_FEED_PAPER: 0x0002) indicates controlling the printer to feed one line of paper; assigning a value to the set resolution command (such as CMD_SET_RESOLUTION: 0x0003) indicates setting the print resolution. In this case, the low byte of the USB index field can be used to pass resolution parameters (such as 300 DPI or 600 DPI).
[0080] For example, when assigning a data mode command (such as CMD_DATA_MODE: 0x00A0), it means that the printer firmware is notified that the data transmitted immediately following this request (or in the data phase of this request) is no longer a structured command, but raw print data (print font) that needs to be sent directly to the print engine.
[0081] Furthermore, the printer firmware performs corresponding operations or switches states based on the value of this field.
[0082] Thus, by defining a structured data format and using specific fields to transmit instructions, this application provides an efficient and flexible command and data encoding transmission scheme, enabling complex printer control logic to be implemented on the general HID protocol and simplifying the parsing process.
[0083] Optionally, after generating the HID configuration information, at least one input endpoint and at least one output endpoint can be configured based on the HID configuration information to generate endpoint configuration results.
[0084] In this embodiment, both the input endpoint and the output endpoint can be of interrupted transfer type or bulk transfer type.
[0085] For example, the interrupted transmission type refers to a transmission method that guarantees maximum latency and is suitable for periodic, small-batch data transmission. The host periodically queries the device endpoint at intervals (in milliseconds) no greater than the interval time (such as bInterval) field specified in the endpoint descriptor to check if there is data to be transmitted.
[0086] For example, configure an interrupt input endpoint (e.g., address 0x81), an attribute bitmask field (e.g., bmAttributes=0x03, indicating interrupt transmission), a maximum packet size field (e.g., wMaxPacketSize=8), and an interval field (e.g., bInterval=10). This endpoint can be used by the printer to periodically (approximately every 10ms) report its status information to the host, such as "Busy," "Paper Out," or "Error." Interrupted transmission ensures that status information is received by the host with a predictable and relatively short delay.
[0087] For example, bulk transfer type refers to a transfer method used to transfer large amounts of data with guaranteed accuracy, but without guaranteeing latency or speed. It utilizes the remaining bandwidth on the USB bus for transmission.
[0088] For example, configure the bulk output endpoint (e.g., address 0x02), attribute bitmask field (e.g., bmAttributes=0x02, indicating bulk transfer), and maximum packet size field (e.g., wMaxPacketSize=64). This endpoint is suitable for sending large numbers of print packets from the host to the printer. Data transfer is fast when the USB bus is idle; when the bus is busy, transfer may be temporarily delayed until bandwidth becomes available. Bulk transfer ensures the integrity of print data and is suitable for non-real-time, high-volume data transfer scenarios.
[0089] In this embodiment, the input endpoint is used to receive data from the computer, and the output endpoint is used to send the data processed by the printer to the computer.
[0090] Specifically, the typical addresses of input endpoints are 0x81, 0x82, etc. (with the highest bit being 1), and they are used by the printer firmware to send data to the computer. For example, sending printer status reports, response data to specific commands, etc. The typical addresses of output endpoints are 0x01, 0x02, etc. (with the highest bit being 0), and they are used to receive data from the computer. For example, receiving print control commands and print data content.
[0091] Thus, by flexibly configuring interrupt or batch transmission endpoints, this application provides a stable and efficient bidirectional channel for data interaction under the HID protocol framework, ensuring that printing commands and status data can be transmitted in a timely and reliable manner, thereby improving the robustness of the communication process.
[0092] In some embodiments, based on the endpoint configuration results, data packets from the computer can be received through the input endpoint, and the data packets can be parsed. The control command field and the print data field can be distinguished according to the custom data structure to generate the parsing result.
[0093] For example, the USB interrupt service routine (ISR) or bulk transfer data processing function in the printer firmware monitors the receive buffer of the configured output endpoint (such as address 0x01). When data arrives, the printer firmware reads the data into the memory buffer. Then, the printer firmware checks whether the beginning of the data in the buffer conforms to the format of the predefined DEVICE_INFO structure.
[0094] Specifically, the USB request type field and USB request field can be parsed to verify whether they match the expected values (such as 0xC0 and 0x40) to confirm whether it is a valid custom command packet; then, the value of the USB value field is parsed: if the value is a predefined control command (such as CMD_SET_RESOLUTION), the current data packet is determined to be a control command packet; if the value is a mode switching command (such as CMD_DATA_MODE), the data that follows (which may be in the same packet or a subsequent packet) is determined to be print data.
[0095] Furthermore, the printer operation is performed based on the parsing results, and status information is returned to the computer through the output endpoint.
[0096] In one example, if the parsing result indicates a resolution setting command (such as CMD_SET_RESOLUTION), the printer firmware calls the internal print parameter setting function, configures the relevant registers of the print engine according to the resolution parameter (such as 300) passed by the USB index field, and then organizes a response packet that follows the device information structure. The response packet sets the USB value field to a success status (such as STATUS_SUCCESS: 0x9000) to indicate success, or sets the USB value field to an error status (such as STATUS_ERROR: 0xF000) to indicate failure, and sends it back to the host computer software through the configured input endpoint (such as interrupt input endpoint 0x81).
[0097] In another example, if the parsing result indicates entry into data mode, the printer firmware switches its internal state machine to the data receiving state, preparing to receive subsequent print data packets and send them to the print buffer, while returning a data ready state (such as STATUS_READY_FOR_DATA).
[0098] Thus, this application achieves intelligent separation and processing of mixed data streams by parsing custom data packets on the basis of driverless connection, thereby accurately executing print control commands and processing print data, ensuring the integrity and reliability of the entire printing task process without a dedicated driver.
[0099] Optionally, the above parsing of data packets, which distinguishes between control command fields and print data fields according to a custom data structure, and generates parsing results, may include: determining the current transmission status based on the control command fields and generating a status determination result.
[0100] In this embodiment of the application, the status judgment result includes the control command status and the print data status.
[0101] In practice, the printer firmware maintains global state variables, such as the current transmission status. When a data packet is parsed and its USB value field is a control command (such as the command to start printing (e.g., CMD_PRINT_START) or the command to feed paper (e.g., CMD_FEED_PAPER)), the status judgment result is the control command status, indicating that the printer is currently in the command receiving and processing stage.
[0102] When the parsed USB value field is a command data mode (such as CMD_DATA_MODE), the status judgment result is a print data status, indicating that subsequent data streams transmitted via USB should be interpreted as pure print data until another explicit command is received to terminate the data mode.
[0103] In one alternative implementation, if the status determination result is a control command status, the control command field is converted into an internal control signal of the printer.
[0104] For example, in control command mode, the printer firmware queries a built-in command mapping table based on the specific values of the parsed USB request field and USB value field. This command mapping table maps different command values to corresponding internal processing functions or hardware register operations.
[0105] For example, the command to feed paper (such as CMD_FEED_PAPER) in the USB value field is mapped to the Printer_FeedLine() function. Executing this function can generate specific timing signals or set specific motor control register bits, ultimately driving the stepper motor to perform the paper feed action, thus translating the command field into internal printer control signals and behavior.
[0106] In another optional implementation, if the status judgment result is "print data status", the print data field is extracted as the print content data.
[0107] For example, in print data mode, the data received by the printer firmware from the USB output endpoint is continuous, raw data that needs to be printed. The printer firmware's workflow becomes: extracting data from the USB buffer, performing necessary format conversions or buffering, and writing the data to the printhead's first-in-first-out (FIFO) buffer or the hardware register responsible for direct memory access (DMA); then, the print engine automatically reads this data from the buffer and controls the printhead to print. At this point, the data fields are extracted as print content without the need for a complex command parsing process.
[0108] Thus, by introducing a state machine mechanism to judge and branch the transmission state, this application achieves efficient and error-free conversion and extraction of control signals and printing data, ensuring precise synchronization between the printer's internal operation and the host command, and improving data processing efficiency and accuracy.
[0109] In the printer USB connection method provided in this application embodiment, by configuring the printer's USB interface as a standard HID device, mainstream operating systems can directly recognize and prepare the communication foundation using their built-in general drivers, avoiding the need to develop, install, and maintain dedicated drivers for specific systems. The driverless connection established based on this HID configuration realizes a reliable plug-and-play link, allowing users to complete device mounting without any manual installation, simplifying the usage process and eliminating the risk of connection failure due to driver incompatibility. Furthermore, by leveraging the HID protocol framework and custom data structures to distinguish and transmit control commands and print data, the accuracy and efficiency of command parsing and data exchange are ensured in an environment without additional drivers. Ultimately, this frees the printer from dependence on specific drivers when connecting via USB across platforms, improving device compatibility and connection stability, while reducing development and support costs.
[0110] Optionally, after returning status information to the computer through the output endpoint, the printer USB connection method provided in this application embodiment can also monitor the connection status during data interaction and generate status monitoring results.
[0111] For example, the printer firmware can perform a connection health check periodically (e.g., every 100ms). Specifically, this can be achieved in two ways: 1. Check the bus voltage (VBUS) detection bit in the USB controller status register to confirm whether the physical connection is in place. If VBUS disappears, a "physical disconnection" status monitoring result is generated.
[0112] II. Monitor whether start-of-frame (SOF) packets arrive periodically to determine if bus activity is normal; alternatively, at the application layer, track the timestamp of the last successful reception of a valid host data packet or successful transmission of a status report to determine if a communication timeout has occurred. If no bus activity is detected or no valid data is received for a predetermined time (e.g., 500ms), a "communication timeout" status monitoring result is generated.
[0113] Furthermore, in the event of an abnormal status monitoring result, the endpoint transmission type is switched or the HID configuration is reinitialized according to the type of abnormality, a recovery operation command is generated and executed to restore data transmission, and error information is recorded.
[0114] In one example, if the status monitoring result is "communication timeout" and the interrupted transmission endpoint is currently in use, the recovery operation instruction could be: try to switch the transmission type of the relevant endpoint from interrupted transmission (which may have caused query loss due to excessive system load) to batch transmission.
[0115] Specifically, the attribute bitmask field in the endpoint descriptor can be modified via software (e.g., bmAttributes can be changed from 0x03 to 0x02), which then triggers a soft reset or reconnection sequence for the USB interface, forcing the host to re-enumerate the device and configure it according to the new endpoint attributes.
[0116] In another example, if the status monitoring result indicates an enumeration failure or configuration error, the recovery operation instruction could be to reinitialize the entire USB peripheral controller and its associated descriptor tables, restarting the enumeration process from scratch. Simultaneously, the printer firmware records the error type, the triggered recovery action, and the timestamp in the error log area of the printer's internal non-volatile memory.
[0117] In addition, if communication is restored, error codes or log indexes can be selectively reported to the host application via the input endpoint for users or technicians to diagnose the problem.
[0118] Thus, by monitoring the connection status in real time and automatically triggering a recovery mechanism in case of anomalies, this application significantly enhances the fault tolerance and self-healing capability of the connection system, effectively ensuring the continuity and stability of printing tasks in complex usage environments and improving the user experience.
[0119] The foregoing mainly describes the solutions provided by the embodiments of this application from a methodological perspective. To achieve the above functions, the printer USB connection system or electronic device includes corresponding hardware structures and / or software modules for performing each function. Those skilled in the art should readily recognize that, based on the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein, this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0120] This application embodiment can, according to the above method, exemplarily divide a printer USB connection system or electronic device into functional modules. For example, the printer USB connection system or electronic device may include functional modules corresponding to each functional division, or two or more functions may be integrated into one processing module. The integrated module can be implemented in hardware or as a software functional module. It should be noted that the module division in this application embodiment is illustrative and only represents one logical functional division; in actual implementation, there may be other division methods.
[0121] Figure 2 This is a structural diagram of a printer USB connection system provided in an embodiment of this application. The printer USB connection system 200 includes: a generation unit 201, an establishment unit 202, and a processing unit 203.
[0122] The printer is configured as an HID device recognizable by the operating system by setting the USB device descriptor, USB interface descriptor, HID descriptor, and endpoint descriptor, and generating HID configuration information. The establishment unit 202 is used to establish a driverless connection between the HID device and the computer based on the HID configuration information and generate a communication-ready state. The processing unit 203 is used to distinguish between control commands and print data through a custom data structure based on the communication-ready state and the HID protocol framework, thereby realizing data interaction between the printer and the computer.
[0123] In some embodiments, the generation unit 201 is specifically used to: set a manufacturer identification code and a product identification code in the USB device descriptor to generate printer unique identification information; based on the printer unique identification information, set the interface class field of the USB interface descriptor to HID class interface code and set the interface protocol field to general HID protocol code to generate interface configuration information; and according to the interface configuration information, set the description type field of the HID descriptor to a preset type value representing the HID class descriptor to form HID configuration information.
[0124] In some embodiments, the generation unit 201 is further configured to configure at least one input endpoint and at least one output endpoint according to HID configuration information, and generate endpoint configuration results, wherein the input endpoint and the output endpoint adopt interrupt transmission type or batch transmission type; wherein the input endpoint is used to receive data from the computer, and the output endpoint is used to send the data processed by the printer to the computer.
[0125] In some embodiments, the processing unit 203 is specifically used to: receive data packets from a computer through an input endpoint based on the endpoint configuration result; parse the data packets, distinguish between control command fields and print data fields according to a custom data structure, and generate a parsing result; perform printer operations according to the parsing result, and return status information to the computer through an output endpoint.
[0126] In some embodiments, the processing unit 203 is specifically configured to: determine the current transmission status based on the control command field, generate a status determination result, the status determination result including the control command status and the print data status; if the status determination result is the control command status, convert the control command field into an internal control signal of the printer; if the status determination result is the print data status, extract the print data field as print content data.
[0127] In some embodiments, the generation unit 201 is further configured to: monitor the connection status during data interaction and generate a status monitoring result; if the status monitoring result is abnormal, switch the endpoint transmission type or re-initialize the HID configuration according to the abnormality type and generate a recovery operation instruction; the processing unit is further configured to execute the recovery operation instruction to restore data transmission and record error information.
[0128] In some embodiments, the above-mentioned custom data structure includes: a data structure for device information, the data structure including a request type field, a request field, a value field, an index field, and a length field, the value field being used to transmit printer control commands and print data transmission commands.
[0129] In the printer USB connection system provided in this application embodiment, by configuring the printer's USB interface as a standard HID device, mainstream operating systems can directly recognize and prepare the communication foundation using their built-in general drivers, avoiding the need to develop, install, and maintain dedicated drivers for specific systems. The driverless connection established based on this HID configuration realizes a reliable plug-and-play link, allowing users to mount the device without any manual installation, simplifying the usage process and eliminating the risk of connection failure due to driver incompatibility. Furthermore, by leveraging the HID protocol framework and custom data structures to distinguish and transmit control commands and print data, the accuracy and efficiency of command parsing and data exchange are ensured in an environment without the need for additional drivers. Ultimately, this frees the printer from dependence on specific drivers when connecting via USB across platforms, improving device compatibility and connection stability, while reducing development and support costs.
[0130] Regarding the system in the above embodiments, the specific ways in which each module performs operations have been described in detail in the embodiments related to the method, and will not be elaborated here.
[0131] Figure 3 This is a structural diagram of an electronic device provided in an embodiment of this application. Figure 3 As shown, the electronic device 300 includes, but is not limited to, a processor 301 and a memory 302.
[0132] The memory 302 described above is used to store the executable instructions of the processor 301. It is understood that the processor 301 is configured to execute instructions to implement the printer USB connection method in the above embodiment.
[0133] It should be noted that those skilled in the art will understand that Figure 3 The electronic device structure shown does not constitute a limitation on the electronic device; the electronic device may include, but is not limited to, other electronic devices. Figure 3 This may indicate more or fewer components, or combinations of certain components, or different component arrangements.
[0134] Processor 301 is the control center of the electronic device. It connects various parts of the electronic device via various interfaces and lines. By running or executing software programs and / or modules stored in memory 302, and by calling data stored in memory 302, it performs various functions and processes data, thereby providing overall monitoring of the electronic device. Processor 301 may include one or more processing units. Optionally, processor 301 may integrate an application processor and a modem processor. The application processor mainly handles the operating system, user interface, and applications, while the modem processor mainly handles wireless communication. It is understood that the modem processor may not be integrated into processor 301.
[0135] The memory 302 can be used to store software programs and various data. The memory 302 may mainly include a program storage area and a data storage area, wherein the program storage area may store the operating system, application programs required by at least one functional module (such as a determination unit, a processing unit, etc.), etc. In addition, the memory 302 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0136] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory 302 including instructions, which can be executed by a processor 301 of an electronic device 300 to implement the printer USB connection method in the above embodiments.
[0137] In actual implementation, Figure 2 The steps performed by the generation unit 201, the establishment unit 202, and the processing unit 203 can all be performed by... Figure 3 The processor 301 calls the computer program stored in the memory 302 to implement the process. The specific execution process can be found in the method section of the previous embodiment, and will not be repeated here.
[0138] Optionally, the computer-readable storage medium may be a non-transitory computer-readable storage medium, such as a read-only memory (ROM), random access memory (RAM), CD-ROM, magnetic tape, floppy disk, and optical data storage device.
[0139] In an exemplary embodiment, this application also provides a computer program product including one or more instructions, which can be executed by the processor 301 of an electronic device to complete the printer USB connection method in the above embodiments.
[0140] It should be noted that when one or more instructions in the computer-readable storage medium or computer program product are executed by the processor of the electronic device, they implement the various processes of the above method embodiments and achieve the same technical effect as the above method. To avoid repetition, they will not be described again here.
[0141] Through the above description of the implementation methods, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the system can be divided into different functional modules to complete all or part of the functions described above.
[0142] In the embodiments provided in this application, it should be understood that the disclosed systems and methods can be implemented in other ways. For example, the system embodiments described above are merely illustrative; for instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection of systems or units may be electrical, mechanical, or other forms.
[0143] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units; that is, it can be located in one place or distributed in multiple different locations. Some or all of the constituent units can be selected to achieve the purpose of this embodiment, depending on actual needs.
[0144] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0145] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiments of this application, essentially, or the part that contributes to the prior art, or a complete or partial classification of the technical solution, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, ROM, RAM, magnetic disks, or optical disks.
[0146] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A printer USB connection method, characterized in that, The method includes: By setting the Universal Serial Bus (USB) device descriptor, USB interface descriptor, HID device descriptor, and endpoint descriptor, the printer's USB interface is configured as an HID device that can be recognized by the operating system, and HID configuration information is generated. Based on the HID configuration information, a driverless connection is established between the HID device and the computer, and a communication-ready state is generated. Based on the communication ready state and HID protocol framework, a custom data structure is used to distinguish between control commands and print data, thereby enabling data interaction between the printer and the computer.
2. The printer USB connection method according to claim 1, characterized in that, The process involves configuring the printer's USB interface as an HID device recognizable by the operating system by setting the Universal Serial Bus (USB) device descriptor, USB interface descriptor, HID device descriptor, and endpoint descriptor, and generating HID configuration information, including: Set the manufacturer identification code and product identification code in the USB device descriptor to generate unique printer identification information; Based on the printer's unique identifier information, the interface class field of the USB interface descriptor is set to the HID class interface code, and the interface protocol field is set to the general HID protocol code to generate interface configuration information; Based on the interface configuration information, the description type field of the HID descriptor is set to a preset type value representing the HID class descriptor, thus forming the HID configuration information.
3. The printer USB connection method according to claim 2, characterized in that, The method further includes: Based on the HID configuration information, configure at least one input endpoint and at least one output endpoint, and generate endpoint configuration results. The input endpoint and the output endpoint adopt interrupted transmission type or bulk transmission type. The input endpoint is used to receive data from the computer, and the output endpoint is used to send the data processed by the printer to the computer.
4. The printer USB connection method according to claim 3, characterized in that, Based on the communication ready state and the HID protocol framework, the system distinguishes between control commands and print data through a custom data structure to achieve data interaction between the printer and the computer, including: Based on the endpoint configuration result, data packets from the computer are received through the input endpoint; The data packet is parsed, and the control command field and print data field are distinguished according to the custom data structure to generate the parsing result; The printer operation is performed based on the parsing result, and status information is returned to the computer through the output endpoint.
5. The printer USB connection method according to claim 4, characterized in that, The process of parsing the data packet involves distinguishing between control command fields and print data fields based on the custom data structure, and generating parsing results, including: The current transmission status is determined based on the control command field, and a status determination result is generated, which includes the control command status and the printed data status. If the status determination result is the control command status, the control command field is converted into an internal control signal of the printer; If the status determination result is the print data status, the print data field is extracted as the print content data.
6. The printer USB connection method according to claim 4, characterized in that, The method further includes: Monitor connection status during data interaction and generate status monitoring results; In the event of an abnormal status monitoring result, the endpoint transmission type is switched or the HID configuration is reinitialized according to the type of abnormality, and a recovery operation command is generated. The recovery operation command is executed to restore data transmission and error information is recorded.
7. The printer USB connection method according to any one of claims 1 to 6, characterized in that, The custom data structure includes: a device information data structure, which includes a request type field, a request field, a value field, an index field, and a length field. The value field is used to transmit printer control commands and print data transmission commands.
8. A printer USB connection system, characterized in that, The system includes: The generation unit is used to configure the printer's USB interface as an HID device that can be recognized by the operating system by setting the USB device descriptor, USB interface descriptor, HID descriptor and endpoint descriptor, and to generate HID configuration information. The establishment unit is used to establish a driverless connection between the HID device and the computer based on the HID configuration information and generate a communication-ready state; The processing unit is used to distinguish between control commands and print data through a custom data structure based on the communication ready state and the HID protocol framework, thereby realizing data interaction between the printer and the computer.
9. An electronic device, characterized in that, include: processor; Memory used to store the processor's executable instructions; The processor is configured to execute the instructions to implement the printer USB connection method according to any one of claims 1 to 7.
10. A computer-readable storage medium storing instructions, characterized in that, When the computer executes the instruction, the computer performs the printer USB connection method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Printer
CN103299266A
Optical communication apparatus interface accessing method free of driver based on HID
CN104991870A
Method and device for driving Bluetooth equipment
CN108334339A
Method for realizing driving-free printing by using HID equipment in USB printing equipment
CN112698795A
Communication system and method based on USB, electronic equipment and medium
CN118939590A
Cited By
Printing method and printer supporting local multi-mode switching and cloud verification
CN122018829A
A printing method and a printer supporting local multi-mode switching and cloud revocation
CN122018829B
