Disclosure of Invention
In view of the above-mentioned defects of the prior art, the present invention aims to provide a PLC data acquisition management system, which supports high-speed data acquisition and optimizes data management and use problems after the PLC data acquisition is completed.
In order to achieve the above purpose, the present invention provides a PLC data acquisition management system, which includes a Redis database, a Redis publish-subscribe service module, a service database, a data acquisition and cache module, a data synchronization module, and an automatic data maintenance module; the data acquisition and cache module, the data synchronization module and the data automatic maintenance module take a Redis database as data transfer and operate independently;
the data acquisition and cache module is used for acquiring a binary stream result of each acquisition node according to the configured acquisition node information needing to be read and written, and issuing or storing data to a Redis database by the binary stream result;
the data synchronization module is used for acquiring a list to be synchronized from a Redis database, and performing data type conversion on a binary stream result of a corresponding acquisition node in the list to be synchronized or directly storing the binary stream result into a service database;
and the data automatic maintenance module is used for deleting the data in the designated interval in the data of each acquisition node from the Redis database according to the storage time or the storage number.
Further, the data acquisition and cache module specifically executes the following procedures: acquiring configured acquisition node information needing to be read and written; according to the configured information of the acquisition nodes needing to be read and written, a PLC connecting assembly is used for sending data requests, and binary stream data are obtained in batches; and according to the configured information of the acquisition nodes needing to be read and written, acquiring the binary stream result of each acquisition node from the binary stream data in a segmentation manner and processing the data, wherein the data processing manner is as follows: and (4) publishing or storing the data to a Redis database.
Further, the PLC connecting assembly is a Sharp7 assembly, and the connected PLC equipment is Siemens PLC equipment.
Further, the Redis is issued by using a PUBLISH command, where the PUBLISH command includes two parameters: channel name, message content; the channel name is a character string type and consists of an ID number of an acquisition node and a node data type; the message content is of a binary stream type, including a time byte stream and a node value byte stream.
Further, the data is stored in a Redis database by using a Redis Zadd command, the Zadd command comprises three parameter primary keys, scores and members, and the primary key is of a character string type and consists of an ID number of the acquisition node and a node data type; the fraction is a double-precision floating point type and is the mapping of the current time; the members are of the binary stream type, including a time byte stream and a node value byte stream.
Further, the specific synchronization process of the data synchronization module comprises the following steps;
step S21: acquiring the last time synchronization time of a collection node from a Redis database by using a Redis HGET command;
step S22: acquiring a to-be-synchronized list of the acquisition nodes by using a Redis ZRANGEBYSCORE command;
step S23: according to the synchronization interval of each acquisition node in the list to be synchronized, directly or indirectly storing the binary stream result of each acquisition node in the synchronization interval to a service database;
step S24: and updating the synchronization time of the acquisition node to be the current time by using a Redis HSET command.
Further, the parameters of the list to be synchronized include a primary key, min and max;
the key uses character string, which is composed of node ID number and node data type;
min is a number after the last synchronous time is converted into a double-precision floating point type;
max is the number after the current time is converted into the double-precision floating point type; the interval between min and max is the synchronization interval.
Further, the maintenance mode of the data automatic maintenance module includes: maintaining the mode according to the storage time and the mode according to the storage number;
the maintenance mode is executed by a Redis ZREMRANGELOBYSCORE command according to the preservation time, wherein the parameters comprise a main key, min and max; the key uses character string, which is composed of node ID number and node data type; min is an infinitesimal number; max is the deletion end time;
the maintenance mode according to the stored number is executed through a Redis ZREMRANGELOBYRANK command, wherein the parameters comprise a main key, a start and a stop; the main key uses a character string and consists of a node ID number and a node data type; start is 0; stop is 0 minus the number that needs to be saved.
Compared with the existing PLC data acquisition management system, the PLC data acquisition management system has the following beneficial effects:
1. data collection is carried out in batches, and the total time consumption is lower than that of single-point collection.
2. Data is read and stored in a binary stream format all the time, so that the time for decoding the data into specific numerical values is saved, and the acquisition response speed is improved.
3. The memory database Redis is used as a data transfer area, so that the data storage speed is improved, and the data is ensured not to be lost by combining the Redis persistence characteristic.
4. And the data of the Redis transfer area is automatically maintained, so that the stability of the Redis transfer area is improved.
5. And by adopting a mode of combining Redis and a service database, different databases are used in different application scenes, and the use performance of the data is improved.
Detailed Description
To further illustrate the various embodiments, the invention provides the accompanying drawings. The accompanying drawings, which are incorporated in and constitute a part of this disclosure, illustrate embodiments of the invention and, together with the description, serve to explain the principles of the embodiments. Those skilled in the art will appreciate still other possible embodiments and advantages of the present invention with reference to these figures. Elements in the figures are not drawn to scale and like reference numerals are generally used to indicate like elements.
The invention will now be further described with reference to the accompanying drawings and detailed description.
As shown in fig. 1, the PLC data high frequency acquisition system of the present invention includes a Redis database, a Redis publish-subscribe service module, a data acquisition and cache module, a data synchronization module, and an automatic data maintenance module. The data acquisition and cache module, the data synchronization module and the data automatic maintenance module operate independently and perform data transfer through a Redis database.
The Redis (remote Dictionary Server) database is a fully open-source, BSD-compliant, high-performance key-value database. When the access of the Redis database and the Redis publish-subscribe service module is performed, a plurality of Redis commands are involved, which are described as follows:
the Redis ZADD command is used to add one or more member elements and their fractional values to the ordered set. If a member is already a member of the ordered set, the score value for that member is updated and the member is guaranteed to be in the correct position by reinserting the member element. The fractional value may be an integer value or a double precision floating point number. If the ordered set key does not exist, an empty ordered set is created and the ZADD operation is performed.
The Redis HSET command is used to assign values to fields in the hash table. If the hash table does not exist, a new hash table is created and HSET operation is carried out; if the field is already present in the hash table, the old value will be overwritten.
The Redis HGET command is used to return the value of the specified field in the hash table.
The Redis ZRANGEBYSCORE command is used to return a list of members in the ordered set that specify the fractional interval.
The Redis ZREMRANGELISCORE command is used to remove ordered sets, specifying all members within the score (score) interval.
The Redis ZREMRANGELYRRANK command is used to remove ordered sets, specifying all members within a rank (rank) interval.
The Redis PUBLISH command is used to send information to a specified channel.
The Redis SUBSCRIBE command is used to SUBSCRIBE to information for a given channel or channels.
Reference will now be made to the individual modules
As shown in fig. 2, the data acquisition and caching process of the data acquisition and caching module includes the following steps:
s11: obtaining a batch of acquisition node configurations
Acquiring all configured node information needing to be read and written, wherein the node information at least comprises: ID number (all nodes must be unique), data block number, PLC data start address, PLC data type.
Grouping all the acquired node information according to the data block number;
arranging the node information of each group according to the PLC data starting address;
partitioning each group of node information according to a preset rule, and ensuring that the data density D of each region meets the requirement of partition density; if a group of data contains N pieces of data point information, sequentially judging whether the first N-M pieces of data point information meet the partition density requirement, if so, configuring the corresponding data point information into a group, and judging the rest information again according to a partition method, wherein N is a positive integer, and the value of M is taken from 0 to N-1. The method for calculating the data density D comprises the following steps: d is C/L. Wherein C represents the number of bytes occupied by all configurations in the region; l denotes the total length of the region.
All the node configurations of the partitioned data of each region form a batch of acquisition node configurations.
S12: sending data acquisition requests
And establishing connection according to the IP address, the Rack address and the Slot address of the PLC by using a Sharp7 component (an open-source Siemens connection component), and after the connection is established, sending a data acquisition request, wherein the request parameters comprise the data block number, the starting address and the length of the batch of data.
S13: obtaining binary stream results
Using the Sharp7 component, the raw result data requested at S12, i.e., the entire binary stream data, is obtained.
S14: respectively acquiring binary stream of each acquisition node
According to the configuration of the collection nodes, the binary stream data is segmented, and the result of each small section of binary stream of each collection node is determined.
S15: processing node data
Processing node data includes two tasks:
task 1: publishing data
The issuing data is issued by using a PUBLISH command based on the Pub/Sub characteristic of Redis.
The PUBLISH command contains two parameters: channel name (channel), message content (message). Wherein the channel uses a string of characters, consisting of a node ID number and a node data type. It must be unique; the message uses a binary stream, consisting of two parts, a time byte stream and a node value byte stream.
Task 2: storing data to Redis
The storage data is stored in a SortedSet (ordered set) mode, and is stored by using a ZADD command.
The ZADD command includes three parameters: primary key (key), score (score), member (member).
The key uses a character string and consists of a node ID number and a node data type. It must be unique.
score uses a double precision floating point type (double) that is converted from the current time, the larger the converted double value, and thus may be sorted according to score.
memer uses a binary stream, consisting of two parts, a time byte stream and a node value byte stream.
The conversion method for the temporal and double-precision floating point in score can adopt the following method: the first method is as follows: tick is a middle rotation, and Tick indicates the number of milliseconds from a certain time (for example, 0 minute 0 second at 0 point on 10 months and 1 day in 2020). The second method comprises the following steps: and (4) converting the string and the double through string transfer, for example, converting the string and the time through the format of yyMMddHHmmssfff.
As shown in fig. 3, the data synchronization module performs data synchronization for each collection node, and includes the following steps:
s21: obtaining last time synchronization time of each node
And (3) acquiring the last synchronization time of each node from the Redis database by using an HGET command, wherein each acquisition node is represented by a node ID number.
S22: obtaining a list to be synchronized
The ZRANGEBYSCORE command is used to obtain the list to be synchronized, wherein the parameters comprise a primary key (key), a minimum score (min) and a maximum score (max).
The key uses a character string and consists of a node ID number and a node data type;
min is a number after the last synchronous time is converted into a double-precision floating point type;
max is the number after the current time has been converted to the double precision floating point type. The interval between min and max is the synchronization interval.
S23: according to the synchronous interval of each acquisition node in the list to be synchronized, storing the binary stream result of each acquisition node in the synchronous interval into a service database
And according to the obtained synchronization interval of each acquisition node in the list to be synchronized, storing the binary stream result of each acquisition node in the synchronization interval into a service database at one time, such as a relational database mysq, sql server and the like, preferably using a database with a time sequence characteristic. It should be noted that, in order to ensure high-speed acquisition, the data stored in the Redis is a binary stream, and at this time, the binary stream may be converted into specific information such as numerical values and character strings and stored in the service database according to the data type.
S24: updating synchronization time
The synchronization time of the ID is updated to the current time using the HSET command.
The data synchronization module runs periodically, and the recommended running period is 1-3 seconds.
As shown in fig. 4, the data automatic maintenance module has two maintenance modes for each collection node:
the first mode is as follows: maintenance by retention time
The ZREMRANGELBYSCORE command is used, where the parameters include primary key (key), min (min), max (max).
The key uses a character string and consists of an ID number and a node data type.
min is an infinitesimal number.
max is the deletion ending time, and may be set to be stored for 1 day or 1 hour according to the configuration, that is, all data before 1 day or 1 hour is deleted.
And a second mode: maintenance by stored number
The ZREMRANGELYRANK command is used, where the parameters include primary key (key), start (start), end (stop).
The key uses a character string and consists of an ID number and a node data type.
The start is 0.
stop is 0 minus the number that needs to be saved.
The data automatic maintenance module runs periodically, preferably, the running period is 1 minute to 1 hour.
As shown in fig. 5, the PLC data acquisition management system of the present invention provides three applicable application scenarios.
Scene one: and refreshing the display in real time.
Using the Pub/Sub feature of Redis, SUBSCRIBE is performed using the SUBSCRIBE command. And subscribing to the message for corresponding data refreshing.
Scene two: using thermal data
The latest data, which is relatively frequently used in the early stage, is called thermal data. Since the thermal data is stored in the Redis in-memory database. The method can be directly read from Redis, and the efficiency is very high.
Scene three: analysis of big data, or using non-thermal data
For the analysis of historical data, the processing time is generally long, and at the moment, a background thread is adopted to read and use the business data.
In summary, compared with the existing PLC data acquisition management, the PLC data acquisition management system of the present invention has the following beneficial effects:
1. data collection is carried out in batches, and the total time consumption is lower than that of single-point collection.
2. Data is read and stored in a binary stream format all the time, so that the time for decoding the data into specific numerical values is saved, and the acquisition response speed is improved.
3. The memory database Redis is used as a data transfer area, so that the data storage speed is improved, and the data is ensured not to be lost by combining the Redis persistence characteristic.
4. And the data of the Redis transfer area is automatically maintained, so that the stability of the Redis transfer area is improved.
5. And by adopting a mode of combining Redis and a service database, different databases are used in different application scenes, and the use performance of the data is improved.
While the invention has been particularly shown and described with reference to a preferred embodiment, it will be understood by those skilled in the art that various changes in form and detail may be made therein without departing from the spirit and scope of the invention as defined by the appended claims.