A method, system, device and storage medium for serial port virtualization

CN115964124BActive Publication Date: 2026-09-11西安超越申泰信息科技有限公司
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202211617548.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-15
Publication Date
2026-09-11
Estimated Expiration
2042-12-15

AI Technical Summary

Technical Problem

但是目前的虚拟串口会导致数据回显,并可能影响正常串口通信

Benefits of technology

[0015] The present invention has the following beneficial technical effects: by adding an Inotify mechanism to the client program of the virtual serial port to monitor changes in the status of the directory or file, timely receive or discard serial port data, realize network transmission of serial port device information, and solve the problem of remote access to serial port terminal devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115964124B_ABST
    Figure CN115964124B_ABST
Patent Text Reader

Abstract

The application provides a serial port virtualization method, system, device and storage medium, the method comprises the following steps: creating an inotify handle through a virtual serial port client program, and adding a file or a directory to be monitored by calling an inotify monitoring function; reading an event from a file descriptor of the inotify through a reading function of the virtual serial port client program; in response to receiving remote access, determining whether to receive data according to a file opening flag; and deploying the virtual serial port client program to a client, virtually connecting a TCP / IP network port to a serial port device, and connecting to a remote serial port through a serial port line to realize data communication. The application adopts an inotify mechanism to prevent a serial port from echoing back, virtually connects a serial port to a network, and realizes network transmission of serial port device information, thereby solving the problem of remote access of a serial port terminal device.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of virtualization, and more specifically, to a method, system, device, and storage medium for serial port virtualization. Background Technology

[0002] A virtual serial port (virtual COM port) is a serial port (COM port) generated using the operating system's virtual driver technology. It is a software that simulates a physical serial interface. A virtual serial port has the functions of a real hardware serial port and is recognized as a real serial port by the operating system and serial applications.

[0003] Virtual serial ports have a wide range of applications. Older computers typically included a serial port as standard. However, modern computers generally don't have one. To use serial port functionality, you usually just need to add a USB-to-serial adapter. But if you need to add one or more serial devices without changing the hardware, you need to create one or more virtual serial ports, which function identically to real hardware serial ports. Additionally, virtual serial ports can be used to detect serial input data during program debugging, and multiple applications can communicate using them. However, current virtual serial ports can cause data echoing and may interfere with normal serial communication. Summary of the Invention

[0004] In view of this, the purpose of this invention is to provide a method, system, computer device, and computer-readable storage medium for serial port virtualization. This invention adds an Inotify mechanism to the client program of the virtual serial port to monitor changes in the status of directories or files, and to receive or discard serial port data in a timely manner, thereby realizing the network transmission of serial port device information and solving the problem of remote access to serial port terminal devices.

[0005] To achieve the above objectives, one aspect of this invention provides a method for serial port virtualization, comprising the following steps: creating an Inotify handle through a virtual serial port client program, and adding files or directories to be monitored by calling the Inotify listening function; reading events from the file descriptor of Inotify through the read function of the virtual serial port client program; determining whether to receive data based on the file open flag in response to receiving remote access; deploying the virtual serial port client program to a client, virtualizing the TCP / IP network port as a serial port device, and connecting to the remote serial port through a serial cable to achieve data communication.

[0006] In some implementations, the method further includes: monitoring changes in a file descriptor by setting a file / socket for a selected function call, and waiting for data from a socket, device, or remote connection.

[0007] In some implementations, reading events from the file descriptor of Inotify via the read function of the virtual serial port client program includes: if the read function does not read an event, the read function is blocked until an event occurs; and if the read function reads an event, a buffer containing one or more event structures is returned each time the read function is called.

[0008] In some implementations, the method further includes: setting the bitmask of the event structure as a descriptor that uniquely identifies the monitored item, and determining a file open flag based on the state of the descriptor.

