Communication method supporting CAN bus to transmit data with more than 8 bytes
Technical Field
The invention belongs to the technical field of data transmission, and particularly relates to a communication method supporting CAN bus to transmit more than 8 bytes of data.
Background
With the development of microcomputer control technology and industrial field bus technology, communication modes based on field buses are increasingly widely applied in industrial control. The CAN bus is favored by a plurality of developers due to the characteristics of simple structure, low cost, good functional characteristics, extremely high reliability, strong field anti-interference capability and openness.
The CAN serial bus has the advantages of long transmission distance and high transmission speed, has a perfect error detection mechanism, and CAN ensure that information is not lost by adopting a frame transmission mode of a multiple access conflict arbitration mechanism; a maximum of 8 bytes of data can be transmitted per frame, providing high real-time performance, etc. The advantage of performance ensures that the CAN CAN be applied to a plurality of fields, and CAN be applied to the automobile industry, ship transportation, mechanical control, factory automation, building automation and the like.
The CAN fieldbus system is composed of a physical layer, a data link layer and an application layer 3 layer. The basic protocol specification of CAN only has physical layer protocol and data link layer protocol, and one of the core tasks of the system using CAN bus as transmission mechanism is to design CAN bus application layer protocol CAN2.0a standard CAN protocol and CAN2.0b extended CAN communication protocol to support to transmit 8 bytes of data at most each time, when one node transmits a frame of data segment exceeding 8 bytes to another node, it is unable to satisfy the requirement of transmitting data segment of the length at one time by using CAN2.0a or CAN2.0b communication protocol. The current embedded system is often single in function, simple in structure and limited in resources, but requires high transmission speed, high reliability and high efficiency, so that a novel application layer protocol with a simple structure is required.
A communication protocol currently completed by university of Zhejiang Wu Chaohui et al, which is applied to a large number of data connection-based CAN buses, implements a connection-based application layer protocol for message transmission of data greater than 8 bytes. This protocol, although implementing packetized transmissions of data greater than 8 bytes, lacks a response mechanism for transmission and therefore cannot handle transmission errors. In particular, the protocol can only realize the maximum data transmission of 256 x 7 bytes, has no mass data transmission mechanism, and can not guarantee the accuracy of data transmission. Meanwhile, the protocol is based on connection, and connection needs to be established first for each transmission, so that the transmission efficiency is low.
The name of the specialty used in the present document notes:
1. CAN data buffer: a data field interval in the CAN data frame.
2. Data segment: and a piece of data with the size ranging from 1 to 2, namely the payload_length to the power of unit bytes, cannot be transmitted through the CAN bus at one time when the size is more than 8 bytes.
3. Data frame: one frame of data, the size range is 0-8, and the unit bytes CAN be transmitted through the CAN bus at one time.
4. Payload_length: data segment length, unit bytes.
5. Integer frame: starting from the start position in the data segment, a segment is cut every 8 bytes in size, and the segment of data is called an integer frame.
6. Remainder frame: and a section of data with the size smaller than or equal to 8 is intercepted from the data section, and the length is equal to the remainder of dividing the payload_length by 8.
7. Frame_count: number of integer frames.
8. Domain_frame_length: the length of the remainder frame is equal to the remainder of the payload_length divided by 8.
9. Frame_index: after the data segment is intercepted, the identification of each segment is uniquely expressed relative to other segments.
10. Extension frame id: the identifier field in the can2.0b communication protocol.
11. Standard frame id: the identifier field in the can2.0a communication protocol.
12. Data frame id: the id of the CAN data frame is equal to the standard frame id when the CAN2.0a protocol is used and is equal to the extended frame id when the CAN2.0b protocol is used.
13. Device_id: the device id, unlike the extended frame id/standard frame id, is part of the extended frame id/standard frame id.
14. Extend _id_head: and (3) performing bit-wise or operation on the result obtained by shifting the device_id left by 25 bits, the result obtained by shifting the payload_length left by 11 bits and the frame_index.
15. Extend _id: synonymous with extended frame id, equal to extend _id_head, frame_index per bit or result.
16. Recv_buf: and the memory buffer of the received data segment is used for storing the content of the received data segment, and the length of the memory buffer is more than or equal to that of the data segment.
17. Id_length: this value is 11 when the can2.0a communication protocol is selected and 29 when the can2.0b communication protocol is selected.
Disclosure of Invention
The invention aims to provide a communication method for supporting the transmission of data with more than 8 bytes by a CAN bus, which solves the problem that the transmission efficiency is low because the traditional CAN bus cannot upload data with more than 8 bytes by transmitting data segments with more than 8 bytes between two nodes of the CAN bus and not using a CAN data buffer zone.
In order to solve the technical problems, the invention is realized by the following technical scheme:
The invention relates to a communication method supporting CAN bus to transmit more than 8 bytes of data, which comprises a data transmitting method and a data receiving method;
the data sending method comprises the following steps:
step S1: calculating a frame_count, a main_frame_length, and initializing the frame_index to 0;
Step S2: judging whether the frame_index is smaller than the frame_count;
step S3: if frame_index is less than frame_count, then extend _id_head is obtained, extend _id= extend _id_head bit by bit or frame_index is calculated;
if frame_index is greater than or equal to frame_count, then step S6 is performed;
Step S4: acquiring an 8-byte data frame starting from frame_index x 8 bytes as a data frame to be transmitted;
Step S5: the current node sends the data frame to the CAN bus, then adds up the frame_index to 1, and returns to the step S2;
step S6: the extend _ id _ head is obtained, calculation of extend _id = extend _id/u head bitwise or frame_index;
Step S7: acquiring data with a length of domain_frame_length from a frame_index of 8 bytes of a data segment as a data frame to be transmitted;
step S8: the current node sends the data frame to the CAN bus, and the content of the sent data segment is finished;
the data receiving method comprises the following steps:
step F1: receiving a frame of data, and calculating device_id, payload_length and frame_index values;
step F2: judging whether the receiving device_id is equal to the node device_id;
if the two types are equal, executing the step F3;
If not, discarding the data frame, and continuing to execute the step F1;
step F3: judging whether the result of subtracting frame_index from payload_length by 8 is greater than or equal to 8;
If yes, executing the step F4;
If not, executing the step F5;
step F4: putting the data frame into an 8-byte length memory from the frame_index of recv_buf multiplied by 8;
Step F5: placing the data frame into a (payload_length minus frame_index multiplied by 8) length memory from recv_buf frame_index multiplied by 8;
step F6: the content of the data segment is stored in the recv_buf memory, and the receiving is finished.
As a preferred technical solution, in the step F1, the payload_length represents a length of a data segment, an integer frame and a remainder frame are generated after the data segment is intercepted, the length of the integer frame is 8 bytes, the number of the integer frame is defined as frame_count, the length of the remainder segment is defined as domain_frame_length, and the number of the remainder frame is 1, and the calculation formula between the frame_ count, payload _length and the domain_frame_length is as follows:
frame_count=payload_length/8
remain_frame_length=payload_length%8。
As a preferable technical solution, the device_id, payload_length, and frame_index are fields of a data frame ID, and the sizes are sequentially defined as m bits, n bits, and k bits;
Wherein m, n and k satisfy the following relationship:
As a preferable technical scheme, the values of m, n and k are all user-defined; after m is well defined, on the premise that n and k are removed, the following conditions are satisfied:
as a preferred technical solution, the CAN bus uses a CAN communication protocol for transmission; the CAN communication protocol includes CAD2.0A communication protocol and CAD2.0B communication protocol.
As a preferred technical solution, CAD2.0A communication protocols support 11-bit extended frame ids; 11 is an extended frame ID, bit 10-bit 9 is used for identifying a device ID, and is defined as a device_id, the size is 2 bits, the value range is 0-3, and 4 devices are supported to be communicated at most; bit 8-bit 3 are used for storing payload_length, the size is 6 bits, the value range is 0-63, and the maximum supporting transmission data segment size is 64 bytes; bit 2-bit 0 are used for storing frame_index, the size is 3 bits, the value range is 0-7, and the maximum transmission of 8 segments of data frames is supported.
As a preferred technical solution, CAD2.0B communication protocol supports 29-bit extended frame id; in the 29-bit expansion frame ID, bits 28 to 25 are used for identifying the device ID, are defined as device_id, have a size of 4 bits, have a value range of 0 to 15, and support 16 devices to communicate at most; bit 24-bit 11 are used for storing payload_length, the size is 14 bits, the value range is 0-16383, and the size of a transmission data segment is 16384 bytes at most; bit 10-bit 0 are used for storing frame_index, the size is 11 bits, the value range is 0-2047, and 2048 segments of data frames are maximally supported to be transmitted.
The invention has the following beneficial effects:
According to the invention, the data to be transmitted is intercepted through the CAN protocol, the integer frame and the remainder frame generated after interception are judged, each section of identification of the data frame is multiplied by eight, and then the data frame is put into a (payload_length minus frame_index multiplied by 8) length memory from the frame_index multiplied by 8 of recv_buf, so that the data transmission efficiency is improved.
Of course, it is not necessary for any one product to practice the invention to achieve all of the advantages set forth above at the same time.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings that are needed for the description of the embodiments will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a flow chart of data transmission according to the present invention;
fig. 2 is a flow chart of the data receiving method of the present invention.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present invention, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
The method CAN support the CAN bus to transmit data segments with more than 8 bytes between two nodes, does not use a CAN data buffer area, and improves the transmission efficiency.
When in communication, the length of a data segment is defined as payload_length, the data segment is intercepted, an integer frame and a remainder frame are generated after interception, the length of the integer frame is 8 bytes, the number of the integer frame is defined as frame_count, the length of the remainder segment is defined as domain_frame_length, and the number of the remainder frame is 1. The variables are defined and calculated as follows,
frame_count=payload_length/8
remain_frame_length=payload_length%8
The data frame ID is decomposed as shown in table 1 below:
device_id |
payload_length |
frame_index |
device_id: the region stores a device_id value, the size of which is defined as m bits.
Payload_length: the region stores a payload_length value, the size of which is defined as n bits.
Frame_index: the region stores the frame_index value, the size of which is defined as k bits.
Wherein m, n, k satisfy the following relationship,
Wherein the values of m, k and n are set by user definition, but generally, on the premise of ensuring that n and k are rounded after m is well determined,
The invention mainly solves the problems that the traditional CAN protocol of the CAN2.0A standard and the CAN2.0B extended CAN communication protocol support to transmit 8 bytes of data at most each time, and when one node transmits a frame of data segment with the size exceeding 8 bytes to the other node, the data segment with the length cannot be transmitted at one time no matter the CAN2.0A or the CAN2.0B communication protocol is used.
Example one (CAN2.0A protocol transmission is taken as an example)
Can2.0a supports an 11-bit extended frame ID, so id_length=11, assuming m=2, n= 6,k =3, defined as follows,
① Bits 10 to 9 are used for identifying the device ID, and are defined as device_id, the size is 2 bits, the value range is 0-3, and at most 4 devices are supported for communication.
② Bits 8 to 3 are used for storing payload_length, the size is 6 bits, the value range is 0 to 63, and the maximum supporting transmission data segment size is 64 bytes.
③ Bit 2-bit 0 are used for storing frame_index, the size is 3 bits, the value range is 0-7, and the maximum transmission of 8 segments of data frames is supported.
Assuming that the data length to be transmitted is 64 bytes, bit10 to bit9 are other node device ids to be transmitted by the user, and are defined by the user, bit8 to bit3 are 0x3F, and bit2 to bit0 are 0x7.
As shown in fig. 1, the transmission flow is as follows:
Step ①: frame_count, remain_frame_length, is calculated and frame_index is initialized to 0.
Step ②: is it determined that frame_index is less than frame_count?
Step ③: if frame_index is less than frame_count, extend _id_head is obtained and extend _id is calculated. If frame_index is greater than or equal to frame_count, then step ⑥ is performed.
Step ④: the frame_index data frame is obtained as a data frame to be transmitted, and the data frame length is 8 bytes.
Step ⑤: the current node sends the data frame onto the CAN bus and then accumulates frame_index by 1.
Step ⑥: and obtaining extend _id_head and calculating extend _id.
Step ⑦: acquiring a frame_index data frame as a data frame to be transmitted, wherein the frame_index data frame has a length of main_frame_length
Step ⑧: and the current node sends the data frame to the CAN bus, and the content of the sent data segment is finished.
As shown in fig. 2, the receiving flow is as follows:
step ①: one frame of data is received, and device_id, payload_length, frame_index values are calculated.
Step ②: is it determined whether the received device_id is equal to the node device_id? If equal, go to step ③, if not, discard the data frame, then go to step ①
Step ③: if the result of subtracting frame_index from payload_length is greater than or equal to 8, then step ④ is performed, otherwise step ⑤ is performed.
Step ④: this data frame is placed in 8 byte length memory starting at recv_buf frame_index by 8.
Step ⑤: this data frame is placed in the (payload_length minus frame_index by 8) length memory starting from recv_buf frame_index by 8.
Step ⑥: the content of the data segment is stored in the recv_buf memory, and the receiving is finished.
Example two (CAN2.0B protocol transmission is taken as an example)
Can2.0b supports a 29-bit extended frame ID, so id_length=29, assuming that m=4, n=14, k=11 is taken, it is defined as follows,
① Bits 28 to 25 are used for identifying device IDs, defined as device_id, with the size of 4 bits and the value range of 0 to 15, and support 16 devices to communicate at most.
② Bits 24-11 are used for storing payload_length, the size is 14 bits, the value range is 0-16383, and the maximum supporting transmission data segment size is 16384 bytes.
③ Bits 10 to 0 of San are used for storing frame_index, the size is 11 bits, the value range is 0 to 2047, and 2048 segments of data frames are maximally supported to be transmitted.
Assuming that the data length to be transmitted is 16384 bytes, bit28 to bit25 are other node device ids to be transmitted by the user, and are defined by the user, bit24 to bit11 are 0x3FFF, and bit10 to bit0 are 0x7FF.
As shown in fig. 1, the transmission flow is as follows:
Step ①: frame_count, remain_frame_length, is calculated and frame_index is initialized to 0.
Step ②: is it determined that frame_index is less than frame_count?
Step ③: if frame_index is less than frame_count, extend _id_head is obtained and extend _id is calculated. If frame_index is greater than or equal to frame_count, then step ⑥ is performed.
Step ④: the frame_index data frame is obtained as a data frame to be transmitted, and the data frame length is 8 bytes.
Step ⑤: the current node sends the data frame onto the CAN bus and then accumulates frame_index by 1.
Step ⑥: and obtaining extend _id_head and calculating extend _id.
Step ⑦: acquiring a frame_index data frame as a data frame to be transmitted, wherein the frame_index data frame has a length of main_frame_length
Step ⑧: and the current node sends the data frame to the CAN bus, and the content of the sent data segment is finished.
As shown in fig. 2, the receiving flow is as follows:
step ①: one frame of data is received, and device_id, payload_length, frame_index values are calculated.
Step ②: is it determined whether the received device_id is equal to the node device_id? If equal, go to step ③, if not, discard the data frame, then go to step ①.
Step ③: if the result of subtracting frame_index from payload_length is greater than or equal to 8, then step ④ is performed, otherwise step ⑤ is performed.
Step ④: this data frame is placed in 8 byte length memory starting at recv_buf frame_index by 8.
Step ⑤: this data frame is placed in the (payload_length minus frame_index by 8) length memory starting from recv_buf frame_index by 8.
Step ⑥: the content of the data segment is stored in the recv_buf memory, and the receiving is finished.
It should be noted that, in the above system embodiment, each unit included is only divided according to the functional logic, but not limited to the above division, so long as the corresponding function can be implemented; in addition, the specific names of the functional units are also only for distinguishing from each other, and are not used to limit the protection scope of the present invention.
In addition, those skilled in the art will appreciate that all or part of the steps in implementing the methods of the embodiments described above may be implemented by a program to instruct related hardware, and the corresponding program may be stored in a computer readable storage medium.
The preferred embodiments of the invention disclosed above are intended only to assist in the explanation of the invention. The preferred embodiments are not exhaustive or to limit the invention to the precise form disclosed. Obviously, many modifications and variations are possible in light of the above teaching. The embodiments were chosen and described in order to best explain the principles of the invention and the practical application, to thereby enable others skilled in the art to best understand and utilize the invention. The invention is limited only by the claims and the full scope and equivalents thereof.