Code system controller application layer text remote output method and system
Patent Information
- Application Number
- CN202310691968.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-12
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2043-06-12
AI Technical Summary
[0010]3)使用第三方显示屏时,不同厂家的显示屏使用不同的设计软件,不具有通用性;
[0046]1)本方法不针对特定类型的显示屏,在上位机支持的任何屏幕中均可以查看CODESYS控制器远程输出的文本信息;
Smart Images

Figure CN116860474B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of text transmission technology, and particularly relates to a method and system for remote text output at the application layer of the CODESYS controller. Background Technology
[0002] The statements in this section are merely background information related to the present invention and do not necessarily constitute prior art.
[0003] CODESYS is a software development platform for programmable logic controllers (PLCs) developed by 3S (3S-Smart Software Solutions GmbH) in Germany. Due to its advantages such as hardware platform independence, openness, reconfigurability, modularity, and compliance with the IEC61131-3 international standard, many manufacturers in the field of modern industrial automation have developed programmable logic controllers or motion controllers based on the CODESYS software platform.
[0004] CODESYS includes an integrated development environment (IDE) and a runtime system for programming. After completing the controller hardware design and driver development, the controller manufacturer ports and installs the CODESYS runtime system onto the controller for distribution and sale. To use the CODESYS controller, application development is required through the CODESYS IDE, which compiles and generates binary code. This code is then downloaded to the CODESYS controller via Ethernet or serial port. After debugging to ensure the program, logic, and functionality are correct, final deployment and installation are performed.
[0005] There is a need for text message output in both the development and post-deployment operation phases of CODESYS controller application layer programs (especially multi-tasking applications): during the development phase, outputting key operational statuses or data in text format assists application developers in program development and debugging; during the post-deployment operation phase, outputting data of interest or operational statuses in text format assists maintenance personnel in equipment operation, maintenance, and analysis. These text message output needs have the following characteristics: the information content may change frequently, the text volume may be large, there is a need for remote output and viewing, and there is a need for recording and saving.
[0006] For CODESYS controllers, the primary method for outputting text information at the application layer is currently through displaying text information in a visual interface. Specifically, there are two methods: using the visualization toolkit provided by CODESYS and using a third-party display. The inventors discovered that both methods require application developers to create the visualization interface and bind the data to be output as text to the visual text elements. Furthermore, the former requires additional payment, and the latter, in addition to purchasing a third-party display, requires additional configuration or programming of the controller to work with the display via a bus interface.
[0007] In addition, displaying text information through a visual interface has the following drawbacks:
[0008] 1) Although the output text information can be read and observed on the screen, it is not suitable for outputting large amounts of frequently changing information;
[0009] 2) To save and record this text information, additional code needs to be written;
[0010] 3) When using third-party displays, different manufacturers use different design software, which is not universal;
[0011] 4) Visual interfaces are usually designed for operators at the controller's operating site, and some text information is not convenient to display on the visual interface;
[0012] 5) Visual interfaces are generally deployed at the operational site, which is not conducive to remote viewing;
[0013] 6) Text information displayed in the visualization interface is not easy to quickly export to a computer for further use and analysis with the help of other tools. Summary of the Invention
[0014] To overcome the shortcomings of the prior art, the present invention provides a method and system for remote text output at the application layer of the CODESYS controller. The method for remote text output at the application layer of the CODESYS controller is based on the TCP / IP protocol and uses an Ethernet interface to remotely output text information generated in the CODESYS controller to a host computer in a timely manner so that personnel can view, record and save it.
[0015] To achieve the above objectives, one or more embodiments of the present invention provide the following technical solutions:
[0016] The first aspect of this invention provides a method for remote text output at the application layer of a CODESYS controller.
[0017] The CODESYS controller application layer text remote output method includes the following steps:
[0018] The host computer obtains the IP address and port number of the CODESYS controller to be interacted with, and establishes a TCP connection with the CODESYS controller to be interacted with.
[0019] The CODESYS controller sets a value representing the text information level for the text information to be output, and transmits the text information to be output and the value representing the text information level to the host computer.
[0020] The host computer displays the text information and the numerical values representing the text information levels. At the same time, it creates log level files in the host computer that correspond to the numerical values representing the text information levels through NLog. By matching the numerical values representing the text information levels with the log level files, the text information is automatically saved to the corresponding log level file, thus realizing the hierarchical output of text information in the log file.
[0021] Preferably, it also includes adding a remote text output library to the CODESYS controller, and using the remote text output library to remotely output text information. The method for establishing the remote text output library is as follows:
[0022] S11: Add the SysSocket, SysSem, and SysMem libraries in the library manager of CODESYS IDE;
[0023] S12: Implement a linked list-based queue type and some functions for operating on this queue type;
[0024] S13: Add multiple global variables to the global variable list for global data transfer;
[0025] S14: Implement a FUNCTION using ST language to add the text information to be sent to the global queue object;
[0026] S15: Implement a PROGRAM using ST language to retrieve information from the aforementioned global queue object and send it out via TCP protocol;
[0027] S16: Encapsulates the queue types, functions, programs, and global variables implemented in S12 to S15 into a CODESYS library file.
[0028] Preferably, the added global variables include one string constant, which is sent to the host computer when the TCP communication connection is first established, as an identification mark of the CODESYS controller.
[0029] Preferably, the implementation steps of the FUNCTION described in S14 are as follows:
[0030] S14-1: Define two input variables for this FUNCTION: one INT type variable representing the level of the text information; and one string type variable representing the text information to be added.
[0031] S14-2: Check the validity of the input variables at the beginning of the FUNCTION. Continue only if the length is greater than 0; otherwise, return.
[0032] S14-3: Convert the INT type variable representing text information level described in S14-1 into a string, append a separator ":" to this string, and then append the string type variable representing text information described in S14-1 to the end of the string to form the string to be sent.
[0033] Preferably, the process for creating log-level files is as follows:
[0034] Implement a LogTools class in the host computer. Define an enumeration LOGLEVEL in the LogTools class and implement a public static function WriteLog. The enumeration LOGLEVEL represents the log level of text information. The WriteLog function is used to pass a given string to NLog according to the given level, so as to log the log level file and output logs in different levels.
[0035] Preferably, the text information is automatically saved to the corresponding log level file, specifically:
[0036] Implement the WriteTCPLog function in the host computer. The WriteTCPLog function has two input parameters: a LOGLEVEL variable representing the log level of the text information and a string variable representing the text information. The obtained text information is truncated after the first ":" and added to the text box used to display the text information. The string before the ":" is converted into the log level. Then, the LogTools.WriteLog function is used to output the string after the first ":" of the text information to the log level file corresponding to the log level.
[0037] Preferably, after the host computer obtains the IP address and port number of the CODESYS controller to be interacted with, it first determines whether it is the first time reading. If it is the first time reading, it determines whether the CODESYS controller to be interacted with is a CODESYS controller that supports remote output of text information based on the CODESYS controller's identification flag. Only after determining that the other party is a CODESYS controller that supports remote output of text information will the text information sent by the other party be received.
[0038] A second aspect of the present invention provides a CODESYS controller application layer text remote output system.
[0039] CODESYS controller application layer text remote output system, including:
[0040] The connection establishment module is configured to: obtain the IP address and port number of the CODESYS controller to be interacted with from the host computer, and establish a TCP connection with the CODESYS controller to be interacted with;
[0041] The transmission module is configured such that the CODESYS controller to be interacted with sets a value representing the text information level for the text information to be output, and transmits the text information to be output and the value representing the text information level to the host computer.
[0042] The display and save module is configured as follows: the host computer displays the text information and the numerical values representing the text information levels, and at the same time, it creates a log level file in the host computer corresponding to the numerical values representing the text information levels through NLog. The system matches the numerical values representing the text information levels with the log level files and automatically saves the text information to the corresponding log level file, thereby realizing the hierarchical output of text information in the log file.
[0043] A third aspect of the present invention provides a computer-readable storage medium having a program stored thereon that, when executed by a processor, implements the steps of the CODESYS controller application layer text remote output method as described in the first aspect of the present invention.
[0044] A fourth aspect of the present invention provides an electronic device including a memory, a processor, and a program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps in the CODESYS controller application layer text remote output method as described in the first aspect of the present invention.
[0045] The above one or more technical solutions have the following beneficial effects:
[0046] 1) This method is not specific to any particular type of display screen; the text information remotely output by the CODESYS controller can be viewed on any screen supported by the host computer.
[0047] 2) Developers of CODESYS controllers do not need to draw a visual interface, making them simple and quick to use without affecting the visual interface deployed on-site.
[0048] 3) It can remotely output text information, which only requires the CODESYS controller to have an Ethernet interface and the host computer to establish a TCP connection with the CODESYS controller through the network. This is easily met in the current industrial automation environment.
[0049] 4) It can record a large amount of frequently changing text information, and the amount of text information is only limited by the network transmission speed and the memory and hard disk capacity of the host computer;
[0050] 5) It is applicable to CODESYS controllers. The implementation methods in this technical solution are all based on CODESYS's general library, and therefore are universal for CODESYS controllers.
[0051] 6) The received text information can be automatically saved in the host computer for further use and analysis.
[0052] Advantages of additional aspects of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description
[0053] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.
[0054] Figure 1 This is a flowchart of the method in the first embodiment.
[0055] Figure 2 This is a flowchart illustrating the implementation method of a remote text information output library for CODESYS.
[0056] Figure 3 This is a flowchart showing the steps corresponding to each state of the PROGRAM state machine in S15.
[0057] Figure 4 This is a flowchart illustrating the implementation method of the host computer software.
[0058] Figure 5 This is a diagram showing the steps corresponding to each state of the core function tcpMain and its state machine in the host computer software.
[0059] Figure 6 This is a flowchart illustrating the method for remotely outputting text information using a text information remote output library in the CODESYS IDE.
[0060] Figure 7 A flowchart illustrating the method for remotely receiving text information from the CODESYS controller in a host computer. Detailed Implementation
[0061] It should be noted that the following detailed descriptions are exemplary and intended to provide further illustration of the invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains.
[0062] It should be noted that the terminology used herein is for the purpose of describing particular implementations only and is not intended to limit the exemplary implementations of the present invention.
[0063] Where there is no conflict, the embodiments and features in the embodiments of the present invention can be combined with each other.
[0064] Terminology Explanation:
[0065] 1) PLC: Programmable Logic Controller;
[0066] 2) IEC 61131 Standard: The IEC 61131 standard is a programmable logic controller standard developed by the International Electrotechnical Commission (IEC). It consists of 10 parts, each describing a different part of the programmable logic controller.
[0067] 3) IEC61131-3 standard: IEC61131-3 is the third part of the IEC61131 standard, which defines the software structure, programming language and program execution method of PLC;
[0068] 4) CODESYS: CODESYS is a software development platform for programmable logic controllers (PLCs) developed by 3S (3S-Smart Software Solutions GmbH) in Germany. CODESYS includes an integrated development environment (IDE) and a runtime system for programming. The CODESYS IDE supports the IEC61131-3 standard.
[0069] 5) CODESYS Controller: This refers to a controller containing the CODESYS runtime system. After the controller manufacturer designs the controller hardware and drivers, they port and install the CODESYS runtime system into the controller, thus forming a CODESYS controller. Users of the controller manufacturer purchase a CODESYS controller and use the CODESYS IDE for application development. After compilation, binary machine code is generated and downloaded to the CODESYS controller via Ethernet or serial port. With the support of the CODESYS runtime system, this machine code is executed to achieve the target function.
[0070] 6) CODESYS Controller Application Layer: After purchasing a CODESYS controller, users of the controller manufacturer need to develop programs using the CODESYS IDE to meet specific application requirements. The development work they complete is called application layer development.
[0071] 7) Remote Text Output: This refers to outputting key information in text format to a computer over a network during the operation of the controller application. There is a need for text output during the development of the CODESYS controller application layer. By outputting text information, developers can track and analyze the execution process of the application layer program, thus compensating for the shortcomings of using CODESYS debugging tools for program debugging. Furthermore, during normal use after the controller is deployed and installed, outputting text information can also help maintenance personnel perform routine maintenance and fault analysis of the controller. The significance of remote output is that the reception and viewing of text information does not require on-site visits.
[0072] 8) TASK: A concept defined in the IEC 61131-3 standard to describe a software model, meaning "task". A TASK can be executed, such as through periodic loops or event-triggered execution. A TASK can contain one or more PROGRAMs, which are the programs that the TASK is to execute.
[0073] 9) PROGRAM: A concept defined in the IEC 61131-3 standard to describe a software model, meaning "program". A PROGRAM can be added to a TASK and thus executed, or it can be called and executed by another PROGRAM that has been added to a TASK.
[0074] 10) FUNCTION: A concept defined in the IEC 61131-3 standard for describing software models, meaning "function". A FUNCTION can be called by a PROGRAM or other FUNCTIONs.
[0075] 11) STRUCT: A concept defined in the IEC 61131-3 standard for describing software models, meaning "structure", used to define structure types;
[0076] 12) FUNCTION_BLOCK: The IEC 61131-3 standard defines a concept for describing software models, meaning "functional block", and is used to define the type of functional block.
[0077] Example 1
[0078] This embodiment discloses a method for remote text output at the application layer of the CODESYS controller.
[0079] like Figures 1-7 As shown, the CODESYS controller application layer text remote output method includes the following steps:
[0080] The host computer obtains the IP address and port number of the CODESYS controller to be interacted with, and establishes a TCP connection with the CODESYS controller to be interacted with.
[0081] The CODESYS controller sets a value representing the text information level for the text information to be output, and transmits the text information to be output and the value representing the text information level to the host computer.
[0082] The host computer displays the text information and the numerical values representing the text information levels. At the same time, it creates log level files in the host computer that correspond to the numerical values representing the text information levels through NLog. By matching the numerical values representing the text information levels with the log level files, the text information is automatically saved to the corresponding log level file, thus realizing the hierarchical output of text information in the log file.
[0083] Specifically, the conditions for implementing this method are: the CODESYS controller has an Ethernet interface and supports TCP-based Socket communication.
[0084] The overall technical solution of this method is as follows:
[0085] This method implements a remote text output library for CODESYS and a host computer software for receiving remote text information. The remote text output library sends text information, and the host computer software remotely receives text information. Specifically, this method includes the following four parts:
[0086] S1. An implementation method for a remote text information output library suitable for CODESYS;
[0087] S2, Implementation method of a host computer software;
[0088] S3. A method for remotely outputting text information using a text information remote output library in CODESYS IDE;
[0089] S4. A method for remotely receiving text information from the CODESYS controller in a host computer.
[0090] The implementation method of the remote text information output library applicable to CODESYS described in S1 specifically includes the following steps:
[0091] S11: Add the SysSocket, SysSem, and SysMem libraries in the library manager of CODESYS IDE;
[0092] S12: Implement a linked list-based queue type and some FUNCTIONs (IEC 61131-3 standard term, meaning "function") for operating on this queue type;
[0093] S13: Add necessary global variables to the global variable list for global data transfer;
[0094] S14: Implement a FUNCTION using ST language to add the text information to be sent to the global queue object;
[0095] This function will be invoked in S3 (specifically step S35) and used directly by the text message sender.
[0096] S15: Implement a PROGRAM (IEC61131-3 standard term, meaning "program") using ST language to retrieve information from the aforementioned global queue object and send it out via TCP protocol; it specifically implements the process of sending text information.
[0097] S16: Further encapsulate the queue types, FUNCTION, PROGRAM, and global variables implemented in S12 to S15 into a CODESYS library file, hide the queue types and FUNCTION implemented in S12, hide the global variables added in S13, and only expose the FUNCTION mentioned in S14 and the PROGRAM mentioned in S15.
[0098] The implementation of a linked list-based queue type and some functions for operating on this queue type, as described in S12, includes the following steps:
[0099] S12-1: Define a STRUCT type to represent a linked list node, containing the following member variables: 1 POINTER TOBYTE type member variable, representing the pointer to the data stored in this node; 1 INT type member variable, representing the length of the data stored in this node; 1 pointer type member variable pointing to this STRUCT type, representing the pointer to the next linked list node after this node in the linked list.
[0100] S12-2: Define a STRUCT type to represent a queue type, which includes the following member variables: 1 INT type member variable representing the queue length; 1 INT type member variable representing the maximum queue length; 1 pointer member variable pointing to the STRUCT type described in S12-1, representing the queue head node pointer; 1 pointer member variable pointing to the STRUCT type described in S12-1, representing the queue tail node pointer.
[0101] S12-3: Define a FUNCTION to implement the object initialization function of the queue type defined in S12-2;
[0102] S12-4: Define a FUNCTION to implement the function of obtaining the head node element pointer of the queue type object defined in S12-2;
[0103] S12-5: Define a FUNCTION to implement the function of removing the head node element from the queue type object defined in S12-2;
[0104] S12-6: Define a FUNCTION to implement the function of generating a linked list node type object as described in S12-1 based on the given data and size;
[0105] S12-7: Define a FUNCTION to add a new node element to the tail of the queue type object defined in S12-2. If the length exceeds the maximum length of the queue described in S12-2, remove the head node and then add it to the tail.
[0106] S12-8: Define a FUNCTION to implement the function of obtaining valid data from the linked list node type object described in S12-1.
[0107] The global variables added in S13 include:
[0108] S13-1: A queue-type object implemented in S12, used to store the text information to be sent. The producer is FUNCTION as described in S14, and the consumer is PROGRAM as described in S15.
[0109] S13-2: A semaphore handle used to control secure access to the queue object described in S13-1;
[0110] S13-3: A string constant sent to the client as an identification mark when a TCP connection is first established.
[0111] The steps for implementing the FUNCTION described in S14 are as follows:
[0112] S14-1: For this FUNCTION, define two input variables: one INT type variable, representing the level of the text information; and one string type variable, representing the text information to be added to the queue object described in S13-1.
[0113] S14-2: Check the validity of the input variables at the beginning of the FUNCTION. Continue only if the length is greater than 0; otherwise, return.
[0114] S14-3: Convert the INT type variable representing the text information level described in S14-1 into a string, append a separator ":" to the end of this string, and then append the string type variable representing the text information described in S14-1 to the end of the string to form the string to be sent.
[0115] This step, together with S24-2, enables the hierarchical output of text information in the log file;
[0116] S14-4: Lock the semaphore described in S13-2, access the queue object described in S13-1, use the FUNCTION described in S12-6 to generate the linked list node type object described in S12-1 based on the string data concatenated in S14-3, add it to the tail of the queue, and finally unlock the semaphore.
[0117] The PROGRAM described in S15 is implemented based on a state machine, and the specific steps are as follows:
[0118] S15-1: Define the following input variables: 1 WORD type variable, representing the port number the TCP server will listen on. Define the following output variables: 1 BOOL type variable, representing whether the PROGRAM described in S15 is ready, with an initial value of false, indicating that it is not ready. Define the following local variables: 1 queue type object described in S12-2, representing the local queue to be sent; 1 INT type variable, representing the state machine state; 2 RTS_IEC_RESULT type variables, one representing the socket handle used for listening, and the other representing the socket handle used for connecting to the client; 1 POINTER TO BYTE type variable, representing a pointer to the data expected to be sent this week, with an initial value of 0.
[0119] S15-2: When PROGRAM starts execution, it first determines whether it is ready based on the output variables described in S15-1. If it is not ready, it executes S15-3. If it is ready, it executes S15-4.
[0120] S15-3: Initialize relevant variables, including: initializing the global queue object described in S13-1 using the FUNCTION described in S12-3; initializing the local queue object to be sent in S15-1 using the FUNCTION described in S12-3; creating the semaphore handle described in S13-2; setting the state machine state variables described in S15-1 to 0; and setting the output variables described in S15-1 to true, indicating that preparation is complete. After this step is completed, return and wait for the next execution of this PROGRAM.
[0121] S15-4: Read the state variables of the state machine described in S15-1, execute the steps corresponding to the state values, return after execution, and wait for the next execution of this PROGRAM.
[0122] The steps corresponding to the state value described in S15-4 include:
[0123] S15-4.0: Corresponding to state 0, the execution steps are as follows: Use the SysSockCreate function to create a Socket socket using the TCP protocol, and store it in the socket handle for listening described in S15-1; set the state machine state variable described in S15-1 to 1;
[0124] S15-4.1: Corresponding to state 1, the execution steps are as follows: Use the SysSockIoctl function to set the socket handle used for listening in S15-1 to non-blocking mode; if the setting is successful, set the state machine state variable in S15-1 to 2; if the setting fails, set the state machine state variable in S15-1 to 6.
[0125] S15-4.2: Corresponding to state 2, the execution steps are as follows: Use the SysSockBind function to bind the socket handle used for listening as described in S15-1 to the local IP address and the port number specified by the input variable as described in S15-1; if the binding is successful, set the state machine state variable described in S15-1 to 3; if the binding fails, set the state machine state variable described in S15-1 to 6.
[0126] S15-4.3: Corresponding to state 3, the execution steps are as follows: Use the SysSockListen function to start listening on the socket handle used for listening as described in S15-1; if the listening is successful, set the state variable of the state machine described in S15-1 to 4; if the listening fails, set the state variable of the state machine described in S15-1 to 6.
[0127] S15-4.4: Corresponding to state 4, the execution steps are as follows: Use the SysSockSelect function to check the readability of the socket handle used for listening as described in S15-1, and give a timeout period; if the timeout occurs, the execution steps of this state end; if the SysSockSelect function fails, the state machine state variable described in S15-1 is set to 6, and the execution steps of this state end; if the timeout does not occur, and the function executes correctly and is readable, the subsequent steps of this state continue to be executed; use the SysSockAccept function to accept the client's connection request and create a new socket; if a valid socket handle is created, it is stored in the socket handle variable used for connecting with the client as described in S15-1, the expected data pointer for this week is set to the identification string described in S13-3, and the state machine state variable described in S15-1 is set to 5; if a valid socket handle is not created, the state machine state variable described in S15-1 is set to 6.
[0128] The step of “pointing the data pointer to be sent this week to the identification string described in S13-3” is a key point, which together with the subsequent step S24-3.2 realizes the identification during the first TCP communication.
[0129] S15-4.5: Corresponding to state 5, the execution steps are as follows: Attempt to lock the semaphore described in S13-2. If the lock is successful, use the relevant FUNCTION defined in S12 to remove the head node from the global queue object described in S13-1 and add it to the tail of the local queue to be sent described in S15-1. Then unlock the semaphore described in S13-2. If the pointer to the data expected to be sent this week in S15-1 is 0, use the relevant FUNCTION defined in S12 to attempt to remove the head node from the local queue to be sent described in S15-1, obtain the data pointer in the node, assign it to the pointer to the data expected to be sent this week in S15-1, and obtain the data size for storage. Again, determine whether the pointer to the data expected to be sent this week in S15-1 is 0. If it is not 0, use SysSockSelec. The `t` function determines the writability of the socket handle used for connecting to the client as described in S15-1 and provides a timeout. If the timeout occurs, the execution steps of this state end. If the `SysSockSelect` function fails, the state machine state variable described in S15-1 is set to 6, and the execution steps of this state end. If the timeout does not occur, the function executes correctly, and the socket is writable, the subsequent steps of this state continue. The `SysSockSend` function is used to send the data pointed to by the pointer to the expected data to be sent this week as described in S15-1 to the client represented by the socket handle used for connecting to the client as described in S15-1. If the sending fails, the state machine state variable described in S15-1 is set to 6. If the sending succeeds, the pointer to the expected data to be sent this week as described in S15-1 is released and reset to 0, and the data size is reset to 0.
[0130] S15-4.6: Corresponding to state 6, the execution steps are as follows: determine whether the socket handle used for listening in S15-1 is an invalid handle; if it is not an invalid handle, close it using the SysSockClose function; determine whether the socket handle used for connecting to the client in S15-1 is an invalid handle; if it is not an invalid handle, close it using the SysSockClose function; set the state machine state variable in S15-1 to 0.
[0131] The implementation method of the host computer software described in S2 is based on the .NET platform and implemented using the C# language. The core of this method is to implement a TCP client. When establishing a connection with the TCP server, the client performs identity recognition based on the first string received, and determines whether the other party is a CODSYS controller that supports remote output of text information. If it is, all subsequent text information received will be displayed and output to the log through the NLog framework.
[0132] This host computer software is essentially a TCP client. The implementation process of the TCP client is not the key point, but the identity recognition and NLog log output added when implementing the TCP client are the key points.
[0133] Specifically, it includes the following steps:
[0134] S21: Design the host computer software interface, which includes: a text box for inputting the CODESYS controller IP address, a text box for inputting the CODESYS controller port number, a text box for displaying text information, a button for initiating a TCP connection, and a button for disconnecting a TCP connection.
[0135] S22: Add a reference to NLog and configure the log output rules, including outputting logs as files, naming rules for log output file names, storage path for log output files, size of a single log output file, and output layout style;
[0136] S23: Implement a LogTools class to output given text information to a log file using NLog; this is a key point, involving outputting text information to a log file using NLog.
[0137] S24: Implement a TCPThreadCls class to represent TCP threads and their resources, and manage TCP communication;
[0138] S25: Implement the click event function of the connection button described in S21, which is used to instantiate a TCPThreadCls object and connect to the CODESYS controller through the TCPThreadCls object;
[0139] S26: Implement the click event function of the disconnect button described in S21, used to disconnect the TCP connection with the CODESYS controller.
[0140] In the LogTools class described in S23, an enumeration LOGLEVEL is defined, and a public static function WriteLog is implemented. The enumeration LOGLEVEL represents the log level of the text information to be output, and the WriteLog function is used to pass the given string to NLog according to the given level for log output. This is a key point, which involves outputting text information to the log file through NLog.
[0141] The specific implementation steps of the TCPThreadCls class described in S24 are as follows:
[0142] S24-1: Implement the constructor, instantiate a Thread object in the constructor, use the tcpMain function of the TCPThreadCls class as the entry function of this Thread object, store this Thread object in the public member variable tcpThread of the TCPThreadCls class, and set it as a background thread;
[0143] S24-2: Implement the WriteTCPLog function, which takes two input parameters: a LOGLEVEL variable representing the log level of the text message and a string variable representing the text message. This function extracts the substring after the first colon from the given text message and adds it to the text box used to display the text message as described in S21. The substring before the colon is converted into a log level, and then output to the log file using the LogTools.WriteLog function. This is a crucial point, involving extracting the information level from the text message and outputting it to the log file.
[0144] S24-3: Implement the tcpMain function. The main body of this function is an infinite while loop. The loop body contains a state machine, with int variables representing the state machine states. Before executing the while loop, the state value is set to 0. In the while loop, at the beginning of each iteration, the state machine state value is read first, and then the step corresponding to the state machine state value is executed.
[0145] The steps corresponding to the state machine state values described in 24-3 include:
[0146] S24-3.0: Corresponding to state 0, the execution steps are: instantiate a Socket object using the TCP protocol and store it in the clientSocket variable; set the state machine state value described in S24-3 to 1;
[0147] S24-3.1: Corresponding to state 1, the execution steps are as follows: Execute the clientSocket.Connect function to connect the IP and port number given in the text box for inputting the CODESYS controller IP and the text box for inputting the CODESYS controller port number mentioned in S21. If there is no abnormality, set the state machine state value mentioned in S24-3 to 2.
[0148] S24-3.2: Corresponding to state 2, the execution steps are as follows: Execute the clientSocket.Poll function to check the readability of the data. If it is readable, execute the clientSocket.Receive function. If the reading is successful and it is the first reading, determine whether the other party is a CODESYS controller that supports remote output of text information based on whether the header of this string contains the identification string described in S13-3. If not, set the state machine state value described in S24-3 to 4 and end this step. If the reading is successful and it is not the first reading, call the WriteTCPLog function to output the text information to the software interface and log. If a socket connection is closed, reset, terminated, or other abnormalities are encountered during the execution steps of this state, set the state machine state value described in S24-3 to 3.
[0149] Key points in this step include: identity verification, and calling the WriteTCPLog function to output text information to the software interface and logs.
[0150] S24-3.3: Corresponding to state 3, the execution steps are as follows: execute the clientSocket.Shutdown function to close the data transmission, then execute clientSocket.Close to disconnect and release resources, and set the state machine state value described in S24-3 to 0 so that the socket can be recreated and the connection established in the next loop;
[0151] S24-3.4: Corresponding to state 4, the execution steps are as follows: execute the clientSocket.Shutdown function to close the data transmission, then execute clientSocket.Close to disconnect and release resources, and finally execute break to exit the while loop.
[0152] The click event function described in S25 above is implemented by instantiating a TCPThreadCls object and then executing the tcpThread.Start function of this TCPThreadCls object to start the TCP thread to connect to the CODESYS controller and automatically receive and output text information.
[0153] The click event function described in S26 above is implemented by setting the state machine state value described in S24-3 to 4, and then checking whether it was successfully set to 4 after a certain period of sleep. If it is unsuccessful, it will keep trying to set it to 4. If it is successful, step S24-3.4 will be executed, and then the TCP connection with the CODESYS controller will be disconnected.
[0154] The method described in S3 for remotely outputting text information using a remote text information output library in the CODESYS IDE refers to the method used by CODESYS controller application developers to remotely output text information using the remote text information output library described in S1, which includes the following steps:
[0155] S31: Application developers of the CODESYS controller should add the aforementioned remote output library for text information to the project or library they develop.
[0156] S32: Implement a PROGRAM using ST language, execute the PROGRAM described in S15 in this PROGRAM, and set the port number that the TCP server should listen on by input parameters;
[0157] S33: Add a periodically executed TASK (IEC 61131-3 standard term, meaning "task"), in which a call to the PROGRAM described in S32 is added;
[0158] S34: At the beginning of all PROGRAMs that need to remotely output text information, read the output variable described in S15-1, and determine whether the PROGRAM described in S15 is ready. If it is not ready, return directly.
[0159] S35: Whenever remote output of text information is required, directly call the FUNCTION described in S14, and pass in the text information string to be output and the numerical value representing the text information level.
[0160] The method described in S4 for remotely receiving text information from a CODESYS controller in a host computer refers to receiving text information sent by the CODESYS controller through the method described in S3 for remotely outputting text information using a remote text information output library in the CODESYS IDE, by running the host computer software described in S2 on the host computer. The method includes the following steps:
[0161] S41: First, ensure that the host computer and the CODESYS controller can establish a TCP connection via the network (which can be remote or local);
[0162] S42: Run the host computer software implemented by the host computer software implementation method described in S2 on the host computer, fill in the IP and port number of the CODESYS controller, and click the connect button;
[0163] S43: Once the host computer software successfully connects to and determines that the other party is a CODESYS controller that supports remote output of text information, it can receive the text information sent by the other party and automatically display the text information in the text box in the software interface. It will also automatically save it in the log file on the host computer at the same time.
[0164] S44: To end reception, click the disconnect button;
[0165] Example 2
[0166] This embodiment discloses a CODESYS controller application layer remote text output system.
[0167] CODESYS controller application layer text remote output system, including:
[0168] The connection establishment module is configured to: obtain the IP address and port number of the CODESYS controller to be interacted with from the host computer, and establish a TCP connection with the CODESYS controller to be interacted with;
[0169] The transmission module is configured such that the CODESYS controller to be interacted with sets a value representing the text information level for the text information to be output, and transmits the text information to be output and the value representing the text information level to the host computer.
[0170] The display and save module is configured as follows: the host computer displays the text information and the numerical values representing the text information levels, and at the same time, it creates a log level file in the host computer corresponding to the numerical values representing the text information levels through NLog. The system matches the numerical values representing the text information levels with the log level files and automatically saves the text information to the corresponding log level file, thereby realizing the hierarchical output of text information in the log file.
[0171] Example 3
[0172] The purpose of this embodiment is to provide a computer-readable storage medium.
[0173] A computer-readable storage medium having a computer program stored thereon that, when executed by a processor, implements the steps of the CODESYS controller application layer text remote output method as described in Embodiment 1 of this disclosure.
[0174] Example 4
[0175] The purpose of this embodiment is to provide an electronic device.
[0176] An electronic device includes a memory, a processor, and a program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps in the CODESYS controller application layer text remote output method as described in Embodiment 1 of this disclosure.
[0177] The steps and methods involved in the apparatuses of Embodiments 2, 3, and 4 above correspond to those in Embodiment 1. For specific implementation details, please refer to the relevant description section of Embodiment 1. The term "computer-readable storage medium" should be understood as a single medium or multiple media including one or more instruction sets; it should also be understood as including any medium capable of storing, encoding, or carrying an instruction set for execution by a processor and enabling the processor to perform any of the methods in this invention.
[0178] Those skilled in the art will understand that the modules or steps of the present invention described above can be implemented using general-purpose computer devices. Optionally, they can be implemented using computer-executable program code, thereby allowing them to be stored in a storage device for execution by a computer device, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. The present invention is not limited to any particular combination of hardware and software.
[0179] While the specific embodiments of the present invention have been described above in conjunction with the accompanying drawings, this is not intended to limit the scope of protection of the present invention. Those skilled in the art should understand that various modifications or variations that can be made by those skilled in the art without creative effort based on the technical solutions of the present invention are still within the scope of protection of the present invention.
Claims
1. A method for remote text output at the application layer of a CODESYS controller, characterized in that: Includes the following steps: The host computer obtains the IP address and port number of the CODESYS controller to be interacted with, and establishes a TCP connection with the CODESYS controller to be interacted with. The CODESYS controller sets a value representing the text information level for the text information to be output, and transmits the text information to be output and the value representing the text information level to the host computer. The host computer displays the text information and the numerical values representing the text information levels. At the same time, it creates log level files in the host computer that correspond to the numerical values representing the text information levels through NLog. By matching the numerical values representing the text information levels with the log level files, the text information is automatically saved to the corresponding level log file, thus realizing the hierarchical output of text information in the log file. This also includes adding a remote text output library to the CODESYS controller, which is used to remotely output text information. The method for creating the remote text output library is as follows: S11: Add the SysSocket, SysSem, and SysMem libraries in the library manager of CODESYS IDE; S12: Implement a linked list-based queue type and multiple functions for operating on this queue type; S13: Add multiple global variables to the global variable list for global data transfer; S14: Implement a FUNCTION using ST language to add the text information to be sent to the global queue object; S15: Implement a PROGRAM using ST language to retrieve information from the aforementioned global queue object and send it out via TCP protocol; S16: Encapsulates the queue types, functions, programs, and global variables implemented in S12 to S15 into a CODESYS library file.
2. The CODESYS controller application layer text remote output method as described in claim 1, characterized in that, The added global variables include one string constant, which is sent to the host computer when the TCP communication connection is first established, as an identification mark for the CODESYS controller.
3. The CODESYS controller application layer text remote output method as described in claim 1, characterized in that, The steps for implementing the FUNCTION described in S14 are as follows: S14-1: Define two input variables for this FUNCTION: one INT type variable representing the level of the text information; and one string type variable representing the text information to be added. S14-2: Check the validity of the input variables at the beginning of the FUNCTION. Continue only if the length is greater than 0; otherwise, return. S14-3: Convert the INT type variable representing text information level described in S14-1 into a string, append a separator ":" to this string, and then append the string type variable representing text information described in S14-1 to the end of the string to form the string to be sent.
4. The CODESYS controller application layer remote text output method as described in claim 1, characterized in that, The process of creating log level files is as follows: Implement a LogTools class in the host computer. Define an enumeration LOGLEVEL in the LogTools class and implement a public static function WriteLog. The enumeration LOGLEVEL represents the log level of text information. The WriteLog function is used to pass a given string to NLog according to the given level, so as to log the log level file and output logs in different levels.
5. The CODESYS controller application layer text remote output method as described in claim 3, characterized in that, The text information will be automatically saved to the corresponding log level file, specifically: Implement the WriteTCPLog function in the host computer. The WriteTCPLog function has two input parameters: a LOGLEVEL variable representing the log level of the text information and a string variable representing the text information. The obtained text information is truncated after the first ":", and the string is added to the text box used to display the text information. The string before the ":" is converted into the log level. Then, the LogTools.WriteLog function is used to output the string after the first ":" of the text information to the log level file corresponding to the log level.
6. The CODESYS controller application layer text remote output method as described in claim 2, characterized in that, After the host computer obtains the IP address and port number of the CODESYS controller to be interacted with, it first determines whether it is the first time reading. If it is the first time reading, it determines whether the CODESYS controller to be interacted with is a CODESYS controller that supports remote output of text information based on the CODESYS controller's identification flag. Only after determining that the other party is a CODESYS controller that supports remote output of text information will the text information sent by the other party be received.
7. The CODESYS controller application layer text remote output system, characterized in that: include: The connection establishment module is configured to: obtain the IP address and port number of the CODESYS controller to be interacted with from the host computer, and establish a TCP connection with the CODESYS controller to be interacted with; The transmission module is configured such that the CODESYS controller to be interacted with sets a value representing the text information level for the text information to be output, and transmits the text information to be output and the value representing the text information level to the host computer. The display and save module is configured as follows: the host computer displays the text information and the numerical values representing the text information level, and at the same time, it creates a log level file in the host computer corresponding to the numerical values representing the text information level through NLog. The numerical values representing the text information level and the log level file are matched, and the text information is automatically saved to the corresponding level log level file, so as to realize the hierarchical output of text information in the log file. This also includes adding a remote text output library to the CODESYS controller, which is used to remotely output text information. The method for creating the remote text output library is as follows: S11: Add the SysSocket, SysSem, and SysMem libraries in the library manager of CODESYS IDE; S12: Implement a linked list-based queue type and multiple functions for operating on this queue type; S13: Add multiple global variables to the global variable list for global data transfer; S14: Implement a FUNCTION using ST language to add the text information to be sent to the global queue object; S15: Implement a PROGRAM using ST language to retrieve information from the aforementioned global queue object and send it out via TCP protocol; S16: Encapsulates the queue types, functions, programs, and global variables implemented in S12 to S15 into a CODESYS library file.
8. A computer-readable storage medium having a program stored thereon, characterized in that, When executed by the processor, the program implements the steps in the CODESYS controller application layer text remote output method as described in any one of claims 1-6.
9. An electronic device, comprising a memory, a processor, and a program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the steps in the CODESYS controller application layer text remote output method as described in any one of claims 1-6.
Citation Information
Patent Citations
CodeSys-based external extension module component design method and system
CN114460898A
A method of configuring an automation system
WO2019054929A1