[0009] In another aspect, this invention provides a serial port virtualization system, comprising: a creation module configured to create an Inotify handle through a virtual serial port client program and add files or directories to be monitored by calling the Inotify listening function; a reading module configured to read events from the file descriptor of Inotify through the reading function of the virtual serial port client program; a judgment module configured to determine whether to receive data based on a file open flag in response to receiving remote access; and an execution module configured to deploy the virtual serial port client program to a client, virtualize the TCP / IP network port as a serial port device, and connect to a remote serial port via a serial cable to achieve data communication.

[0010] In some implementations, the system further includes a listening module configured to: monitor changes in file descriptors by setting up a file / socket for a selected function call, and wait for data from a socket, device, or remote connection.

[0011] In some implementations, the read module is configured to: block the read function until an event occurs if the read function does not read an event; and return a buffer containing one or more event structures each time the read function is called if the read function reads an event.

[0012] In some implementations, the system further includes a setting module configured to: set the bitmask of the event structure as a descriptor that uniquely identifies the monitored item, and determine a file open flag based on the state of the descriptor.

[0013] In another aspect of the present invention, a computer device is provided, comprising: at least one processor; and a memory storing computer instructions executable on the processor, the instructions, when executed by the processor, implementing the steps of the method described above.

[0014] In another aspect, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the above-described method steps.

[0015] The present invention has the following beneficial technical effects: by adding an Inotify mechanism to the client program of the virtual serial port to monitor changes in the status of the directory or file, timely receive or discard serial port data, realize network transmission of serial port device information, and solve the problem of remote access to serial port terminal devices. Attached Figure Description

[0016] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other embodiments can be obtained based on these drawings without creative effort.

[0017] Figure 1 A schematic diagram illustrating an embodiment of the serial port virtualization method provided by the present invention;

[0018] Figure 2 This is a schematic diagram of the virtual serial port principle provided by the present invention;

[0019] Figure 3 A schematic diagram illustrating the application principle of the serial port virtualization method provided by this invention;

[0020] Figure 4 A schematic diagram of an embodiment of the serial port virtualization system provided by the present invention;

[0021] Figure 5 A schematic diagram of the hardware structure of an embodiment of the computer device with serial port virtualization provided by the present invention;

[0022] Figure 6 This is a schematic diagram of an embodiment of the serial port virtualization computer storage medium provided by the present invention. Detailed Implementation

[0023] To make the objectives, technical solutions, and advantages of the present invention clearer, the embodiments of the present invention will be further described in detail below with reference to specific examples and the accompanying drawings.

[0024] It should be noted that all uses of "first" and "second" in the embodiments of the present invention are for the purpose of distinguishing two entities or parameters with the same name but different names. It is clear that "first" and "second" are only for the convenience of expression and should not be construed as limiting the embodiments of the present invention. Subsequent embodiments will not explain this in detail.

[0025] In a first aspect, an embodiment of a serial port virtualization method is proposed. Figure 1The diagram shown is a schematic representation of an embodiment of the serial port virtualization method provided by the present invention. Figure 1 As shown, the embodiments of the present invention include the following steps:

[0026] S1. Create an Inotify handle through a virtual serial port client program, and add the file or directory to be monitored by calling the Inotify listening function.

[0027] S2. Read events from the file descriptor of Inotify using the read function of the virtual serial port client program;

[0028] S3. In response to receiving remote access, determine whether to receive data based on the file open flag; and

[0029] S4. Deploy the virtual serial port client program to the client, virtualize the TCP / IP network port as a serial port device, and connect it to the remote serial port through a serial cable to achieve data communication.

[0030] Virtual serial ports achieve multiplexing of physical serial ports via a network, allowing multiple applications to share a single serial port. A schematic diagram of a virtual serial port implementation is shown below. Figure 2 As shown, in a multiplexed scenario, the server's real physical serial port is used to create a virtual serial port. Multiple clients communicate with each other via a virtual serial port and a physical serial port on the server over a network. When a computer (PC) communicates with a client via serial port, the serial port debugging software on the PC can also receive data, and the amount of data received is the number of clients minus one. Therefore, to enable the multiplexed virtual serial port device to adapt to the new network environment, this embodiment of the invention employs an Inotify mechanism-based serial port virtualization method to prevent data echoing, thereby enabling network transmission of serial port device information and solving the problem of remote access to serial port terminal devices.

