A method and terminal for reconnecting and replacing USB based on Linux system
By recording USB configuration information and matching device information in the Linux system, USB devices can be automatically reconnected, solving the reconnection failure problem caused by device node name changes and achieving efficient and flexible USB device reconnection.
Patent Information
- Application Number
- CN202411224888.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-29
- Publication Date
- 2025-09-30
- Estimated Expiration
- 2044-05-29
AI Technical Summary
In Linux systems, the device node name changes after a USB device is reconnected, making it impossible to ensure the consistency of the device and the communication path. Existing methods are inefficient and prone to reconnection failures.
By recording the USB configuration information that has been connected to the Linux system, monitoring the insertion event and matching the device information, it automatically reconnects if they are consistent and updates the device information to maintain the continuity of the communication path and avoid manual intervention and confirmation.
Improves the flexibility and efficiency of USB device reconnection, avoids manual confirmation steps, and ensures the accuracy and efficiency of device reconnection.
Smart Images

Figure CN119127770B_ABST
Abstract
Description
[0001] This case is a divisional application based on the invention patent with application date of 2024-05-29, application number 202410675232.0, and name “A method and terminal for reconnecting USB based on Linux system” as the parent case. Technical Field
[0002] The present invention relates to the field of data processing, and in particular to a method and a terminal for reconnecting a USB based on a Linux system. Background Art
[0003] In Linux, plugging and unplugging USB devices will cause the USB device names in the system to change. Generally speaking, the Linux kernel assigns numbers to devices based on the order in which they are plugged in. For example, the first device plugged in is numbered ttyUSB0, the second device plugged in is numbered ttyUSB1, and so on.
[0004] When you restart the Linux system or reconnect a USB device, the Linux kernel reassigns device numbers to each device, changing the original device numbers. There's no guarantee that device A's device node name will be ttyUSB0, while device B's device node name will be ttyUSB1. The device file directory doesn't provide a fixed correspondence between USB devices and device node names. This means that after plugging in a USB device, the communication path to the port it's plugged into cannot be guaranteed to match the target path it's plugged into. Therefore, using only the device node name to identify a specific device can easily cause confusion in programs. In Linux, programs require fixed device node names to communicate with USB devices.
[0005] To obtain the specific node name of a USB device, the common practice is to check whether there is a new node name in the device file directory when the USB device is inserted to determine the device name. However, this process usually requires manual intervention, is inefficient, and has cumbersome steps. Alternatively, udev rules can be used to write USB device insertion and removal events, and customize USB device aliases based on device attributes such as the manufacturer ID, product ID, and serial number. This alias makes the device name unique. However, due to differences in USB manufacturers, it is often impossible to obtain one or more of the manufacturer ID, product ID, and serial number, making it impossible to generate a unique alias, resulting in deviations and reconnection failures. Summary of the Invention
[0006] The technical problem to be solved by the present invention is to provide a method and terminal for reconnecting a USB device based on a Linux system, so as to solve the problem of reconnection failure after a USB device is inserted again.
[0007] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0008] A method for reconnecting a USB based on a Linux system, comprising the steps of:
[0009] S1. Obtain the configuration information of the USB connected to the Linux system;
[0010] S2, monitor the event of USB insertion in Linux system and record the device information of USB insertion;
[0011] S3, matching the configuration information with the device information of the USB device, and if they are consistent, re-establishing the communication connection between the USB device and the program in the system according to the configuration information;
[0012] S4. When an insertion event is detected and the device information of the inserted USB device except the serial number information is consistent with the device information of a certain USB device, it is determined to be a replacement of the same type of USB device, and the device information of this USB is updated to the configuration information of the replaced USB and associated with the communication path of the replaced USB.
[0013] In order to solve the above technical problems, another technical solution adopted by the present invention is:
[0014] A Linux-based USB reconnection terminal includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the processor performs the following steps:
[0015] S1. Obtain the configuration information of the USB connected to the Linux system;
[0016] S2, monitor the event of USB insertion in Linux system and record the device information of USB insertion;
[0017] S3, matching the configuration information with the device information of the USB device, and if they are consistent, re-establishing the communication connection between the USB device and the program in the system according to the configuration information;
[0018] S4. When an insertion event is detected and the device information of the inserted USB device except the serial number information is consistent with the device information of a certain USB device, it is determined to be a replacement of the same type of USB device, and the device information of this USB is updated to the configuration information of the replaced USB and associated with the communication path of the replaced USB.
[0019] The beneficial effects of the present invention include at least: providing a method and terminal for reconnecting a USB based on a Linux system, by recording the configuration information of a USB that has been connected to the Linux system and matching it with the device information inserted into the USB. If the matching consistency is passed, the method automatically reconnects with the program in the system according to the original configuration information, avoiding manual intervention and confirmation before reconnection. At the same time, there is no need to obtain all the device information of the USB for identification, and matching can be completed only when the previous and subsequent device information are consistent, thereby improving the flexibility and efficiency of USB device reconnection. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 This is a flowchart of a method for reconnecting USB based on a Linux system in an embodiment of the present invention;
[0021] Figure 2 This is a specific flow chart of a method for reconnecting USB based on a Linux system in an embodiment of the present invention;
[0022] Figure 3 Schematic diagram of a terminal for reconnecting USB based on a Linux system in an embodiment of the present invention;
[0023] Description of labels:
[0024] 1. A method for reconnecting USB based on Linux system; 2. Memory; 3. Processor. DETAILED DESCRIPTION
[0025] To illustrate the technical content, achieved objectives and effects of the present invention in detail, the following description is given in conjunction with the embodiments and accompanying drawings.
[0026] Please refer to Figures 1 to 2 , a method for reconnecting USB based on Linux system, comprising the steps of:
[0027] S1. Obtain configuration information of a USB device connected to a Linux system; the configuration information includes USB device information and a communication path of the USB device in the Linux system;
[0028] S2, monitor the event of USB insertion in Linux system and record the device information of USB insertion;
[0029] S3, matching the configuration information with the device information of the USB device, and if they are consistent, re-establishing the communication connection between the USB device and the program in the system according to the configuration information;
[0030] S4. When an insertion event is detected and the device information of the inserted USB device except the serial number information is consistent with the device information of a certain USB device, it is determined to be a replacement of the same type of USB device, and the device information of this USB is updated to the configuration information of the replaced USB and associated with the communication path of the replaced USB.
[0031] From the above description, it can be seen that the beneficial effect of the present invention is that: by recording the configuration information of the USB that has been connected to the Linux system and matching it with the device information inserted into the USB, if the matching consistency is passed, it will automatically reconnect with the program in the system according to the original configuration information, avoiding human intervention and confirmation before reconnection. At the same time, there is no need to obtain all the device information of the USB for identification, and the matching can be completed only if the device information before and after is consistent, thereby improving the flexibility and efficiency of USB device reconnection.
[0032] At the same time, the system verifies the newly inserted USB device by checking its device information to confirm whether it is a replacement for an existing USB device. If so, the new USB device information (primarily the serial number) is updated to the configuration information of the replaced USB device and associated with the communication path of the replaced USB device, eliminating the need for manual information matching. The device information includes but is not limited to information such as the manufacturer ID, product ID, and serial number, which can be obtained when the USB device is inserted into the Linux system to identify the USB device.
[0033] It is understandable that the configuration information of the USB connected to the Linux system generally includes attribute information of the USB device itself, such as manufacturer ID, product ID, serial number, and path information associated with the Linux system program.
[0034] Furthermore, the configuration information includes USB device information and a USB communication path in the Linux system.
[0035] From the above description, it can be seen that the configuration information includes the USB device information and the USB communication path in the Linux system. The device information includes but is not limited to the manufacturer ID, product ID, serial number and other information that can be obtained to identify the USB when the USB is inserted into the Linux system. Therefore, when the USB is inserted into the Linux system again, the matching can be completed by obtaining the above device information again without obtaining specific and necessary fixed one or several pieces of information.
[0036] At the same time, it is understandable that the USB communication path in the Linux system is generally constructed based on the position of the port where the USB is inserted into the Linux system at the physical layer. For example, when the device node name of device B is ttyUSB0, it means that device B is inserted into the default first port in the Linux system at this time, and the communication connections of internal programs are all established based on the first port position itself.
[0037] In an embodiment of the present invention, step S3 specifically includes the following steps:
[0038] S31, matching the configuration information with the device information inserted into the USB, if they are consistent, proceed to step S32; otherwise, proceed to step 33;
[0039] S32, re-establishing a communication connection between the USB plugged in and the program in the system according to the communication path in the configuration information;
[0040] S33: If the device information in the configuration information is consistent with the device information of the inserted USB and the communication path information is inconsistent, a new communication path is established between the inserted USB and the program in the system.
[0041] As can be seen from the above description, the device information inserted into the USB is matched with the original configuration information. There are two cases: the path and device information are consistent, and the device information is consistent but the path information is inconsistent. In order to distinguish and handle these two cases, the specific processing in this example is as follows:
[0042] When the path and device information are consistent, that is, the path of the newly connected USB device (that is, its location or identifier in the system, such as / dev / usb001) is the same as the path in the previously recorded configuration information, the system will communicate according to the preset configuration information. This situation is suitable for scenarios where the device is reconnected to the same physical port or the system identifies the device in the same way. The system will assume that it is the same device and communicate according to the originally set parameters without the need for reconfiguration, improving communication efficiency and preventing reconnection failures after the identity is fully identified.
[0043] When the device information is consistent but the path information is inconsistent—that is, the new device's vendor, model, and other information match those of a known device, but its path in the system has changed (for example, / dev / usb002 instead of / dev / usb001), the system will no longer communicate according to the original configuration, but will instead use the new device path. This typically occurs when a device is plugged into a different USB port. Although the device itself remains unchanged, the change in physical connection location necessitates a new path. At this point, the system must reestablish communication based on the new path to avoid reconnection failures and program disruptions after reconnection.
[0044] In an embodiment of the present invention, step S33 further includes the following steps:
[0045] Update the information of the new communication path established between the USB inserted and the program in the system to its configuration information in the Linux system.
[0046] As can be seen from the above description, in order to quickly match in subsequent plugging and unplugging, the device information of the USB whose communication path is changed this time is re-associated with the changed communication path and saved in the Linux system configuration information.
[0047] In an embodiment of the present invention, step S2 specifically includes the following steps:
[0048] S21, traverse the directory storing external device information in the Linux system, cache the device information of all current USB devices and establish a USB device information table;
[0049] S22, monitoring the event of USB insertion in the Linux system, when the event occurs, querying the USB devices in the directory and comparing them with the USB device information table to filter out newly added USB devices;
[0050] S23. Record the device information inserted into the USB.
[0051] As can be seen from the above description, the polling mechanism is used to quickly obtain information about USB devices currently connected to the Linux system and filter out newly inserted USB devices, avoiding manual intervention and improving processing efficiency. Preferably, the directory storing external device information is the / dev / directory; it is understandable that the / dev / directory is the abbreviation of device and is used to provide a standardized way for applications to access various devices in the system by accessing device files.
[0052] Please refer to Figure 3 A terminal for reconnecting USB based on Linux system includes a memory, a processor and a computer program stored in the memory and runnable on the processor. When the processor executes the computer program, the steps in the method for reconnecting USB based on Linux system are completed.
[0053] From the above description, it can be seen that the beneficial effect of the present invention is: providing an execution carrier of a method for reconnecting USB based on a Linux system, when executing the steps in the above method, by recording the configuration information of the USB that has been connected to the Linux system and matching it with the device information inserted into the USB, if the matching consistency is passed, it will automatically reconnect with the program in the system according to the original configuration information, avoiding human intervention and confirmation before reconnection, and at the same time, there is no need to obtain all the device information of the USB for identification, and only the previous and subsequent device information needs to be consistent to complete the matching, thereby improving the flexibility and efficiency of USB device reconnection.
[0054] The present invention provides a method and terminal for reconnecting USB based on Linux system, which is mainly used to solve the problem of USB system reconnection in Linux system. The following is a detailed description with reference to the embodiments:
[0055] Please refer to Figure 1 , embodiment 1 of the present invention is:
[0056] A method for reconnecting a USB based on a Linux system, comprising the steps of:
[0057] S1. Obtain the configuration information of the USB connected to the Linux system;
[0058] S2, monitor the event of USB insertion in Linux system and record the device information of USB insertion;
[0059] S3. Match the configuration information with the device information inserted into the USB. If they are consistent, reestablish the communication connection between the inserted USB and the program in the system according to the configuration information.
[0060] That is, in this embodiment, by recording the configuration information of the USB that has been connected to the Linux system and matching it with the device information inserted into the USB, if the matching consistency is passed, it will automatically reconnect with the program in the system according to the original configuration information, avoiding human intervention and confirmation before reconnection. At the same time, there is no need to obtain all the device information of the USB for identification, and the matching can be completed as long as the previous and subsequent device information are consistent, which improves the flexibility and efficiency of USB device reconnection.
[0061] Please refer to Figure 2 , the second embodiment of the present invention is:
[0062] Based on the first embodiment, step S2 specifically includes the following steps:
[0063] S21, traverse the directory storing external device information in the Linux system, cache the device information of all current USB devices and establish a USB device information table;
[0064] S22. Monitor the event of USB insertion in the Linux system. When the event occurs, query the USB devices in the directory and compare them with the USB device information table to filter out newly inserted USB devices. Use a polling mechanism to quickly obtain information about USB devices currently connected to the Linux system and filter out newly inserted USB devices, thereby avoiding manual intervention and improving processing efficiency.
[0065] S23. Record the device information inserted into the USB.
[0066] Preferably, the directory storing external device information is the / dev / directory.
[0067] The configuration information includes USB device information and USB communication path in the Linux system.
[0068] Step S3 specifically includes the following steps:
[0069] S31. Match the configuration information with the device information inserted into the USB. If they are consistent, proceed to step S32. Otherwise, proceed to step S33. Use the device information inserted into the USB to match the original configuration information. There are two cases: the path and device information are consistent, and the device information is consistent but the path information is inconsistent. In order to distinguish and handle the two cases, the specific processing in this example is as follows:
[0070] S32. Re-establish the communication connection between the inserted USB and the program in the system according to the communication path in the configuration information; when the path and device information are consistent, that is, the path of the newly connected USB device (that is, the location or identifier in the system, such as / dev / usb001) is the same as the path in the previously recorded configuration information, the system will communicate according to the preset configuration information. This situation is applicable to the scenario where the device is reconnected to the same physical port or the system identifies the device in the same way. The system will consider it to be the same device and communicate according to the originally set parameters without the need for reconfiguration, thereby improving communication efficiency and preventing reconnection failure after the identity is fully identified.
[0071] S33, if the device information in the configuration information is consistent with the device information inserted into the USB and the communication path information is inconsistent, then a new communication path will be established between the program inserted into the USB and the system, and the information of the new communication path established between the program inserted into the USB and the system will be updated to its configuration information in the Linux system. When the device information is consistent but the path information is inconsistent, that is, the information such as the supplier and model of the new device is consistent with the known device, but its path in the system has changed (such as / dev / usb001 before, and now it has become / dev / usb002), then the system will not communicate according to the original configuration, but use the new device path, which usually occurs when the device is inserted into a different USB port. Although the device itself has not changed, the system has given it a new path because of the change in the physical connection position. At this point, the system needs to re-establish the communication connection according to the new path to avoid reconnection failure and program disorder after reconnection.
[0072] Step S3 is followed by step S4:
[0073] When an insertion event is detected and the device information other than the serial number of the inserted USB device is consistent with the device information of a certain USB device, it is preliminarily determined to be a replacement of the same type of USB device;
[0074] If the USB device is inserted again within the preset time, it is determined that the USB device is replaced by the same type, and the device information of the USB is updated to the configuration information of the replaced USB and associated with the communication path of the replaced USB.
[0075] This secondary detection method confirms whether the newly inserted USB device is a replacement for an existing USB device. If so, the new USB device's device information (primarily the serial number) is updated to the configuration information of the replaced USB device and associated with the communication path of the replaced USB device, eliminating the need for manual information matching. The device information includes but is not limited to the manufacturer ID, product ID, serial number, and other information that can be obtained when the USB device is inserted into the Linux system to identify the USB device.
[0076] Please refer to Figure 3 , the third embodiment of the present invention is:
[0077] A terminal 1 for reconnecting a USB based on a Linux system includes a memory 2, a processor 3, and a computer program stored in the memory 2 and executable on the processor 3. When the processor 3 executes the computer program, the steps of any one of the methods for reconnecting a USB based on a Linux system described in the first and second embodiments are completed.
[0078] In summary, the present invention provides a method and terminal for reconnecting a USB based on a Linux system. The method records the configuration information of a USB that has been connected to the Linux system and matches it with the device information inserted into the USB. If the matching consistency is passed, the method automatically reconnects with the program in the system according to the original configuration information, avoiding manual intervention and confirmation before reconnection. At the same time, there is no need to obtain all the device information of the USB for identification. Matching can be completed only if the previous and subsequent device information are consistent, thereby improving the flexibility and efficiency of USB device reconnection.
[0079] The above descriptions are merely embodiments of the present invention and are not intended to limit the patent scope of the present invention. Any equivalent transformations made using the contents of the present invention's description and drawings, or directly or indirectly applied in related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A method for reconnecting and replacing USB based on Linux system, characterized by: Including steps: S1. Obtain configuration information of a USB device connected to a Linux system; the configuration information includes USB device information and a communication path of the USB device in the Linux system; S2. Monitor the event of USB insertion in the Linux system and record the device information of the USB insertion; the device information includes the manufacturer ID, product ID or serial number; S3, matching the configuration information with the device information of the USB device, and if they are consistent, re-establishing the communication connection between the USB device and the program in the system according to the configuration information; The step S3 specifically includes the following steps: S31, matching the configuration information with the device information inserted into the USB, if they are consistent, proceeding to step S32; Otherwise, proceed to step S33; S32, re-establishing a communication connection between the USB plugged in and the program in the system according to the communication path in the configuration information; S33, if the device information in the configuration information is consistent with the device information of the inserted USB and the communication path information is inconsistent, then establish a new communication path between the inserted USB and the program in the system; S4. When an insertion event is detected and the device information other than the serial number information obtained from the inserted USB device is consistent with the device information of a certain USB device, it is preliminarily determined that the USB device is replaced with the same type; If the insertion event of the USB device is detected again within the preset time, it is determined that the USB device of the same type is replaced, and the device information of this USB is updated to the configuration information of the replaced USB and associated with the communication path of the replaced USB.
2. The method for reconnecting and replacing a USB device based on a Linux system according to claim 1, wherein: The step S33 further comprises the steps of: Update the information of the new communication path established between the USB inserted and the program in the system to its configuration information in the Linux system.
3. The method for reconnecting and replacing a USB device based on a Linux system according to claim 1, wherein: The step S2 specifically includes the following steps: S21, traverse the directory storing external device information in the Linux system, cache the device information of all current USB devices and establish a USB device information table; S22, monitoring the event of USB insertion in the Linux system, when the event occurs, querying the USB devices in the directory and comparing them with the USB device information table to filter out newly added USB devices; S23. Record the device information inserted into the USB.
4. A terminal for reconnecting and replacing USB based on Linux system, characterized by: The invention comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the following steps are performed: S1. Obtain configuration information of a USB device connected to a Linux system; the configuration information includes USB device information and a communication path of the USB device in the Linux system; S2. Monitor the event of USB insertion in the Linux system and record the device information of the USB insertion; the device information includes the manufacturer ID, product ID or serial number; S3, matching the configuration information with the device information of the USB device, and if they are consistent, re-establishing the communication connection between the USB device and the program in the system according to the configuration information; The step S3 specifically includes the following steps: S31, matching the configuration information with the device information inserted into the USB, if they are consistent, proceeding to step S32; Otherwise, proceed to step S33; S32, re-establishing a communication connection between the USB plugged in and the program in the system according to the communication path in the configuration information; S33, if the device information in the configuration information is consistent with the device information of the inserted USB and the communication path information is inconsistent, then establish a new communication path between the inserted USB and the program in the system; S4. When an insertion event is detected and the device information other than the serial number information obtained from the inserted USB device is consistent with the device information of a certain USB device, it is preliminarily determined that the USB device is replaced with the same type; If the insertion event of the USB device is detected again within the preset time, it is determined that the USB device of the same type is replaced, and the device information of this USB is updated to the configuration information of the replaced USB and associated with the communication path of the replaced USB.
5. The terminal for reconnecting and replacing USB based on Linux system according to claim 4, characterized in that: The step S33 further comprises the steps of: Update the information of the new communication path established between the USB inserted and the program in the system to its configuration information in the Linux system.
6. The terminal for reconnecting and replacing USB based on Linux system according to claim 4, characterized in that: The step S2 specifically includes the following steps: S21, traverse the directory storing external device information in the Linux system, cache the device information of all current USB devices and establish a USB device information table; S22, monitoring the event of USB insertion in the Linux system, when the event occurs, querying the USB devices in the directory and comparing them with the USB device information table to filter out newly added USB devices; S23. Record the device information inserted into the USB.
Citation Information
Patent Citations
Method and device for realizing hot plugging in recovery mode
CN106844261A
Method for auditing historical usage condition of USB device in Linux system
CN107918550A