Communication method for 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 for supporting CAN bus transmission of more than 8 bytes of data.
Background
With the development of microcomputer control technology and industrial fieldbus technology, fieldbus-based communication is becoming more and more widely used in industrial control. The CAN bus is favored by many 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; at most 8 bytes of data can be transmitted in each frame, providing high real-time performance, etc. The performance advantage ensures that the CAN CAN be applied to a plurality of fields, and the CAN CAN be applied to the automobile industry, the ship transportation, the mechanical control, the factory automation, the building automation and the like.
The CAN field bus system is composed of a physical layer, a data link layer and an application layer 3 layer. The basic protocol specification of CAN is only physical layer protocol and data link layer protocol, and one of the core tasks of a system using CAN bus as a transmission mechanism is to design application layer protocol can2.0a standard CAN protocol of CAN bus and can2.0b extended CAN communication protocol to support transmitting 8 bytes of data at most each time, when one node transmits a frame of data segment with size exceeding 8 bytes to another node, the data segment with the length cannot be transmitted once by using either can2.0a or can2.0b communication protocol. The current embedded system is often single in function, simple in structure, limited in resources, but requires high transmission speed, reliability and efficiency, so that a novel application layer protocol with a simple structure is needed.
A communication protocol applied to a large amount of data based on a connected CAN bus, which has been completed by Wu Chaohui of zhejiang university and the like at present, realizes a connection-based application layer protocol for transmitting messages with data greater than 8 bytes. Although the protocol realizes the packed sending of data larger than 8 bytes, the protocol lacks a response mechanism of transmission, so that the condition of transmission errors cannot be processed. Particularly, the protocol can only realize the maximum data transmission of 256 × 7 bytes, has no mass data transmission mechanism, and cannot ensure the accuracy of the data transmission. Meanwhile, the protocol is based on connection, and connection needs to be established first in each transmission, so that the transmission efficiency is low.
The nomenclature used throughout this application notes:
1. a CAN data buffer area: data field interval in the CAN data frame.
2. And (3) data segment: one segment of data, which is the payload _ length power in the size range of 1 to 2, in units of bytes, cannot be transmitted through the CAN bus at one time when the size is larger than 8 bytes.
3. And (3) data frame: one frame of data with the size range of 0-8 and the unit byte CAN be transmitted through the CAN bus at one time.
4. payload _ length: data segment length, unit byte.
5. Integer frame: and (4) intercepting one section of data, namely an integer frame, at the size of 8 bytes from the initial position of the data section.
6. And (4) remaining frames: and a section of data with the size less than or equal to 8 is intercepted from the data section, and the length of the section of data is equal to the remainder of dividing payload _ length by 8.
7. frame _ count: the number of integer frames.
8. Main _ 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 represented relative to other segments.
10. Extended frame id: an identifier field in the CAN2.0B communication protocol.
11. Standard frame id: an 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 extension frame id when the CAN2.0b protocol is used.
13. device _ id: and the device id is different from the extended frame id/standard frame id and belongs to a part of the extended frame id/standard frame id.
14. extend _ id _ head: and the result of the left shift of the device _ id by 25 bits, the result of the left shift of the payload _ length by 11 bits and the frame _ index are subjected to bitwise OR operation to obtain the value.
15. extended _ id: synonymous with extension frame id, equal to extended _ id _ head, frame _ index bitwise or result.
16. recv _ buf: and the received data segment memory buffer is used for storing the content of the received data segment, and the length of the received data segment memory buffer is greater than or equal to the length of the data segment.
17. ID _ length: this value is 11 when the can2.0a communication protocol is selected, and is 29 when the can2.0b communication protocol is selected.
Disclosure of Invention
The invention aims to provide a communication method for supporting a CAN bus to transmit data with more than 8 bytes, which solves the problem of low transmission efficiency caused by the fact that the existing CAN bus cannot upload data with a plurality of 8 bytes by transmitting a data segment 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 transmission of more than 8 bytes of data, which comprises a data sending method and a data receiving method;
the data sending method comprises the following steps:
step S1: calculating frame _ count, domain _ frame _ length, initializing frame _ index to 0;
step S2: judging whether the frame _ index is smaller than the frame _ count;
and step S3: if the frame _ index is smaller than the frame _ count, acquiring an extended _ id _ head, and calculating the extended _ id = extended _ id _ head bitwise or frame _ index;
if the frame _ index is greater than or equal to the frame _ count, executing step S6;
and step S4: acquiring an 8-byte data frame starting from the frame _ index x 8 bytes as a data frame to be sent;
step S5: the current node sends the data frame to the CAN bus, then accumulates the frame _ index by 1, and returns to the step S2;
step S6: acquiring an extended _ id _ head, and calculating extended _ id = extended _ id _ head bitwise or frame _ index;
step S7: acquiring data with the length of a main _ frame _ length starting from the first frame _ index by 8 bytes of the data segment as a data frame to be sent;
step S8: the current node sends the data frame to the CAN bus, and the content of the data segment is sent;
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 received device _ id is equal to the device _ id of the node;
if yes, executing step F3;
if not, discarding the data frame and continuing to execute step F1;
step F3: judging whether the result of subtracting the frame _ index by 8 from the payload _ length is more than or equal to 8;
if yes, executing step F4;
if not, executing the step F5;
step F4: putting the data frame into an 8-byte length memory from the first frame _ index multiplied by 8 of recv _ buf;
step F5: putting the data frame into the (payload _ length minus frame _ index multiplied by 8) length memory from the first frame _ index multiplied by 8 of recv _ buf;
step F6: and the content of the data segment is stored in the recv _ buf memory, and the receiving is finished.
As a preferred technical solution, in step F1, payload _ length represents the length of a data segment, the data segment is truncated to generate an integer frame and a remainder frame, 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 frame is defined as remaining _ frame _ length, and the number of the remainder frame is 1, then the calculation formula between frame _ count, payload _ length, and remaining _ frame _ length is as follows:
frame_count=payload_length/8
remain_frame_length=payload_length%8。
as a preferred technical solution, the device _ ID, payload _ length, and frame _ index are fields of a data frame ID, and the size is defined as m bits, n bits, and k bits in sequence;
wherein m, n and k satisfy the following relationship:
as a preferred technical solution, the values of m, n and k are all customized by a user; after m is well defined, the following conditions are satisfied under the premise that n and k are removed:
as a preferred technical solution, the CAN bus uses a CAN communication protocol for transmission; the CAN communication protocol comprises CAD2.0A communication protocol and CAD2.0B communication protocol.
As a preferred technical solution, the CAD2.0A communication protocol supports 11-bit extension frame id; in the expansion frame ID 11, bit10 to bit9 are used for identifying the device ID, are defined as device _ ID, have the size of 2 bits, have the value range of 0 to 3, and support at most 4 devices for passing; bit 8-bit 3 is used for storing payload _ length, the size is 6 bits, the value range is 0-63, and the size of the most supported transmission data segment is 64 bytes; bit 2-bit 0 is used for storing frame _ index, the size is 3 bits, the value range is 0-7, and the maximum support is for transmitting 8 sections of data frames.
As a preferred technical solution, the CAD2.0B communication protocol supports 29-bit extension frame id; in the 29-bit extended frame ID, bits 28 to 25 are used for identifying the equipment ID, are defined as device _ ID, have the size of 4 bits and the value range of 0 to 15, and most 16 equipment is supported for communication; 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 the most supported transmission data segment is 16384 bytes; bit 10-bit 0 is used for storing frame _ index, the size is 11bit, the value range is 0-2047, and 2048 sections of data frames are supported to be transmitted maximally.
The invention has the following beneficial effects:
according to the invention, data to be transmitted is intercepted through a CAN protocol, an integer frame and a remainder frame generated after the interception are judged and processed, each segment of identification of the data frame is multiplied by eight, and the data frame is placed in 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 product in which the invention is practiced to achieve all of the above-described advantages at the same time.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments will be briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art that other drawings can be obtained according to the drawings without creative efforts.
FIG. 1 is a flow chart of data transmission according to the present invention;
fig. 2 is a flow chart of data reception according to the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The method provides a communication method for supporting the CAN bus to transmit data with more than 8 bytes, 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.
During communication, the length of a data segment is defined as payload _ length, the data segment is intercepted, and an integer frame and a remainder frame are generated after the 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 remaining _ frame _ length, and the number of the remainder frame is 1. The definition of each variable is 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 area stores a device _ id value, the size of which is defined as m bits.
payload _ length: this area stores a payload _ length value, the size of which is defined as n bits.
frame _ index: this field stores a frame _ index value, defined as k bits in size.
Wherein m, n, k satisfy the following relationship,
wherein, the values of m, k and n are set by a user, but generally, after m is determined, under the premise of ensuring the integer of n and k,
the invention mainly solves the problems that the traditional CAN2.0A standard CAN protocol 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 of more than 8 bytes to another node, the CAN2.0A or CAN2.0B communication protocol cannot be used for transmitting the data segment with the length once.
Embodiment one (taking CAN2.0A communication protocol transmission as an example)
Can2.0a supports 11-bit extension frame ID, so ID _ length =11, assuming that m =2, n =6, k =3, as defined below,
(1) the bits 10 to 9 are used for identifying the equipment ID, are defined as device _ ID, have the size of 2 bits and the value range of 0 to 3, and support 4 pieces of equipment to carry out communication at most.
(2) bit 8-bit 3 is used for storing payload _ length, the size is 6 bits, the value range is 0-63, and the size of the most supported transmission data segment is 64 bytes.
(3) bit 2-bit 0 is used for storing frame _ index, the size is 3 bits, the value range is 0-7, and the maximum support is for transmitting 8 sections of data frames.
Assuming that the length of data to be transmitted is 64 bytes, bits 10 to 9 are other node device ids to be transmitted by the user, and are defined by the user, bits 8 to 3 are 0x3F, and bits 2 to 0 are 0x7.
As shown in fig. 1, the transmission flow is as follows:
step (1): frame _ count, remaining _ frame _ length, is calculated, initializing frame _ index to 0.
Step (2): determine if frame _ index is less than frame _ count?
And (3): if the frame _ index is smaller than the frame _ count, the extended _ id _ head is obtained, and the extended _ id is calculated. If frame _ index is greater than or equal to frame _ count, step (6) is performed.
And (4): and acquiring the first frame _ index data frame as a data frame to be sent, wherein the length of the data frame is 8 bytes.
And (5): the current node sends the data frame onto the CAN bus and then accumulates frame _ index by 1.
And (6): and acquiring an extended _ id _ head and calculating the extended _ id.
And (7): acquiring the first frame _ index data frame as the data frame to be transmitted, with the length of main _ frame _ length
And (8): and the current node sends the data frame to the CAN bus, and the content of the data segment is sent to the end.
As shown in fig. 2, the receiving process is as follows:
step (1): receiving a frame of data, calculating device _ id, payload _ length, and frame _ index values.
Step (2): determine whether the receiving device _ id is equal to the device _ id of the node? If equal, executing step (3), if not equal, discarding the data frame, and then executing step (1)
And (3): if the result of subtracting frame _ index by 8 from payload _ length is greater than or equal to 8, then step (4) is executed, otherwise step (5) is executed.
And (4): this data frame is placed in the 8-byte length memory starting from the frame _ index multiplied by 8 of recv _ buf.
And (5): this data frame is placed in the (payload _ length minus frame _ index by 8) length memory starting at the frame _ index by 8 of recv _ buf.
And (6): and the content of the data segment is stored in the recv _ buf memory, and the receiving is finished.
Example two (taking CAN2.0B communication protocol transmission as an example)
Since can2.0b supports 29-bit extension frame ID, ID _ length =29, assuming that m =4, n =14, k =11, as defined below,
(1) the bits 28 to 25 are used for identifying the device ID, are defined as device _ ID, have the size of 4 bits and the value range of 0 to 15, and support 16 devices to carry out communication.
(2) 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 the most supported transmission data segment is 16384 bytes.
(3) The bit 10-bit 0 of San is used for storing frame _ index, the size is 11 bits, the value range is 0-2047, and 2048 data frames are maximally supported to be transmitted.
Assuming that the length of data to be transmitted is 16384 bytes, bits 28 to 25 are other node device ids to be transmitted by the user, and are defined by the user, bits 24 to 11 are 0x3FFF, and bits 10 to 0 are 0x7FF.
As shown in fig. 1, the transmission flow is as follows:
step (1): frame _ count, remaining _ frame _ length, is calculated, initializing frame _ index to 0.
Step (2): determine if frame _ index is less than frame _ count?
And (3): if the frame _ index is smaller than the frame _ count, the extended _ id _ head is obtained, and the extended _ id is calculated. If frame _ index is greater than or equal to frame _ count, step (6) is performed.
And (4): and acquiring the first frame _ index data frame as a data frame to be sent, wherein the length of the data frame is 8 bytes.
And (5): the current node sends the data frame onto the CAN bus and then increments frame _ index by 1.
And (6): and acquiring extended _ id _ head and calculating extended _ id.
And (7): acquiring the first frame _ index data frame as the data frame to be transmitted, with the length of main _ frame _ length
And (8): and the current node sends the data frame to the CAN bus, and the content of the data segment is sent.
As shown in fig. 2, the receiving process is as follows:
step (1): receiving a frame of data, calculating device _ id, payload _ length, and frame _ index values.
Step (2): determine whether the receiving device _ id is equal to the device _ id of the node? If equal, executing step (3), if not equal, discarding the data frame, and then executing step (1).
And (3): if the result of subtracting frame _ index by 8 from payload _ length is greater than or equal to 8, then step (4) is executed, otherwise step (5) is executed.
And (4): this data frame is placed in the 8-byte length memory starting from the frame _ index multiplied by 8 of recv _ buf.
And (5): this data frame is placed in the (payload _ length minus frame _ index by 8) length memory beginning at the frame _ index by 8 of recv _ buf.
And (6): and 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 included unit is only divided according to functional logic, but is not limited to the above division as long as the corresponding function can be implemented; in addition, specific names of the functional units are only for convenience of distinguishing from each other, and are not used for limiting the protection scope of the present invention.
In addition, it is understood by those skilled in the art that all or part of the steps in the method for implementing the embodiments described above may be implemented by a program instructing associated hardware, and the corresponding program may be stored in a computer-readable storage medium.
The preferred embodiments of the invention disclosed above are intended to be illustrative only. The preferred embodiments are not intended to be exhaustive or to limit the invention to the precise embodiments 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 utilize the invention. The invention is limited only by the claims and their full scope and equivalents.