[0031] Inotify is a file system change notification mechanism that provides Linux with an efficient way to monitor and track file changes. Upon detecting a file system change, it sends an event to the corresponding application, allowing for real-time processing, debugging, and monitoring of file changes. A typical application scenario is a file manager, where ideally, the latest content of a file should be displayed immediately after a user modifies it. Without Inotify, this functionality would typically be achieved through polling, a delayed mechanism that cannot react to file system changes immediately and wastes CPU time. Inotify undoubtedly provides powerful support for implementing event-driven services such as system backups, build services, and file-based program debugging.

[0032] A virtual serial port client program creates an Inotify handle, and then adds the file or directory to be monitored by calling the Inotify listener function. The virtual serial port client program `remserial` creates an Inotify handle; this handle is the Inotify object waiting for data from the listening socket, device, or remote connection. After creating the Inotify handle, the file or directory to be monitored, such as ` / dev / vttyS1`, is added by calling the listener function `Inotify_add_watch()`.

[0033] In some implementations, the method further includes: setting up a file / socket for a select function call to monitor changes in the file descriptor and waiting for data from the socket, device, or remote connection. The file / socket is set up for the select() function call, which is a non-blocking program capable of monitoring changes in the file descriptor to be monitored, such as read / write activity or abnormal conditions. Data is then waited for from the listening socket, device, or remote connection.

[0034] The Remserial program uses the read() function of the virtual serial port client program to read events from the file descriptor of Inotify to determine if a certain event has occurred, such as a file being accessed or modified.

[0035] In some implementations, reading events from the file descriptor of Inotify via the read function of the virtual serial port client program includes: blocking the read function until an event occurs if no event is read; and returning a buffer containing one or more event structures each time the read function is called, in response to an event being read. If no event has occurred at the time of reading, the read() function will block until an event occurs. After an event occurs, each call to the read() function will return a buffer containing one or more Inotify_event structures, i.e., the event that occurred.

[0036] In some implementations, the method further includes: setting the bitmask of the event structure to uniquely identify the descriptor of the monitored item, and determining the file open flag based on the state of the descriptor. The bitmask of the Inotify_event structure is used to uniquely identify the descriptor of this monitored item. It determines whether the descriptor is in an open state; if it is in an open state, the file open flag is set to 1; otherwise, the file open flag is set to 0.

[0037] In response to a received remote access request, the system determines whether to receive data based on the file open flag. When a remote access request is received, the system checks the file open flag. If it is 1, it means the virtual serial port device is open and data can be received. If it is 0, it means the device is not open, and the data is discarded and will not be echoed to the remote access serial port.

[0038] The virtual serial port client program is deployed on the client, virtualizing the TCP / IP network port as a serial device, and connecting it to a remote serial port via a serial cable to achieve data communication. Similarly, the Remserial program is deployed on the client, virtualizing the TCP / IP network port as a serial device, and connecting it to a remote serial port via a serial cable to achieve data communication.

[0039] It should be noted that the steps in each embodiment of the above-mentioned serial port virtualization method can be interleaved, substituted, added, or deleted. Therefore, these reasonable permutations and combinations of the serial port virtualization method should also fall within the protection scope of this invention, and the protection scope of this invention should not be limited to the embodiments.

[0040] Based on the above objectives, a second aspect of this invention proposes a serial port virtualization system. For example... Figure 4 As shown, system 200 includes the following modules: a creation module, configured to create an Inotify handle through a virtual serial port client program and add files or directories to be monitored by calling the Inotify listening function; a reading module, configured to read events from the file descriptor of Inotify through the reading function of the virtual serial port client program; a judgment module, configured to determine whether to receive data based on the file open flag in response to receiving remote access; and an execution module, configured to deploy the virtual serial port client program to the client, virtualize the TCP / IP network port as a serial port device, and connect to the remote serial port through a serial cable to achieve data communication.

