Exclusive data acquisition method based on CAN bus
Technical Field
The invention relates to a data acquisition method, in particular to an exclusive data acquisition method based on a CAN bus.
Background
Among a plurality of field buses, the CAN bus is more and more favored by people due to the advantages of outstanding reliability, instantaneity, flexibility and the like, and accordingly, the market of product requirements based on the CAN bus is increasingly expanded and the application is also more and more extensive.
Data acquisition is an interface technology in which data is acquired from the outside of a system by a device and input into the inside of the system, and the same point data is repeatedly acquired at intervals of a certain time (called a sampling period), and is widely applied to various fields. With the rapid development of the internet industry, data acquisition has been widely applied to the internet and distributed fields, bringing data acquisition into a new era.
The method comprises the steps that a working framework of a client/server is adopted for periodical data collection, a data summarizing end serves as the client, a collection terminal serves as the server, when the data summarizing end needs to collect data, a command request is sent to the collection terminal, and the collection terminal responds to the command request and returns the data to be collected. According to the communication flow, data acquisition of other residual acquisition terminals is completed in one sampling period. The defects are as follows: in a sampling period, the data center sends out the same command request for many times, and on the CAN bus, the same acquisition terminal receives the same command request for many times, plus the command processing time, the communication bandwidth is greatly wasted. In addition, when the acquisition terminal needs to send emergency data, the real-time response time of the data center is uncertain. The best response condition is that when the appointed acquisition terminal is inquired, the terminal just needs to send emergency data; the worst response situation is that the last acquisition terminal needs to send urgent data when inquiring the first acquisition terminal.
Disclosure of Invention
The invention aims to solve the technical problem of providing a real-time and efficient exclusive data acquisition method based on a CAN bus aiming at the defects of the prior art.
The technical problem to be solved by the present invention is achieved by the following technical means. The invention relates to a CAN bus-based exclusive data acquisition method, which is characterized in that:
the method is based on a CAN bus multi-terminal exclusive data acquisition method, and realizes data communication between a set of data summarization terminal and N sets of acquisition terminals, wherein N is a positive integer; the data summarization end and the acquisition terminal are intelligent equipment which is provided with a CAN hardware interface and CAN execute program code characteristics; n sets of acquisition terminals are connected in parallel to a CAN bus in a hand-in-hand mode through a CAN bus cable and finally connected to a data summarizing end;
the data acquisition method comprises the following steps: exclusive "one-to-one" communication; on the CAN bus, a data summarization end initiates a command request to an acquisition terminal with a designated identifier value, only the acquisition terminal responds to the command request, and returns response data;
exclusive "one-to-many" polling communications; the data acquisition process of one period can be completed by configuring a parent terminal and a data summarization terminal only by sending a command request once;
emergency communication; two methods for mutually matching the 'emergency bit' and the 'command code' flag bit are designed, and real-time transmission and response of emergency data are ensured by adjusting the priority of the message under different conditions.
The invention relates to a CAN bus-based exclusive data acquisition method, which further adopts the preferable technical scheme that: the intelligent device is a PC computer.
The invention relates to a CAN bus-based exclusive data acquisition method, which further adopts the preferable technical scheme that: the method supports CAN hardware of CAN2.0A or CAN2.0B protocol; m-bit identifiers are used, Y ≧ M ≧ 3, Y equals 11 for CAN2.0A hardware, and Y equals 29 for CAN2.0B hardware; the identifier is stored in the identifier field of the CAN message.
The invention relates to a CAN bus-based exclusive data acquisition method, which further adopts the preferable technical scheme that: the identifiers of the data summarizing end and the acquisition terminal are different in numerical value, and the numerical value of the identifier of the data summarizing end is the minimum; defining the lowest bit of the identifier as an emergency bit, defaulting to '1' to represent normal state communication and '0' to represent emergency data communication; using two bits of bit (M-2) | bit (M-1) of the identifier as a command code; bit0 represents the lowest order bit of the identifier, and bit (M-1) represents the highest order bit of the identifier.
The invention relates to a CAN bus-based exclusive data acquisition method, which further adopts the preferable technical scheme that: the command code is defined in the data summarization end by adopting the following method:
(1) "11 b" indicates that the data summarizing end sends out a data polling command and requests to return data collected by each terminal in sequence from the specified x-th collecting terminal, wherein x is the identifier of the collecting terminal and is stored in the data field of the CAN message;
(2) "10 b" indicates that the data summarization end sends a specific command request to the x-th acquisition terminal, wherein x is the identifier of the acquisition terminal, and x and the specific command request are stored in the data field of the CAN message;
the command code is defined in the acquisition terminal by adopting the following method:
A. "11 b" and "10 b" indicate that the "acquisition terminal" responds to the "11 b" data polling command request sent by the "data summarization terminal"; wherein "10 b" indicates that the current data transmission has not ended; "11 b" indicates that the current data transmission has ended;
B. when the emergency bit is "1", 00b "and 01 b" indicate that the "acquisition terminal" responds to the "10 b" specific command request sent by the "data summarization terminal"; where "00 b" indicates that the current data transmission has not ended; "01 b" indicates that the current data transmission has ended;
(3) when the emergency bit is '0', '00 b' and '01 b' indicate that the 'acquisition terminal' sends an emergency command request to the 'data summarization terminal'; where "00 b" indicates that the current data transmission has not ended; "01 b" indicates that the current data transmission has ended.
The invention relates to a CAN bus-based exclusive data acquisition method, which further adopts the preferable technical scheme that: the method adopts exclusive one-to-one communication, namely a data gathering end initiates a command request to an acquisition terminal with a designated identifier value, and only the acquisition terminal responds to the command request and returns response data; the process of configuring the father terminal belongs to one-to-one communication, the data summarizing end sends a '10 b' command request packet to a specified acquisition terminal, a data field of the command request packet contains an identifier value, the acquisition terminal with the identifier value of the command request is configured as the father terminal of the terminal, the terminal carries out response configuration after receiving the command request, and a configuration result is reported to the data summarizing end; the father terminal is configured, so that the acquisition terminal CAN receive the CAN message from the father terminal and is used for exclusive one-to-many polling communication.
The invention relates to a CAN bus-based exclusive data acquisition method, which further adopts the preferable technical scheme that: the method adopts exclusive one-to-many polling communication: the data summarization end initiates a query request to a plurality of acquisition terminals and requires the acquisition terminals to return respective acquired data to the data summarization end, and the one-to-many communication is a periodic data communication process; in a one-to-many communication period, the data gathering end only needs to send a command request once, and a plurality of acquisition terminals respond in sequence and return acquired data of the terminal;
(1) the data gathering end sends an 11b command request to the CAN bus, the data field of the message contains an identifier value of an acquisition terminal 1#, and the acquisition terminal 1# is required to return acquired data;
(2) all the acquisition terminals on the CAN bus CAN receive an 11b command request message of the data gathering terminal, and by judging the identifier value of the message data field, the acquisition terminal 1# meets the requirement, responds to the command request, returns acquired data to the data gathering terminal, and ignores the command message by other acquisition terminals;
(3) acquisition terminal 1# is a parent terminal of acquisition terminal 2#, acquisition terminal 2# receives a response message of acquisition terminal 1#, and acquisition terminal 1# judges whether an emergency bit of an identifier in the message is 0 or not, and ignores the message if the emergency bit is 0; if the message is "1", then the following processing is carried out through the "command code" for identifying the identifier in the response message:
(i) when the command code is not 11b, the message is ignored by the acquisition terminal 2#, and is not processed;
(ii) when the command code is 11b, the acquisition terminal 2# returns acquired data to the data gathering terminal;
the other acquisition terminals directly ignore the response message of the acquisition terminal 1#, and do not process the response message;
(4) acquisition terminal 2# is a parent terminal of acquisition terminal 3#, acquisition terminal 3# receives a response message of acquisition terminal 2#, and acquisition terminal 2# judges whether an emergency bit of an identifier in the message is 0 or not, and ignores the message if the emergency bit is 0; if the message is "1", then the following processing is carried out through the "command code" for identifying the identifier in the response message:
(i) when the command code is not 11b, the message is ignored by the acquisition terminal 2#, and is not processed;
(ii) when the command code is 11b, the acquisition terminal 2# returns acquired data to the data gathering terminal;
the other acquisition terminals directly ignore the response message of the acquisition terminal 2#, and do not process the response message;
(5) by analogy, the last acquisition terminal N # returns acquired data to the data gathering terminal, and the data acquisition process of one period is completed.
The invention relates to a CAN bus-based exclusive data acquisition method, which further adopts the preferable technical scheme that: the 'emergency communication' of the method is that the 'acquisition terminal' actively sends data to the 'data summarization terminal'; when the acquisition terminal a needs emergency communication, the emergency position of the identifier is set to be 0, and compared with the priority of polling communication, the acquisition terminal a has higher priority, so that the periodic polling communication process is interrupted, and the emergency data is guaranteed to be preferentially transmitted to the data summarizing end; after receiving the emergency message, the data summarizing end sets the emergency position of the identifier of the data summarizing end to be 0, the message of the data summarizing end has the highest priority, when the emergency data of other acquisition terminals are about to be transmitted on the CAN bus, the transmission process is interrupted, and after the response message is transmitted to the acquisition terminal a firstly, the interrupted acquisition terminal CAN continue to transmit the data;
and by analogy, the emergency communication process of other terminals is completed.
Under extreme conditions, a plurality of acquisition terminals are abnormal simultaneously, an emergency message is sent to a data gathering terminal, and the CAN bus enters an arbitration mechanism and sends data in sequence according to the priority level; after receiving the emergency message, the data summarizing end sets the emergency position of the identifier of the data summarizing end to be 0, the message of the data summarizing end has the highest priority, response messages are sequentially sent to corresponding acquisition terminals according to the sequence of receiving the emergency message, and finally emergency communication of all the acquisition terminals is completed.
Compared with the prior art, the invention and the further designed invention have the following remarkable advantages:
1. the data summarization end and the acquisition terminal are preferably selected from a PC (personal computer) but not limited to the PC, and the intelligent equipment which is provided with the CAN hardware interface and CAN execute the program code characteristics meets the technical requirements and has wide application range.
2. The invention can support more collection terminals, the performance is improved by tens of times, and the more the collection terminals, the more obvious the technical effect is.
3. Even in extreme cases, m acquisition terminals simultaneously require to send the emergency data, and the acquisition terminal with the lowest priority needs to wait for (m-1) × 0.4.4 ms (calculated at the rate of 250 kb/s) at the longest.
Drawings
FIG. 1 is a data collection type classification of an exclusive data collection method based on a CAN bus according to the present invention;
FIG. 2 is a hardware block diagram of the present invention;
FIG. 3 is a diagram of an identifier definition for a CAN message;
FIG. 4 is a communication process diagram of exclusive "one-to-many" polling communication;
fig. 5 is a communication process diagram corresponding to a plurality of "acquisition terminals" of emergency communication not initiating emergency communication at the same time;
fig. 6 is a communication process diagram of multiple "acquisition terminals" simultaneously initiating emergency communication corresponding to the emergency communication.
Detailed Description
The embodiments of the present invention will be further described with reference to the accompanying drawings so as to facilitate the further understanding of the present invention by those skilled in the art, and do not limit the right thereto.
Referring to fig. 1, an exclusive data acquisition method based on a CAN bus is an exclusive data acquisition method based on multiple terminals of the CAN bus, and realizes data communication between a set of data summarization terminals and N sets of acquisition terminals, where N is a positive integer; the data summarization end and the acquisition terminal are intelligent equipment which is provided with a CAN hardware interface and CAN execute program code characteristics; n sets of acquisition terminals are connected in parallel to a CAN bus in a hand-pulling mode through a CAN bus cable and finally connected to a data gathering end.
The method comprises the following steps:
exclusive one-to-one communication, on a CAN bus, a data gathering end initiates a command request to a collection terminal with a designated identifier value, and only the collection terminal responds to the command request and returns response data;
the exclusive one-to-many polling communication can complete the data acquisition process of one period by configuring a parent terminal and a data summarization terminal only needing to send a command request once;
in the emergency communication, two methods of mutually matching an emergency bit and a command code are designed, and the real-time transmission and response of emergency data are ensured by adjusting the priority of a message under different conditions.
Referring to fig. 2, a method for acquiring exclusive data based on a CAN bus: CAN hardware supporting CAN2.0A or CAN2.0B protocols. The whole hardware structure comprises: the CAN bus, N acquisition terminals and a data summarization terminal are all required to support CAN2.0A or CAN2.0B protocols.
Referring to FIG. 3, using an M-bit identifier (Y ≧ M ≧ 3), Y equals 11 for CAN2.0A hardware and 29 for CAN2.0B hardware. The identifier is stored in the identifier field of the CAN message.
The lowest bit0 of the identifier is an emergency bit, the default value is "1", indicating normal state communication, and the value "0" indicates emergency data communication; the highest bit of the identifier is bit (M-1), and the lowest two bits of the identifier (M-2) | bit (M-1) are used as the command code.
The identifiers of the data summarization end and the acquisition terminal are different in numerical value, and the numerical value of the identifier of the data summarization end is the minimum. The priority of the message is determined by the identifier value, and the smaller the identifier value, the higher the priority, and the right to use the CAN bus preferentially is provided.
According to the principle, a mutual matching method of the 'emergency bit' and the 'command code' zone bit is designed. The following is a definition of the use of command codes:
(1) the definition of command code is used in the "data summarization side":
and 11b indicates that the data summarizing end sends a data polling command to request that the data is collected by each terminal from the specified x-th collecting terminal, wherein x is the identifier of the collecting terminal and is stored in the data field of the CAN message.
"10 b" indicates that the "data summarization end" sends a specific command request to the x-th "acquisition terminal", wherein x is the identifier of the "acquisition terminal", and x and the specific command request are stored in the data field of the CAN message.
(2) The definition of command codes is used in "acquisition terminal":
"11 b" and "10 b" indicate that the "acquisition terminal" responds "to the" 11b "data polling command request sent by the data summarization terminal". Wherein "10 b" indicates that the current data transmission has not ended; "11 b" indicates that the current data transmission has ended.
When the emergency bit is "1", 00b "and 01 b" indicate that the "acquisition terminal" responds to the "10 b" specific command request issued by the "data summarization terminal". Where "00 b" indicates that the current data transmission has not ended; "01 b" indicates that the current data transmission has ended.
When the emergency bit is "0", 00b "and 01 b" indicate that the "acquisition terminal" sends an emergency command request to the "data summarization terminal". Where "00 b" indicates that the current data transmission has not ended; "01 b" indicates that the current data transmission has ended.
Referring to fig. 3-4, exclusive "one-to-many" polling communication, the steps are as follows:
the method comprises the following steps: the data gathering end sends an 11b command request to the CAN bus, the data field of the message contains an identifier value of an acquisition terminal 1#, and the acquisition terminal 1# is required to return acquired data;
step two: all the acquisition terminals on the CAN bus receive an 11b command request message of the data gathering terminal, and by judging the identifier value of the message data field, the acquisition terminal 1# meets the requirement, responds to the command request, returns acquired data to the data gathering terminal, and ignores the command message by other acquisition terminals;
step three: the ' acquisition terminal ' 1# ' is a ' parent terminal ' of the ' acquisition terminal 2# ', the ' acquisition terminal 2# ' simultaneously receives a response message of the ' acquisition terminal 1# ', the ' acquisition terminal 1# ' judges whether an emergency bit of an identifier in the message is ' 0 ', and if the emergency bit is ' 0 ', the message is ignored; if the message is "1", then the following processing is carried out through the "command code" for identifying the identifier in the response message:
(1) when the command code is not 11b, the message is ignored by the acquisition terminal 2#, and is not processed;
(2) when the command code is 11b, the acquisition terminal 2# returns acquired data to the data gathering terminal; other acquisition terminals ignore the response message of the acquisition terminal 1#, and do not process;
step four: and repeating the process according to the third step, and returning the acquired data to the data summarizing end by the last acquisition terminal N, namely completing the data acquisition process of one period.
Wherein, the configuration process of the parent terminal belongs to one-to-one communication. The data summarization end sends a 10b command request packet to a designated acquisition terminal, the data field of the command request packet contains an identifier value, the acquisition terminal with the identifier value is configured as a parent terminal of the terminal, the terminal receives the command request, then response configuration is carried out, and the configuration result is reported to the data summarization end. The "parent terminal" is configured to allow the "acquisition terminal" to receive the CAN packet from the "parent terminal" and thus apply to exclusive "one-to-many" polling communication.
Referring to fig. 5-6, the emergency communication includes the following steps:
(1) the acquisition terminal sets the identifier 'emergency position' of the transmitted message to '0' to obtain the current highest priority;
(2) after the data summarization end receives the message, the emergency position of the identifier of the response message is set as 0 to obtain the highest priority, and the real-time transmission and response of the emergency data are ensured.
Referring to fig. 5, in emergency communication, the "acquisition terminal" actively sends data to the "data summarization end", and when the "acquisition terminal a" needs emergency communication, the "emergency position" of the identifier is set to "0", and compared with the priority of polling communication, the "acquisition terminal a" has higher priority, so that the periodic polling communication process is interrupted, and the emergency data is guaranteed to be preferentially transmitted to the "data summarization end". After the data summarizing end receives the emergency message, the emergency position of the identifier of the data summarizing end is set to be 0, the message of the data summarizing end has the highest priority, if the emergency data of other acquisition terminals on the CAN bus are to be sent, the sending process is interrupted, and after the response message is sent to the acquisition terminal a firstly, the interrupted acquisition terminal CAN continue to send the data. And by analogy, the emergency communication process of other terminals is completed.
Referring to fig. 6, in an extreme case, a plurality of "acquisition terminals" may be abnormal at the same time, and an emergency message is sent to the "data summarization terminal", and at this time, the CAN bus enters an arbitration mechanism and sequentially sends data according to the priority level. After receiving the emergency message, the data summarizing end sets the emergency position of the identifier of the data summarizing end to be 0, the message of the data summarizing end has the highest priority, response messages are sequentially sent to corresponding acquisition terminals according to the sequence of receiving the emergency message, and finally emergency communication of all the acquisition terminals is completed. By adopting the method, the acquisition terminals with different priorities can report the emergency data to the data summarizing terminal in the shortest time and obtain priority processing.