Method and terminal for reconnecting and replacing USB based on Linux system
By obtaining and recording the configuration information of the USB device, matching and updating the device information, the reconnection failure caused by the number changes in the Linux system after the USB device restart is solved, automatic reconnection and efficient matching are achieved, and the reconnection of the USB device is improved. Flexibility and efficiency.
Patent Information
- Application Number
- CN202411224888.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-29
- Publication Date
- 2024-12-13
- Estimated Expiration
- 2044-05-29
AI Technical Summary
In Linux systems, after the USB device is restarted or re-plugged, the device number will change, resulting in the inability to ensure the consistency of the device node name, resulting in the failure of reconnection, and the existing methods are inefficient and cumbersome steps.
By obtaining and recording the configuration information of the USB device, monitoring the insertion event, matching the configuration information and device information, if the same is true, rebuilding the communication connection, identifying it as a device of the same type to replace and update the configuration information, avoiding human intervention and improving the reconnection efficiency.
It realizes automatic reconnection of USB devices, improves flexibility and efficiency, avoids human confirmation steps, and only needs consistent information of front and rear devices to complete matching, improving the reliability and speed of reconnection of USB devices.
Smart Images

Figure CN119127770A_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 systems, the insertion and removal of USB devices will cause the names of USB devices in the system to change. Generally speaking, the Linux kernel will assign numbers according to the order in which the devices are inserted, for example: the first inserted device is numbered ttyUSB0, the second inserted device is numbered ttyUSB1, and so on.
[0004] When you restart the Linux system or re-plug the USB device, the Linux kernel will re-assign the number of each device, and the original device number will change. It is impossible to guarantee that the device node name of device A is ttyUSB0, and the device node name of device B is ttyUSB1. There is no method to provide a fixed correspondence between USB and device node name in the device file directory, that is, after inserting the USB, it is impossible to guarantee that the communication path of the plug-in port is consistent with the target path of the plug-in USB. Therefore, if you only use the device node name to distinguish which specific device it is, it is easy to cause confusion in the program. In Linux, the program needs a fixed device node name to communicate with the USB device.
[0005] In order to obtain the specific node name of the USB device, the usual practice is to check whether there is a new node name in the device file directory when the USB device is inserted, so as to determine the name of the device. However, this process usually requires human intervention, which is inefficient and cumbersome. Alternatively, udev rules can be used to write the insertion and removal events of the USB device, and the USB device alias can be customized according to the device's manufacturer ID, product ID, serial number and other attributes. The alias makes the device name unique, but due to the differences between USB manufacturers, it is often the case that one or more of the manufacturer ID, product ID, and serial number cannot be obtained, and a unique alias cannot be generated, resulting in deviations during reconnection and reconnection failure. Summary of the invention
[0006] The technical problem to be solved by the present invention is to provide a method and a terminal for reconnecting a USB 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 USB based on 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 inserting USB into Linux system and record the device information of inserting USB;
[0011] S3, matching the configuration information with the device information inserted into the USB, and if there is consistency, reestablishing the communication connection between the inserted USB 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, 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 is 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 terminal for reconnecting USB based on Linux system, comprising a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor completes the following steps when executing the computer program:
[0015] S1. Obtain the configuration information of the USB connected to the Linux system;
[0016] S2, monitor the event of inserting USB into Linux system and record the device information of inserting USB;
[0017] S3, matching the configuration information with the device information inserted into the USB, and if there is consistency, reestablishing the communication connection between the inserted USB 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, 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 is associated with the communication path of the replaced USB.
[0019] The beneficial effects of the present invention at least include: providing a method and terminal for reconnecting USB based on Linux system, by recording the configuration information of USB that has been connected to Linux system and matching it with the device information inserted into 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 USB for identification, 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. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 This is a flow chart of a method for reconnecting USB based on a Linux system in an embodiment of the present invention;
[0021] Figure 2 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 A 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] In order to explain the technical content, achieved objectives and effects of the present invention in detail, the following is an explanation in combination with the implementation modes and the accompanying drawings.
[0026] Please refer to Figure 1 to Figure 2 , a method for reconnecting USB based on Linux system, comprising the steps of:
[0027] S1. Obtaining configuration information of a USB connected to a Linux system; the configuration information includes USB device information and a communication path of the USB in the Linux system;
[0028] S2, monitor the event of inserting USB into Linux system and record the device information of inserting USB;
[0029] S3, matching the configuration information with the device information inserted into the USB, and if there is consistency, reestablishing the communication connection between the inserted USB 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, 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 is 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 when the previous and subsequent device information are consistent, thereby improving the flexibility and efficiency of USB device reconnection.
[0032] At the same time, by checking the device information, it is confirmed whether the newly inserted USB is a replacement device of the original USB. If it is, the device information (mainly the serial number) of the new USB is updated to the configuration information of the replaced USB and associated with the communication path of the replaced USB, without 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 is inserted into the Linux system to identify the USB.
[0033] It is understandable that the configuration information of the USB connected to the Linux system generally includes the attribute information of the USB device itself, such as the manufacturer ID, product ID, serial number and path information associated with the program of the Linux system.
[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 device information of the USB and the communication path of the USB 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 items of information.
[0036] At the same time, it is understandable that the communication path of USB in the Linux system is generally constructed based on the position of the USB port inserted in 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, reestablishing the communication connection between the inserted USB 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] From the above description, we can know that the device information inserted into the USB is matched with the original configuration information. There are two situations: 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 situations, 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 (i.e., 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 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 consider it to be the same device and communicate according to the originally set parameters without reconfiguration, which improves communication efficiency and prevents reconnection failures after the identity is fully identified.
[0043] When the device information is consistent but the path information is inconsistent, that is, the vendor, model and other information of the new device are consistent with the known device, but its path in the system has changed (for example, it was / dev / usb001 before and now becomes / dev / usb002), then the system will not communicate according to the original configuration, but will use the new device path. This usually happens when the device is plugged into a different USB port. Although the device itself has not changed, the system assigns it a new path because of the change in the physical connection location. At this time, the system needs to re-establish the communication connection according to the new path to avoid reconnection failure and program disorder after reconnection.
[0044] In an embodiment of the present invention, the step S33 further includes the steps of:
[0045] Update the information of the new communication path established between the inserted USB and the program in the system to its configuration information in the Linux system.
[0046] From the above description, it can be known that 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 configuration information of the Linux system.
[0047] In an embodiment of the present invention, step S2 specifically includes the steps of:
[0048] S21, traverse the directory storing external device information in the Linux system, cache the device information of all current USBs and establish a USB device information table;
[0049] S22, monitoring the event of inserting a USB into the Linux system, and 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 USBs;
[0050] S23, record the device information inserted into the USB.
[0051] From the above description, it can be seen that the polling mechanism is used to quickly obtain the USB device information currently connected to the Linux system and filter out the newly inserted USB, avoiding manual intervention and improving processing efficiency. Preferably, the directory storing the external device information is the / dev / directory; it can be understood that the / dev / directory is the abbreviation of device, which 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 executable 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 effects of the present invention are: 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 is automatically reconnected 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 the matching can be completed only when the previous and subsequent device information are consistent, 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 are mainly used to solve the problem of USB system reconnection in Linux system, and is specifically described in conjunction with embodiments below:
[0055] Please refer to Figure 1 , Embodiment 1 of the present invention is:
[0056] A method for reconnecting USB based on 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 inserting USB into Linux system and record the device information of inserting USB;
[0059] S3. Match the configuration information and 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 , Embodiment 2 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 USBs and establish a USB device information table;
[0064] S22, monitoring the event of inserting a USB into the Linux system, querying the USB devices in the directory when the event occurs and comparing with the USB device information table to filter out the newly added USB; using the polling mechanism to quickly obtain the USB device information currently connected to the Linux system and filter out the newly inserted USB, avoiding manual intervention and improving processing efficiency;
[0065] S23, record the device information inserted into the USB.
[0066] Preferably, the directory storing the 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 and the device information inserted into the USB, if there is consistency, go to step S32; otherwise, go to step 33; use the device information inserted into the USB to match the original configuration information, which is divided into two situations: the path and device information are consistent and the device information is consistent but the path information is inconsistent. In order to distinguish and process the two situations, 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 position 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 consider this to be the same device and communicate according to the originally set parameters without 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 is established between the inserted USB and the program in the system, and the information of the new communication path established between the inserted USB and the program in the system is 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 (for example, it was / 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 plugged into different USB ports. 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 time, 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 further includes step S4:
[0073] When an insertion event is detected, and the device information other than the serial number information obtained from the inserted USB device information is consistent with the device information of a certain USB, it is preliminarily determined to be a replacement of the same type of USB device;
[0074] If the insertion event of the USB device is detected again within the preset time, it is confirmed 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.
[0075] Through the above secondary detection method, it is confirmed whether the newly inserted USB is a replacement device of an original USB. If it is, the device information (mainly the serial number) of the new USB is updated to the configuration information of the replaced USB and associated with the communication path of the replaced USB, without 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 is inserted into the Linux system to identify the USB.
[0076] Please refer to Figure 3 , Embodiment 3 of the present invention is:
[0077] A terminal 1 for reconnecting USB based on Linux system, comprising 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 USB based on Linux system in the above-mentioned embodiments 1 to 2 are completed.
[0078] In summary, the present invention provides a method and terminal for reconnecting USB based on Linux system, which records the configuration information of USB that has been connected to Linux system and matches it with the device information inserted into 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 USB for identification, and the matching can be completed only when 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 specification 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: Includes steps: S1. Obtaining configuration information of a USB connected to a Linux system; the configuration information includes USB device information and a communication path of the USB in the Linux system; S2, monitor the event of inserting USB into Linux system and record the device information of inserting USB; S3, matching the configuration information with the device information of the USB device, and if they are consistent, reestablishing the communication connection between the USB device and the program in the system according to the configuration information; 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, 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 is associated with the communication path of the replaced USB.
2. The method for reconnecting and replacing USB based on Linux system according to claim 1, characterized in that: The step S3 specifically comprises 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, reestablishing the communication connection between the inserted USB 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, a new communication path is established between the inserted USB and the program in the system.
3. The method for reconnecting and replacing USB based on Linux system according to claim 2, characterized in that: The step S33 further comprises the steps of: Update the information of the new communication path established between the inserted USB and the program in the system to its configuration information in the Linux system.
4. The method for reconnecting and replacing USB based on Linux system according to claim 1, 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 USBs and establish a USB device information table; S22, monitoring the event of inserting a USB into the Linux system, and 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 USBs; S23, record the device information inserted into the USB.
5. The method for reconnecting and replacing USB based on Linux system according to claim 1, characterized in that: The step S4 specifically comprises the following steps: When an insertion event is detected, and the device information other than the serial number information obtained from the inserted USB device information is consistent with the device information of a certain USB, it is preliminarily determined to be a replacement of the same type of USB device; If the insertion event of the USB device is detected again within the preset time, it is confirmed 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.
6. 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. Obtaining configuration information of a USB connected to a Linux system; the configuration information includes USB device information and a communication path of the USB in the Linux system; S2, monitor the event of inserting USB into Linux system and record the device information of inserting USB; S3, matching the configuration information with the device information of the USB device, and if they are consistent, reestablishing the communication connection between the USB device and the program in the system according to the configuration information; 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, 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 is associated with the communication path of the replaced USB.
7. A terminal for reconnecting and replacing USB based on Linux system according to claim 6, characterized in that: The step S3 specifically comprises 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, reestablishing the communication connection between the inserted USB 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, a new communication path is established between the inserted USB and the program in the system.
8. A terminal for reconnecting and replacing USB based on Linux system according to claim 7, characterized in that: The step S33 further comprises the steps of: Update the information of the new communication path established between the inserted USB and the program in the system to its configuration information in the Linux system.
9. A terminal for reconnecting and replacing USB based on Linux system according to claim 6, 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 USBs and establish a USB device information table; S22, monitoring the event of inserting a USB into the Linux system, and 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 USBs; S23, record the device information inserted into the USB.
10. A terminal for reconnecting and replacing USB based on Linux system according to claim 6, characterized in that: The step S4 specifically comprises the following steps: When an insertion event is detected, and the device information other than the serial number information obtained from the inserted USB device information is consistent with the device information of a certain USB, it is preliminarily determined to be a replacement of the same type of USB device; If the insertion event of the USB device is detected again within the preset time, it is confirmed 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.
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
Auto-configurable host pluggable computing
US20180101392A1
Method, apparatus, system to enhance the device policy manager (DPM) to manage an extended USB-c ecosystem
US20190121764A1
