A method, device and medium for identifying serial port number
By obtaining and traversing the USB access serial port number and sending verification data to identify the serial port number, it solves the problem of failed identification of VID and PID identifiers, achieves accurate identification and stable connection of USB-to-serial port devices, and improves the user experience and applications. of robustness.
Patent Information
- Application Number
- CN202310371488.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-06
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2043-04-06
AI Technical Summary
It is difficult to accurately identify the serial port number using the VID and PID identification of the USB device, resulting in connection failure and poor user experience, especially when changing manufacturers or chip types.
Obtain real-time access serial numbers through the application programming interface, traverse and send verification data to them, and determine the serial number if a correct reply is received. The preferred solution includes storing identification results, monitoring threads, and soft restart processing.
It achieves accurate identification of USB-to-serial devices and is suitable for initial connection and re-plugging without special processing, which improves the robustness and user experience of the application.
Smart Images

Figure CN116483751B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communications, and in particular to a serial port number identification method, device and medium. Background Art
[0002] Universal Serial Bus (USB) to serial port is to realize the conversion between the computer USB interface and the universal serial port. It provides a fast channel for computers without serial ports. Moreover, using USB to serial port device is equivalent to turning traditional serial port devices into plug-and-play USB devices.
[0003] All USB devices have a vendor identification code (VID) and a product identification code (PID). Currently, USB-to-serial port identification mostly uses the VID and PID of the USB-to-serial port device to identify the USB device and the corresponding serial port number.
[0004] On the one hand, it is necessary to manually modify the configuration file in advance and save the VID and PID information of the USB device into the configuration file, which increases the burden on users. On the other hand, for the sake of convenience, many USB device manufacturers do not apply for their own VID to the USB Implementation Forum, but still use the VID of the main control manufacturer or write VID and PID to the product at will. At the same time, regular manufacturers only need to apply for VID, and PID is determined by the manufacturer. Therefore, there are products of the same model that may use different main controls, and their PIDs are the same. Based on the above reasons, the main control model of the USB device cannot be accurately identified by VID and PID, and thus the corresponding serial port number cannot be accurately identified. If the USB to serial port device line is replaced with a USB device from a different manufacturer, it will not work according to the VID and PID of the original manufacturer's USB device. Therefore, it is difficult to automatically and accurately identify the serial port number required for communication using the VID and PID identification of the USB device in many cases, which leads to the situation that the host computer system cannot connect or control the device when controlling the device, which brings a bad experience to the user.
[0005] It can be seen that how to solve the problem that using the VID and PID identifiers of the USB device to identify the communication serial port number may cause connection failure is a technical problem that needs to be solved urgently by people in this field. Summary of the invention
[0006] The purpose of the present application is to provide a serial port number identification method, device and medium to solve the problem that using the VID and PID identifiers of a USB device to identify the communication serial port number may cause connection failure.
[0007] In order to solve the above technical problems, the present application provides a serial port number identification method, which is characterized by comprising:
[0008] Obtain all real-time access serial port numbers through the application programming interface;
[0009] Traverse all access serial port numbers, open the corresponding serial port, and send verification data to the corresponding serial port;
[0010] If the correct reply data is received, it is determined that the serial port number used by the communication channel is the current access serial port number.
[0011] As a preferred solution, in the above serial port number identification method, determining that the current access serial port number is the serial port number used by the current communication channel, then further comprising:
[0012] Store the communication channel and the corresponding access serial port number in the configuration file.
[0013] As a preferred solution, in the above serial port number identification method, all access serial port numbers are obtained in real time through an application programming interface, and the method also includes:
[0014] Determine whether the access serial port number corresponding to the current communication channel exists in the configuration file;
[0015] If it exists, the serial port corresponding to the stored access serial port number is opened, and verification data is sent to the corresponding serial port;
[0016] If the correct reply data is received, it is determined that the serial port number used by the communication channel is the current access serial port number;
[0017] If no reply data is received, the process returns to the step of obtaining all access serial port numbers through the application programming interface.
[0018] As a preferred solution, the above serial port number identification method further includes:
[0019] When the communication channel is interrupted, all access serial port numbers are obtained in real time through the application programming interface;
[0020] Determine whether all currently accessed serial port numbers include the accessed serial port number used before the communication channel was interrupted;
[0021] If not, the monitoring thread is started. If a USB device is detected to be inserted, the process returns to the step of obtaining all real-time serial port numbers through the application programming interface.
[0022] As a preferred solution, in the above serial port number identification method, if all current access serial port numbers include the access serial port number used before the communication channel is interrupted, it also includes:
[0023] Close and reopen the serial port corresponding to the access serial port number used before the communication channel was interrupted;
[0024] If the opening fails, the serial port is soft-restarted, and the process returns to the step of closing and reopening the serial port corresponding to the access serial port number used before the communication channel was interrupted, until the number of soft restarts reaches the preset maximum value, and the process ends;
[0025] If it is opened successfully, the verification data will be sent to the corresponding serial port;
[0026] If the reply data is received, it is determined that the communication of the communication channel has returned to normal;
[0027] If no reply data is received, it is determined that the receiving device is faulty.
[0028] As a preferred solution, in the above serial port number identification method, traversing the access serial port number includes:
[0029] Open the access serial port numbers in sequence. If the access serial port number is the same as the access serial port number corresponding to the current communication channel in the configuration file, open the next access serial port number.
[0030] If the access serial port number has been opened, open the next access serial port number.
[0031] As a preferred solution, the above serial port number identification method further includes:
[0032] If the periodic identification thread is started, the step of acquiring all real-time access serial port numbers through the application programming interface is executed at preset time intervals.
[0033] In order to solve the above technical problems, the present application also provides a serial port number recognition device, which is characterized by comprising:
[0034] An acquisition module is used to acquire all access serial port numbers in real time through an application programming interface;
[0035] The traversal module is used to traverse all the access serial port numbers, open the corresponding serial port, and send verification data to the corresponding serial port;
[0036] The determination module is used to determine that the serial port number used by the communication channel is the current access serial port number if the correct reply data is received.
[0037] In order to solve the above technical problems, the present application also provides a serial port number recognition device, which is characterized by comprising:
[0038] Memory for storing computer programs;
[0039] The processor is used to implement the steps of the above-mentioned serial port number identification method when executing a computer program.
[0040] In order to solve the above technical problems, the present application also provides a computer-readable storage medium, characterized in that a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the steps of the above serial port number identification method are implemented.
[0041] The serial port number identification method provided in this application obtains all real-time access serial port numbers through the application programming interface, traverses all access serial port numbers, opens the corresponding serial port, and sends verification data to the corresponding serial port; if the correct reply data is received, the serial port number used by the communication channel is determined to be the current access serial port number. When a communication channel needs to identify the access serial port number, all real-time access serial port numbers are obtained through the application programming interface, and a verification data is sent to the serial port that can be used on the host computer one by one, and then the serial port number used by the communication channel is judged to be the current access serial port number based on whether the reply data is received and whether the received reply data is correct. It can accurately identify the serial port number corresponding to the USB to serial port device in different situations, which is not only suitable for initial identification, but also for the situation where the serial port number changes after re-plugging. After the USB to serial port changes the chip type or changes the manufacturer, there is no need for special processing, and there is no need to restart the software, which improves the robustness and maintainability of the application, and also indirectly improves the user experience.
[0042] In addition, the present application also provides a device and a medium, which correspond to the above method and have the same effect as above. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0044] Figure 1 A flowchart of a serial port number identification method provided in an embodiment of the present application;
[0045] Figure 2 A structural diagram of a serial port number identification device provided in an embodiment of the present application;
[0046] Figure 3 A structural diagram of another serial port number identification device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0047] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0048] The core of this application is to provide a serial port number identification method, device and medium.
[0049] In order to enable those skilled in the art to better understand the present application, the present application is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0050] USB to serial port is to realize the conversion between the computer USB interface and the universal serial port. It provides a fast channel for computers without serial ports. Moreover, using USB to serial port equipment is equivalent to turning traditional serial port devices into plug-and-play USB devices. All USB devices have VID and PID. At present, most USB to serial port identification uses the VID and PID of USB to serial port equipment to identify USB devices and corresponding serial port numbers.
[0051] On the one hand, it is necessary to manually modify the configuration file in advance and save the VID and PID information of the USB device into the configuration file, which increases the burden on users. On the other hand, for the sake of convenience, many USB device manufacturers do not apply for their own VID to the USB Implementation Forum, but still use the VID of the main control manufacturer or write VID and PID to the product at will. At the same time, regular manufacturers only need to apply for VID, and PID is determined by the manufacturer. Therefore, there are products of the same model that may use different main controls, and their PIDs are the same. Based on the above reasons, the main control model of the USB device cannot be accurately identified by VID and PID, and thus the corresponding serial port number cannot be accurately identified. If the USB to serial port device line is replaced with a USB device from a different manufacturer, it will not work according to the VID and PID of the original manufacturer's USB device. Therefore, it is difficult to automatically and accurately identify the serial port number required for communication using the VID and PID identification of the USB device in many cases, which leads to the situation that the host computer system cannot connect or control the device when controlling the device, which brings a bad experience to the user.
[0052] To solve the above problem, this embodiment provides a serial port number identification method. Figure 1 A flowchart of a serial port number identification method provided in an embodiment of the present application includes:
[0053] S11: Obtain all real-time access serial port numbers through the application programming interface;
[0054] S12: traverse all access serial port numbers, open the corresponding serial port, and send verification data to the corresponding serial port;
[0055] S13: If the correct reply data is received, it is determined that the serial port number used by the communication channel is the current access serial port number.
[0056] This application is based on the perspective of the host computer. When some USB devices need to be controlled by the host computer, the communication connection with the USB device is realized by identifying the serial port number required for communication. For example, when the host computer software controls medical rehabilitation equipment, it needs to use the USB to serial port communication method to interact with the device for data and commands, thereby realizing the control and real-time monitoring of the medical equipment.
[0057] Application Programming Interface (API) is a set of predefined functions, the purpose of which is to provide applications and developers with the ability to access a set of routines based on certain software or hardware, without having to access the source code or understand the details of the internal working mechanism. All real-time access serial port numbers are obtained through the application programming interface. In actual applications, the host computer may contain multiple external communication channels to achieve control of multiple devices, and when each device is connected, all current access serial port numbers can be obtained through the application programming interface. This embodiment does not limit the specific scenario where the serial port number needs to be identified. It can be when the connection is first established, or when the communication is interrupted and needs to be reconnected. When a communication channel needs to identify the access serial port number, the serial port number can be identified by the method provided in this embodiment.
[0058] Step S12 traverses all access serial port numbers, opens the corresponding serial port, and sends verification data to the corresponding serial port; this embodiment does not limit the form of the verification data, and it can be set according to actual needs. Step S13 If the correct reply data is received, it is determined that the serial port number used by the communication channel is the current access serial port number. If no reply data is received, continue to open the next access serial port number. For example, if a string of data is sent as 01 00 (denoted as DS), then the received data is 01 01 (denoted as DR), which means that the pairing is successful, and the matching principle can be freely specified. By sending a verification data to the serial ports that can be used on the host computer one by one, and then judging the access serial port number to be used by the current function based on whether the reply data is received and whether the received reply data matches the sent verification data.
[0059] Through the serial port number identification method provided in this embodiment, all real-time access serial port numbers are obtained through the application programming interface, all access serial port numbers are traversed, the corresponding serial port is opened, and verification data is sent to the corresponding serial port; if the correct reply data is received, the serial port number used by the communication channel is determined to be the current access serial port number. When a communication channel needs to identify the access serial port number, all real-time access serial port numbers are obtained through the application programming interface, and a verification data is sent to the serial port that can be used on the host computer one by one, and then the serial port number used by the communication channel is judged to be the current access serial port number based on whether the reply data is received and whether the received reply data is correct. It can accurately identify the serial port number corresponding to the USB to serial port device in different situations, which is not only suitable for initial identification, but also for the situation where the serial port number changes after re-plugging. After the USB to serial port changes the chip type or changes the manufacturer, no special processing is required, and no software restart is required, which improves the robustness and maintainability of the application, and also indirectly improves the user experience.
[0060] According to the above embodiment, in an actual application environment, a communication channel is usually fixedly connected to the same USB device. Therefore, in order to improve the recognition efficiency, this embodiment provides a preferred solution, which determines that the current access serial port number is the serial port number used by the current communication channel, and then further includes:
[0061] Store the communication channel and the corresponding access serial port number in the configuration file.
[0062] After each correct identification, the communication channel and the corresponding access serial port number are stored in the configuration file. The information recorded in the configuration file can be directly used next time to improve the identification efficiency.
[0063] Specifically, all access serial port numbers in real time are obtained through the application programming interface, which previously also includes:
[0064] Determine whether the access serial port number corresponding to the current communication channel exists in the configuration file;
[0065] If it exists, the serial port corresponding to the stored access serial port number is opened, and verification data is sent to the corresponding serial port;
[0066] If the correct reply data is received, it is determined that the serial port number used by the communication channel is the current access serial port number;
[0067] If no reply data is received, the process returns to the step of obtaining all access serial port numbers through the application programming interface.
[0068] If the access serial port number corresponding to the current communication channel has been recorded in the configuration file, then an attempt is made to connect through the stored access serial port number. If the connection fails, the process returns to step S11 to traverse and find the correct serial port number.
[0069] In a specific application, if the communication is interrupted, it may be that the USB device is unplugged, or the communication connection network is in a bad condition. Therefore, this embodiment provides a preferred solution, which also includes:
[0070] When the communication channel is interrupted, all access serial port numbers are obtained in real time through the application programming interface;
[0071] Determine whether all currently accessed serial port numbers include the accessed serial port number used before the communication channel was interrupted;
[0072] If not, the monitoring thread is started. If a USB device is detected to be inserted, the process returns to the step of obtaining all real-time serial port numbers through the application programming interface.
[0073] The purpose of obtaining all real-time access serial port numbers when the communication is interrupted is different from step S11. This embodiment is to confirm whether the current real-time serial port number includes the access serial port number used before the communication channel is interrupted; if not, it means that the USB device has been unplugged. If a new USB device is inserted later, step S11 is executed to identify the serial port number.
[0074] If all current access serial port numbers include the access serial port number used before the communication channel was interrupted, they also include:
[0075] Close and reopen the serial port corresponding to the access serial port number used before the communication channel was interrupted;
[0076] If the opening fails, the serial port is soft-restarted, and the process returns to the step of closing and reopening the serial port corresponding to the access serial port number used before the communication channel was interrupted, until the number of soft restarts reaches the preset maximum value, and the process ends;
[0077] If it is opened successfully, the verification data will be sent to the corresponding serial port;
[0078] If the reply data is received, it is determined that the communication of the communication channel has returned to normal;
[0079] If no reply data is received, it is determined that the receiving device is faulty.
[0080] In this embodiment, if all currently accessed serial port numbers include the accessed serial port number used before the communication channel was interrupted, then the serial port is closed and reopened to try to reconnect. If the connection is successfully opened, then the communication is judged to be normal by sending verification data to the corresponding serial port. If the connection fails, then the serial port is soft-restarted, and the process of closing and reopening the serial port corresponding to the accessed serial port number used before the communication channel was interrupted is returned until the number of soft-restarts reaches the preset maximum value. If the serial port can be successfully opened, but the reply data cannot be received, then it means that the receiving device has a fault, and a fault prompt message can be issued.
[0081] For interruptions during device operation (including unplugging and plugging of the communication line), this embodiment uses programming technology to perform a soft restart of the USB device (using the program to disable and then enable) to restart the USB device in the background, so as to avoid the problem of the serial port being occupied or other reasons causing the serial port to not be opened normally when the program directly reopens the serial port. Since it does not rely on any information of the external hardware device, all the information used is dynamically obtained during the program operation, so when the USB to serial port model or manufacturer is changed, no special processing is required, and the serial port number can still be correctly identified.
[0082] According to the above embodiment, the corresponding access serial port number recorded in the current communication channel can be known through the configuration file. Preferably, traversing the access serial port number includes:
[0083] Open the access serial port numbers in sequence. If the access serial port number is the same as the access serial port number corresponding to the current communication channel in the configuration file, open the next access serial port number.
[0084] If the access serial port number has been opened, open the next access serial port number.
[0085] Because no reply data is received, the process returns to the step of obtaining all access serial port numbers through the application programming interface. This indicates that the access serial port number corresponding to the current communication channel in the configuration file is not the correct access serial port number, and no opening verification is required. If the access serial port number has been opened, it indicates that the serial port has been used and cannot be the serial port to be used by the current communication channel. In this case, the next access serial port number is opened.
[0086] In addition, if the host computer is turned on first and the rehabilitation device is turned on later, the result of the judgment in step S11 is that the correct serial port number is not found. In order to handle this situation, this embodiment provides a preferred solution, which also includes:
[0087] If the periodic identification thread is started, the step of acquiring all real-time access serial port numbers through the application programming interface is executed at preset time intervals.
[0088] In this embodiment, the host computer system background starts a periodic identification thread, and executes step S11 every preset time. This method can achieve the purpose of accurately identifying the communication port after the device is powered on and the data line is connected normally.
[0089] In the above embodiments, the serial port number identification method is described in detail, and the present application also provides an embodiment corresponding to the serial port number identification device. It should be noted that the present application describes the embodiment of the device part from two perspectives, one is based on the functional module perspective, and the other is based on the hardware perspective.
[0090] From the perspective of functional modules, Figure 2 A structural diagram of a serial port number recognition device provided in an embodiment of the present application, such as Figure 2 As shown,
[0091] An acquisition module 21 is used to acquire all access serial port numbers in real time through an application programming interface;
[0092] The traversal module 22 is used to traverse all access serial port numbers, open the corresponding serial port, and send verification data to the corresponding serial port;
[0093] The determination module 23 is used to determine that the serial port number used by the communication channel is the current access serial port number if the correct reply data is received.
[0094] The serial port number identification device provided in this embodiment, the acquisition module 21 obtains all real-time access serial port numbers through the application programming interface; the traversal module 22 traverses all access serial port numbers, opens the corresponding serial port, and sends verification data to the corresponding serial port; if the determination module 23 receives the correct reply data, it determines that the serial port number used by the communication channel is the current access serial port number. When a communication channel needs to identify the access serial port number, all real-time access serial port numbers are obtained through the application programming interface, and a verification data is sent to the serial port that can be used on the host computer one by one, and then the serial port number used by the communication channel is judged to be the current access serial port number based on whether the reply data is received and whether the received reply data is correct. It can accurately identify the serial port number corresponding to the USB to serial port device in different situations, which is not only suitable for initial identification, but also for the situation where the serial port number changes after re-plugging. After the USB to serial port changes the chip type or changes the manufacturer, there is no need for special processing, and there is no need to restart the software, which improves the robustness and maintainability of the application, and also indirectly improves the user experience.
[0095] Since the embodiments of the apparatus part correspond to the embodiments of the method part, please refer to the description of the embodiments of the method part for the embodiments of the apparatus part, which will not be repeated here.
[0096] Figure 3 A structural diagram of another serial port number recognition device provided in an embodiment of the present application, such as Figure 3 As shown, the serial port number recognition device includes: a memory 30 for storing a computer program;
[0097] The processor 31 is used to implement the steps of the method for obtaining user operation habit information in the above embodiment (serial port number recognition method) when executing the computer program.
[0098] The serial port number identification device provided in this embodiment may include but is not limited to a smart phone, a tablet computer, a laptop computer, or a desktop computer.
[0099] Among them, the processor 31 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 31 can be implemented in at least one hardware form of a digital signal processor (DSP), a field-programmable gate array (FPGA), and a programmable logic array (PLA). The processor 31 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 31 may be integrated with a graphics processing unit (GPU), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 31 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.
[0100] The memory 30 may include one or more computer-readable storage media, which may be non-transitory. The memory 30 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 30 is at least used to store the following computer program 301, wherein, after the computer program is loaded and executed by the processor 31, it can implement the relevant steps of the serial port number identification method disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 30 may also include an operating system 302 and data 303, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 302 may include Windows, Unix, Linux, etc. Data 303 may include but is not limited to data involved in implementing the serial port number identification method, etc.
[0101] In some embodiments, the serial port number recognition device may further include a display screen 32 , an input / output interface 33 , a communication interface 34 , a power supply 35 , and a communication bus 36 .
[0102] Those skilled in the art will understand that Figure 3 The structure shown in does not constitute a limitation on the serial port number identification device, and may include more or fewer components than those shown in the figure.
[0103] The serial port number identification device provided by the embodiment of the present application includes a memory and a processor. When the processor executes the program stored in the memory, it can implement the following method: a serial port number identification method, obtains all real-time access serial port numbers through an application programming interface, traverses all access serial port numbers, opens the corresponding serial port, and sends verification data to the corresponding serial port; if the correct reply data is received, the serial port number used by the communication channel is determined to be the current access serial port number. When a communication channel needs to identify the access serial port number, all real-time access serial port numbers are obtained through the application programming interface, and a verification data is sent to the serial port that can be used on the host computer one by one, and then the serial port number used by the communication channel is judged to be the current access serial port number according to whether the reply data is received and whether the received reply data is correct. It can accurately identify the serial port number corresponding to the USB to serial port device in different situations, which is not only suitable for initial identification, but also suitable for the situation where the serial port number changes after re-plugging. After the USB to serial port changes the chip type or changes the manufacturer, no special processing is required, and no software restart is required, which improves the robustness and maintainability of the application, and also indirectly improves the user experience.
[0104] Finally, the present application also provides an embodiment corresponding to a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps described in the above embodiment of the serial port number identification method are implemented.
[0105] It is understandable that if the method in the above embodiment is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium to execute all or part of the steps of the methods of each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.
[0106] The computer-readable storage medium provided in this embodiment stores a computer program thereon. When the processor executes the program, the following method can be implemented: a serial port number identification method, obtaining all real-time access serial port numbers through an application programming interface, traversing all access serial port numbers, opening the corresponding serial port, and sending verification data to the corresponding serial port; if the correct reply data is received, the serial port number used by the communication channel is determined to be the current access serial port number. When a communication channel needs to identify the access serial port number, all real-time access serial port numbers are obtained through the application programming interface, and a verification data is sent to the serial port that can be used on the host computer one by one, and then the serial port number used by the communication channel is determined to be the current access serial port number according to whether the reply data is received and whether the received reply data is correct. It can accurately identify the serial port number corresponding to the USB to serial port device in different situations, which is not only suitable for initial identification, but also for the situation where the serial port number changes after re-plugging. After the USB to serial port changes the chip type or changes the manufacturer, no special processing is required, and no software restart is required, which improves the robustness and maintainability of the application, and also indirectly improves the user experience.
[0107] The above is a detailed introduction to the serial port number identification method, device and medium provided by the present application. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present application, several improvements and modifications can be made to the present application, and these improvements and modifications also fall within the scope of protection of the claims of the present application.
[0108] It should also be noted that, in this specification, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.
Claims
1. A method for identifying a serial port number. It is characterized in that The method is used to identify the serial port number corresponding to the USB to serial port device, including: Obtain all real-time access serial port numbers through the application programming interface; Traverse all the access serial port numbers, open the corresponding serial port, and send verification data to the corresponding serial port; If the correct reply data is received, it is determined that the serial port number used by the communication channel is the current access serial port number; When the communication of the communication channel is interrupted, obtaining all access serial port numbers in real time through the application programming interface; Determine whether all the current access serial port numbers include the access serial port number used before the communication channel is interrupted; If not, start the monitoring thread, and if it is monitored that a USB device is inserted, return to the step of obtaining all real-time access serial port numbers through the application programming interface; If so, close and reopen the serial port corresponding to the access serial port number used before the communication channel is interrupted.
2. The serial port number identification method according to claim 1, It is characterized in that The step of determining that the serial port number used by the communication channel is the current access serial port number further includes: The communication channel and the corresponding access serial port number are stored in a configuration file.
3. The serial port number identification method according to claim 2, It is characterized in that The method of obtaining all real-time access serial port numbers through the application programming interface also includes: Determine whether the access serial port number corresponding to the current communication channel exists in the configuration file; If it exists, the serial port corresponding to the stored access serial port number is opened, and verification data is sent to the corresponding serial port; If the correct reply data is received, it is determined that the serial port number used by the communication channel is the current access serial port number; If no reply data is received, return to the step of obtaining all access serial port numbers through the application programming interface.
4. The serial port number identification method according to claim 1, It is characterized in that Closing and reopening the serial port corresponding to the access serial port number used before the communication channel was interrupted, and then further comprising: If the opening fails, the serial port is soft-restarted, and the process returns to the step of closing and reopening the serial port corresponding to the access serial port number used before the communication channel is interrupted, until the number of soft restarts reaches a preset maximum value, and the process ends; If it is opened successfully, the verification data will be sent to the corresponding serial port; If the reply data is received, it is determined that the communication of the communication channel is restored to normal; If no reply data is received, it is determined that the receiving device is faulty.
5. The serial port number identification method according to claim 3, It is characterized in that The traversing the access serial port number includes: Open the access serial port numbers in sequence, and if the access serial port number is the same as the access serial port number corresponding to the current communication channel in the configuration file, open the next access serial port number; If the access serial port number has been opened, then open the next access serial port number.
6. The serial port number identification method according to claim 1, It is characterized in that Also includes: If the periodic identification thread is started, the step of acquiring all real-time access serial port numbers through the application programming interface is executed at preset time intervals.
7. A serial port number recognition device, It is characterized in that The device is used to identify the serial port number corresponding to the USB to serial port device, including: An acquisition module is used to acquire all access serial port numbers in real time through an application programming interface; A traversal module, used for traversing all the access serial port numbers, opening the corresponding serial port, and sending verification data to the corresponding serial port; A determination module, configured to determine that the serial port number used by the communication channel is the current access serial port number if correct reply data is received; When the communication of the communication channel is interrupted, obtaining all access serial port numbers in real time through the application programming interface; Determine whether all the current access serial port numbers include the access serial port number used before the communication channel is interrupted; If not, start the monitoring thread, and if it is monitored that a USB device is inserted, return to the step of obtaining all real-time access serial port numbers through the application programming interface; If so, close and reopen the serial port corresponding to the access serial port number used before the communication channel is interrupted.
8. A serial port number recognition device, It is characterized in that include: Memory for storing computer programs; A processor, configured to implement the steps of the serial port number identification method as described in any one of claims 1 to 6 when executing the computer program.
9. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the serial port number identification method according to any one of claims 1 to 6 are implemented.