[0041] In some implementations, the system further includes a listening module configured to: monitor changes in file descriptors by setting up a file / socket for a selected function call, and wait for data from a socket, device, or remote connection.

[0042] In some implementations, the read module is configured to: block the read function until an event occurs if the read function does not read an event; and return a buffer containing one or more event structures each time the read function is called if the read function reads an event.

[0043] In some implementations, the system further includes a setting module configured to: set the bitmask of the event structure as a descriptor that uniquely identifies the monitored item, and determine a file open flag based on the state of the descriptor.

[0044] Based on the above objectives, a third aspect of the present invention provides a computer device, comprising: at least one processor; and a memory storing computer instructions executable on the processor, the instructions being executed by the processor to perform the following steps: S1, creating an Inotify handle through a virtual serial port client program, and adding a file or directory to be monitored by calling the Inotify listening function; S2, reading events from the file descriptor of Inotify through the read function of the virtual serial port client program; S3, in response to receiving remote access, determining whether to receive data based on a file open flag; and S4, deploying the virtual serial port client program to a client, virtualizing the TCP / IP network port as a serial port device, and connecting to a remote serial port via a serial cable to achieve data communication.

[0045] In some implementations, the steps further include: setting up a file / socket for the selected function call to monitor changes in the file descriptor and waiting for data from the socket, device, or remote connection.

[0046] In some implementations, reading events from the file descriptor of Inotify via the read function of the virtual serial port client program includes: if the read function does not read an event, the read function is blocked until an event occurs; and if the read function reads an event, a buffer containing one or more event structures is returned each time the read function is called.

[0047] In some implementations, the steps further include: setting the bitmask of the event structure to a descriptor that uniquely identifies the monitored item, and determining a file open flag based on the state of the descriptor.

[0048] like Figure 5 The diagram shown is a hardware structure schematic of an embodiment of the serial port virtualization computer device provided by the present invention.

[0049] For example Figure 5 Taking the device shown as an example, the device includes a processor 301 and a memory 302.

[0050] Processor 301 and memory 302 can be connected via a bus or other means. Figure 5 Taking the example of a connection between China and Israel via a bus.

[0051] The memory 302, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs, non-volatile computer-executable programs, and modules, such as the program instructions / modules corresponding to the serial port virtualization method in this embodiment. The processor 301 executes various server functions and data processing by running the non-volatile software programs, instructions, and modules stored in the memory 302, thereby implementing the serial port virtualization method.

[0052] Memory 302 may include a program storage area and a data storage area. The program storage area may store the operating system and applications required for at least one function; the data storage area may store data created using serial port virtualization methods, etc. Furthermore, memory 302 may include high-speed random access memory and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other non-volatile solid-state storage device. In some embodiments, memory 302 may optionally include memory remotely located relative to processor 301, and these remote memories can be connected to the local module via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.

[0053] One or more serial port virtualization methods are stored in memory 302. When executed by processor 301, the serial port virtualization method in any of the above method embodiments is executed.

[0054] Any embodiment of the computer device that performs the above-described serial port virtualization method can achieve the same or similar effects as any of the aforementioned method embodiments.

[0055] The present invention also provides a computer-readable storage medium storing a computer program that, when executed by a processor, performs a method for serial port virtualization.

[0056] like Figure 6 The diagram shown is a schematic representation of an embodiment of the serial port virtualization computer storage medium provided by the present invention. Figure 6 Taking the computer storage medium shown as an example, the computer-readable storage medium 401 stores a computer program 402 that, when executed by a processor, performs the above method.

[0057] Finally, it should be noted that those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program for the serial port virtualization method can be stored in a computer-readable storage medium. When executed, the program can include the processes of the embodiments of the above methods. The storage medium for the program can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc. The above computer program embodiments can achieve the same or similar effects as any of the corresponding foregoing method embodiments.

[0058] The above are exemplary embodiments disclosed in this invention. However, it should be noted that various changes and modifications can be made without departing from the scope of the embodiments of this invention as defined by the claims. The functions, steps, and / or actions of the methods according to the disclosed embodiments described herein do not need to be performed in any particular order. Furthermore, although the elements disclosed in the embodiments of this invention may be described or claimed individually, they may be understood as multiple unless explicitly limited to a singular number.

