The CAN bus communication that is used for mass data transfers based on message mechanism
Technical field
The present invention relates to the embedded real time system of computer realm, in particular, relate to a kind of communication protocol that is applied to mass data based on the CAN bus that connects.
Background technology
Along with the development of Control Technique of Microcomputer and industrial field bus technology, Fieldbus Based communication mode is able to application more and more widely in Industry Control.Wherein the CAN bus is simple in structure with it, and is with low cost, good functional characteristics and high reliability, and on-the-spot antijamming capability reaches open characteristics by force and receives numerous developers' favor.
The CAN universal serial bus has the advantage that transmission range is long, transmission speed is fast, and it has perfect error detection mechanism, adopts frame transmission means of " multiple access conflict arbitration " mechanism, can guarantee not drop-out; Can transmit 8 byte datas in each frame at most, very high real-time or the like can be provided.Advantage on the performance has guaranteed that CAN can be applied in a lot of fields, can see the application of CAN at auto industry, ship, mechanical control, factory automation, building automatic etc.
The CAN field bus system is made up of for 3 layers physical layer, data link layer and application layer.The basic agreement standard of CAN has only PHY and data link layer protocol, is exactly the application layer protocol of designs C AN bus and utilize the CAN bus as one of core missions of the system of transmission mechanism.Current Business agreement based on the CAN bus mainly contains DeviceNet and two kinds of agreements of CANopen, and these two kinds of bus protocols are universal agreements, and multiple functional, complex structure, very flexible, efficiency of transmission are lower, and occupying system resources is more.And current embedded system often function singleness, simple in structure, resource-constrained, but require that transmission speed is fast, reliability is high with efficient, so a kind of new application layer protocol simple in structure of needs.A kind of communication protocol that is applied to mass data based on the CAN bus that connects of having been accomplished by people such as the Wu of Zhejiang University morning sunlight has at present realized the application layer protocol based on connection that data are sent greater than the message of 8 bytes.Though this agreement has realized the packing greater than 8 byte datas is sent, and lacks the response mechanism of transmission, therefore can not handle the situation of loading error occurring.Particularly this agreement can only realize maximum 256*7 byte data transmission, does not have mass data transfers mechanism, and can not guarantee the accuracy of transfer of data.This agreement is based on connection simultaneously, and each transmission need connect earlier, so efficiency of transmission is lower.
Summary of the invention
Main purpose of the present invention is to overcome deficiency of the prior art, and a kind of CAN bus communication protocol based on message mechanism that is applicable to mass data transfers is provided.
In order to solve the problems of the technologies described above, the present invention realizes through following technical scheme:
The CAN bus communication that is used for mass data transfers based on message mechanism is characterized in that: CAN controller 11 bit identifiers of define equipment, and adopt master-slave mode to connect between the equipment, data realize transmission with connectionless-mode; Transfer of data adopts message mechanism, and a piece of news can send 65536 byte datas at most; When sending greater than 65536 byte datas, adopt packing manner, realize maximum 4G mass data transfers.
As a kind of improvement; The frame number of described multiframe data from 0 to 255 sign; Sequence number distributes since 1, and cycle assignment independent packet to the last that goes down continuously, and packet also should begin order from first independent packet and sends separately.
As a kind of improvement, the multiframe data are except that first frame, and every frame data must comprise a sequence number and 7 byte datas.And first frame comprises data length information.
As a kind of improvement, during mass data transfers, packet header from 0 to 65535 sign since 0 distribution, and distribute the bag to the last that goes down continuously, and single bag also begins the order transmission from first packet.
As a kind of improvement, described communication protocol comprises: principal and subordinate's connection management, transmission of messages management and mass data transfers management.
As a kind of improvement, described principal and subordinate's connection management comprises: main equipment initialization and slave unit initialization.
As a kind of improvement, described principal and subordinate's connection management implementation procedure is following: whole system can only have a main equipment, and 14 slave units can be arranged at most.Principal and subordinate's connection management comprises main equipment initialization and slave unit initialization, the initialization slave unit tabulation of main equipment initialization elder generation, and then initial message formation and broadcast registration message are waited for the slave unit response.The slave unit initialization begins from the initial message formation, then responds main equipment broadcasting, gets into message circulation and handles.All transmission are initiated by main equipment.
As a kind of improvement, every message of described message mechanism can be the single frames data, also can be the multiframe data.
As a kind of improvement, described transmission of messages management comprises: message transmission, wait-for-response and message sink.
As a kind of improvement, described transmission of messages management implementation procedure is following: according to different functions code in the message message is packaged into multiframe and sends, receiving terminal is same to unpack message according to different functions code in the message that receives.After every message has been sent; Wait for recipient's feedback message (if send be feedback message then no longer wait to be feedback); Whether acknowledge message is by correct reception; Be the recipient after receiving entire message, all can send a feedback message (if the message that receives be feedback message not to this message feedback), notice transmit leg content is by correct reception.If transmit leg waits for that 1s does not receive feedback message or feedback message mistake, then resend this frame and wait for 1s once more.If be retransmitted to three times, then be judged to be fault, stop to send and pointing out and make mistakes.Otherwise continue to send a piece of news down.So just guaranteed every correctness that message is sent.
As a kind of improvement, described mass data transfers management comprises: packing data, data transmission, Data Receiving and data unpack.
As a kind of improvement; Described mass data transfers management implementation procedure is following: for the data greater than 65536 bytes; Transmitting terminal is broken down into the multiple messages that is 65536 bytes to the maximum and sends; Packet header of definition in every message, and receiving terminal unpacks the multiple messages that receives according to this packet header again.After receiving terminal is received a bag, carry out message response, reception makes mistakes if transmitting terminal is received receiving terminal prompting bag, then retransmits a bag.Packet header is made up of two byte datas, and the bag sequence number is up to 65535 since 0 distribution.
Compared with prior art, the invention has the beneficial effects as follows:
The present invention be directed to mini-system and use and demand, the upper-layer protocol of exploitation has been realized maximum 4GB mass data transfers.Utilize message mechanism and mass data transfers mechanism, ensured the high efficiency and the accuracy of transfer of data.Because protocol architecture is simple, it is few to have the resource of taking, the characteristics that efficient is high.
Description of drawings
Fig. 1 is a system construction drawing of the present invention.
Fig. 2 is the flow chart of transmission of messages of the present invention.
Fig. 3 is the flow chart of mass data transfers of the present invention.
Embodiment
In conjunction with accompanying drawing, through specific embodiment the present invention is elaborated below.
MicroCAN adopts principal and subordinate's connected mode of server/client mode.The CAN bus is a kind of multi-master bus, and each node can send message to other nodes on the bus.And in mini-system is used, by the transmission of messages and the state of each node of main frame control, and the real-time that guarantees message response, therefore adopt master slave mode can guarantee stability of data transmission and accuracy.
The CAN controller is judged priority, condition of acceptance, function etc. through identifier, so identifier is the core of CAN host-host protocol, the MicroCAN agreement is distributed 11 bit identifiers, and is as shown in table 1.
Table 1
Occupy-place number (high) to low |
10 |
9~6 |
5~2 |
1~0 |
Implication |
1 |
From node address |
Function code |
Subfunction |
In the identifier agreement that MicroCAN distributes, 9~6bit representes node address, and host address is 0x0; Slave addresses can be 0x1~0xE; 0xF representes broadcast frame, and 5~2bit and 1~0bit be presentation function code and subfunction respectively, and content is distributed as shown in table 2.
Table 2
MicroCAN message mechanism standard Data Transport Protocol between the master-slave equipment, transfer of data is to be the unit with message between the master-slave equipment, process such as transmission course comprises transmission, receives, replys, repeating transmission.Every message can be the single frames data, also can be the multiframe data, and maximum can be sent 65536 byte datas.
The definition message structure is following:
typedef?struct_MCAN_MESSAGE_STRUCT{
BOOL bServerSend; The message that // sign main frame sends or receives
BYTE byAddress; // node address
MCANMajorFunction CommType; // the function of tonic chord is described
MCANDeviceType HostDeviceType; // main equipment type
MCANDeviceType ClientDeviceType; // slave unit type
BYTE byCommParam; // functional description
MCANMessageState MessageState; // message status
WORD wDataLen; // data length
BYTE*pData; // data
_ MCAN_MESSAGE_STRUCT*pNextMessage; // following a piece of news
}MCANMessage;
According to different functions code in the message message is packaged into multiframe and sends, receiving terminal is same to unpack message according to different functions code in the message that receives.
After every message has been sent; Wait for recipient's feedback message (if send be feedback message then no longer wait to be feedback); Whether acknowledge message is by correct reception; Be the recipient after receiving entire message, all can send a feedback message (if the message that receives be feedback message not to this message feedback), notice transmit leg content is by correct reception.If transmit leg waits for that 1s does not receive feedback message or feedback message mistake, then resend this frame and wait for 1s once more.If be retransmitted to three times, then be judged as fault, stop to send and pointing out and make mistakes.Otherwise continue to send a piece of news down.So just guaranteed every correctness that message is sent.As long as the content of sending is as required filled message structure; Transmit leg will be packed to sending message according to agreement automatically; And the recipient equally also will unpack according to agreement butt joint collection of letters breath, so message mechanism can guarantee the correctness that message is sent and received.
Can ensure that based on MicroCAN message mechanism host-host protocol is not more than 65536 byte data transmission of messages successes, but, then can't realize for the big bag transfer of data more than the 65K.In view of the defective of this agreement in the existence of big data quantity transmission course, need expand, make it satisfy the big data quantity transmission requirements.
The MicroCAN protocol specification big data quantity host-host protocol, comprise to big data decompose, repack, send, receive, unpack, step such as data splitting.
Whether once be the detection of greatly wrapping data, if unpack data splitting after receiving for all bags such as big Bao Ze if doing earlier when the recipient receives data at every turn.Packet header is made up of two byte datas, and 0x0000 representes initial bag, and maximum is 0xFFFF, because every message can be sent the data of 65535 bytes at most, therefore a data transfer heap(ed) capacity can reach 4G.
The distributed system that constitutes based on the CAN bus is except that having higher real-time, and multiple error detection mechanism has guaranteed the reliability and the anti-interference of communication in its agreement, is fit to very much in the boats and ships cabin narrow and small application scenarios, space such as communication.The MicroCAN agreement is utilized message mechanism and big data quantity transmission mechanism under the prerequisite that guarantees higher reliability and stability, it is few to have the resource of taking, and the characteristics that efficiency of transmission is high particularly have good practicability in the built-in small system.
What should be understood that is: the foregoing description is just to explanation of the present invention, rather than limitation of the present invention, and any innovation and creation that do not exceed in the connotation scope of the present invention all fall within protection scope of the present invention.