[0059] It should be understood that, as used herein, the singular form “a” is intended to include the plural form as well, unless the context clearly supports an exception. It should also be understood that, as used herein, “and / or” refers to any and all possible combinations of one or more of the associated listed items.

[0060] The embodiment numbers disclosed in the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.

[0061] Those skilled in the art will understand that all or part of the steps of the above embodiments can be implemented by hardware or by a program instructing related hardware. The program can be stored in a computer-readable storage medium, such as a read-only memory, a disk, or an optical disk.

[0062] Those skilled in the art should understand that the discussion of any of the above embodiments is merely exemplary and is not intended to imply that the scope of the invention (including the claims) is limited to these examples. Within the framework of the invention, technical features of the above embodiments or different embodiments can be combined, and many other variations of different aspects of the invention exist, which are not provided in the details for the sake of brevity. Therefore, any omissions, modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the invention should be included within the protection scope of the invention.

Claims

1. A method for serial port virtualization, characterized in that, Includes the following steps: Create an Inotify handle using a virtual serial port client program, and add the file or directory to be monitored by calling the Inotify listener function. The virtual serial port client program reads events from the file descriptor of Inotify using its read function, including: if the read function does not read an event, it is blocked until an event occurs; and if the read function reads an event, it returns a buffer containing one or more event structures each time the read function is called. Set the bitmask of the event structure to a descriptor that uniquely identifies the monitored item, and determine the file open flag based on the state of the descriptor; Determine if the file descriptor is open. If it is open, set the file open flag to 1; otherwise, set it to 0. In response to receiving remote access, determine whether to receive data based on the file open flag; If it is 1, it means that the virtual serial port device has been opened and can receive data. If it is 0, it means that the device has not been opened, and the data will be discarded and will not be echoed to the remote access serial port. The virtual serial port client program is deployed to the client, which virtualizes the TCP / IP network port as a serial port device and connects to the remote serial port through a serial cable to achieve data communication.

2. The method according to claim 1, characterized in that, The method further includes: By setting up a file / socket for a selection function call, you can monitor changes to the file descriptor and wait for data from the socket, device, or remote connection.

3. A serial port virtualization system, characterized in that, include: Create a module, configure it to create an Inotify handle through a virtual serial port client program, and add files or directories to be monitored by calling the Inotify listener function; The read module is configured to read events from the file descriptor of Inotify via the read function of the virtual serial port client program; The judgment module is configured to respond to received remote access by determining whether to receive data based on the file open flag. The execution module is configured to deploy the virtual serial port client program to the client, virtualize the TCP / IP network port as a serial port device, and connect it to the remote serial port through a serial cable to achieve data communication. The reading module is configured to: If the read function fails to read an event, it is blocked until an event occurs; and In response to the read function reading an event, each call to the read function returns a buffer containing one or more event structures; The system also includes a settings module configured to: set the bitmask of the event structure as a descriptor that uniquely identifies the monitored item, and determine the file open flag based on the state of the descriptor; The judgment module is also used to: determine whether the descriptor is in an open state; if it is in an open state, set the file open flag to 1; otherwise, set the file open flag to 0. If the value is 1, it means that the virtual serial port device is open and can receive data. If the value is 0, it means that the device is not open, and the data will be discarded and will not be echoed to the remote access serial port.

4. The system according to claim 3, characterized in that, The system also includes a monitoring module configured for: By setting up a file / socket for a selection function call, you can monitor changes to the file descriptor and wait for data from the socket, device, or remote connection.

5. A computer device, characterized in that, include: At least one processor; as well as A memory storing computer instructions executable on the processor, which, when executed by the processor, implement the steps of the method according to any one of claims 1-2.

6. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1-2.

Citation Information

Patent Citations

  • Communication apparatus and method based on Ethernet virtual serial port technology and LTE wireless network

    CN106713411A