Communication program module and application and pre-compilation management method of communication program module

By designing a unified communication program module, using pre-compiled instruction management function modules and communication media, the reliability and compatibility problems of communication networks in the prior art are solved, and an efficient and scalable communication solution is realized.

CN113703771BActive Publication Date: 2024-12-10BAFANG ELECTRIC (SUZHOU) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110965450.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-23
Publication Date
2024-12-10
Estimated Expiration
2041-08-23

AI Technical Summary

Technical Problem

The existing communication network lacks a unified communication program module, resulting in repetition of work, inconsistent reliability, poor moduleability, poor transplantability, poor compatibility and poor scalability, affecting work efficiency and increasing system costs.

Method used

A communication program module is designed, including configuration files, working mode sublayer, functional module sublayer and common sublayer. Through pre-compiled instruction switch management function module, it supports a variety of communication media such as CAN bus, BLE, 4G/5G. WIFI, etc., realize modular programming and management, and improve communication efficiency and stability.

Benefits of technology

Implement the modular structure of the communication program module, simplifies code management, reduces costs, improves communication efficiency and reliability, enhances compatibility and scalability, avoids duplicate writing work, and is suitable for a variety of devices and scenarios .

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113703771B_ABST
    Figure CN113703771B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of embedded software development technology, and specifically to a communication program module and application, as well as a pre-compilation management method for the communication program module. The communication program module includes a configuration file, a working mode sublayer, a function module sublayer, and a common sublayer; the working mode sublayer is used to select and set the working mode of the communication program module; the function module sublayer includes commonly used communication function modules, and a main communication medium is provided in the function module sublayer; the common sublayer includes general programs and public data, and the general program can be called by the working mode sublayer and the function module sublayer; the configuration file stores a pre-compilation instruction switch, and before compiling, turning on or off the corresponding pre-compilation instruction can enable or disable the corresponding function. The communication program module of the present invention has strong communication reliability, is easy to maintain, has high communication efficiency, enhances the compatibility and extensibility of the program module, and has stronger versatility.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the technical field of embedded software development, in particular to a communication program module and application and a pre-compilation management method of the communication program module. Background Art

[0002] Usually in a product system, there are many electronic devices, which follow certain communication protocols and are connected through the network to form a complete system. With the development of technology, the expansion of customer groups, and the increasing diversification of product demands, the communication network of electronic products has become more and more complex, and the functions and burdens of software engineers have also become greater and greater. If there is no unified communication program module and no good program management method, each software engineer and supplier will write programs in his own way, resulting in duplication of work, and it is easy to have data interaction problems caused by inconsistent understanding and inconsistent reliability, poor modularity, poor portability, poor compatibility, poor scalability, and lack of maintainability, which is not conducive to unified management, affects work efficiency, and increases system costs, and therefore needs to be improved. Summary of the invention

[0003] The purpose of the present invention is to provide a communication program module that can solve the problems described in the background technology that the existing communication network does not have a unified communication program module program, resulting in repeated work, inconsistent reliability, poor modularity, poor portability, poor compatibility, poor scalability, lack of maintainability, unfavorable for unified management, affecting work efficiency, and increasing system costs.

[0004] To achieve the above objectives, the present invention is implemented through the following technical solutions: a communication program module, characterized in that it includes a configuration file, a working mode sublayer, a function module sublayer and a common sublayer;

[0005] The working mode sublayer is used to select and set the working mode of the communication program module. The working mode sublayer is provided with commonly used program working modes;

[0006] The function module sublayer is used to store commonly used communication function modules, and the function module sublayer is provided with a main communication medium;

[0007] The common sublayer contains common programs and common data, which can be called by the working mode sublayer and the functional module sublayer;

[0008] The configuration file stores pre-compilation instruction switches. Each common function in the functional module sublayer corresponds to a pre-compilation instruction switch in the configuration file. Before compiling, the developer turns on or off the corresponding pre-compilation instruction and enables or disables the corresponding function according to product requirements.

[0009] As a further improvement of the communication program module in the above scheme, the main communication medium of the communication medium sublayer is a CAN bus. The selection of the CAN bus as the main communication medium can improve the communication quality and reliability.

[0010] As a further improvement of the communication program module in the above scheme, it also includes a communication medium sublayer, and the communication medium sublayer also includes sub-communication media, and the sub-communication media include but are not limited to one or more of BLE, 4G\5G, WIFI, 485 bus, zigbee or GPS. The communication medium sublayer contains the protocol stack code of all sub-communication media, which is used to parse and package the received and sent data of the sub-communication media according to the protocol stack; the communication medium sublayer is used to connect more than two communication media, and the working mode of the communication medium sublayer depends on the working mode sublayer. The sub-communication medium depends on the main communication medium and can only form a communication network with the main communication medium. The above-mentioned sub-communication media can be selected or not, and one or more can be selected.

[0011] As a further improvement of the communication program module in the above scheme, the protocol stacks of the sub-communication media should be independent of each other. If there is a common code, it needs to be processed through the logical operation between the pre-compiled instruction switches in the configuration file. Because the protocol stacks of all sub-communication media are independent of each other, any sub-communication medium can be opened or closed as needed.

[0012] As a further improvement of the communication program module in the above scheme, each sub-communication medium has a corresponding pre-compilation instruction switch in the configuration file. When the pre-compilation instruction switch is turned on, the corresponding module will be compiled during compilation. When the pre-compilation instruction switch is turned off, the corresponding module will not be compiled during compilation.

[0013] In actual applications, the communication network may have various types of combinations according to the situations of the main communication medium and the sub-communication medium. Different combinations may require different hardware devices and different communication protocols, which will complicate the management of the communication transmission layer program and make the program code bloated, bringing inconvenience to the unified management of the code. Therefore, the present invention adopts the method of pre-compiled instructions to perform modular programming and management for the main communication medium and the sub-communication medium according to actual needs, which can avoid the bloated execution of the program, improve the operating efficiency, and reduce the program error rate.

[0014] As a further improvement of the communication program module in the above solution, the sub-communication medium of the communication medium sub-layer can be expanded and upgraded to add a new protocol stack. Through this setting, the communication medium can expand the application scope of the communication program module according to the needs of the communication device.

[0015] As a further improvement of the communication program module in the above scheme, the communication devices connected to the communication medium sublayer include master node devices and slave node devices; both the master node devices and the slave node devices can actively send data to the main communication medium; the master node device has the authority to actively request the slave node device to read and write data, and the slave node device cannot actively send messages to the communication network except for broadcast messages, and the slave node device must respond after receiving the request from the master node device. Setting the communication devices as master node devices and slave node devices respectively, and setting the functions of the master node devices and slave node devices can further improve the communication efficiency.

[0016] As a further improvement of the communication program module in the above scheme, when there is a sub-communication medium in the communication network, the master node device and the slave node device can be used to serve as the data transfer channel between the sub-communication medium and the main communication medium. The configuration file of the master node device as the data transfer channel has a pre-compiled instruction switch corresponding to each sub-communication medium. Before compiling, turning on the pre-compiled instruction switch corresponding to the sub-communication medium will enable the master node device to process the functional module of the sub-communication medium and the message on the sub-communication medium. In the above scheme, the master node device is used as the data channel between the sub-communication medium and the main communication medium. Only the master node device will be connected to the sub-communication medium. Therefore, it is only necessary to have an IOT module on the master node device, and there is no need to set up an IOT module on other communication devices of the main communication medium. Because the IOT module has a high hardware cost and will occupy the execution time of the single-chip microcomputer, it will affect the execution efficiency. Therefore, the above scheme can reduce costs and reduce the operating load of the single-chip microcomputer, and improve the stability and efficiency of communication.

[0017] As a further improvement of the communication program module in the above scheme, the master node device can be set in the sub-communication medium. The master node device on the sub-communication medium needs to turn on the master node mode pre-compilation instruction switch before compilation. At this time, the master node device can actively request and read the data of any device on the main communication medium according to demand, and can also actively request to write data to any device on the main communication medium; in a communication network, there is one or more master node devices; the master node device can actively request to read and write data from the slave node device, and can also be requested to read and write data by other master node devices. In this mode, the master node device is both a master node mode device and a slave node mode device. This mode can be used when there are more than two master node devices in the communication network.

[0018] As a further improvement of the communication program module in the above scheme, the working modes in the working mode sublayer include 1ms mode and non-1ms mode; in 1ms mode, the main function of the communication program module program will be called once every 1ms, and each time it is called, it will only process one frame of message, and it is required that this frame of message be processed in the shortest possible time; in non-1ms mode, the calling cycle of the main function of the communication program module program is greater than 1ms. Both modes have their own advantages. Compared with the 1ms mode, the non-1ms mode has low execution efficiency and slow message response, but it is applicable to more scenarios; modes with a cycle greater than 1ms are all non-1ms modes, usually 10ms or 100ms, and can also be set according to actual needs; in 1ms mode, the communication program module message includes dynamic message and static message, and the execution time of the dynamic message is required to be no more than 12us; the execution time of the static message is required to be no more than 25us. In the communication program network, if the product is not sensitive to the 25us execution time, the upper layer of the product program may not distinguish between dynamic messages and static messages; conversely, the upper layer of the product program must distinguish between dynamic and static situations, and only execute dynamic messages when the execution time requirement is high; and execute static messages when the execution time requirement is not so high.

[0019] As a further improvement of the communication program module in the above scheme, the function modules of the function module sublayer include but are not limited to single-frame message writing, single-frame message reading, multi-frame message writing, multi-frame message reading, write timeout retransmission, read timeout termination, broadcast message and termination message; each function module is independent of each other, and each of the above function modules corresponds to a pre-compiled instruction switch in the configuration file. Developers can enable or disable the corresponding function by turning on or off the corresponding pre-compiled instruction according to product requirements. By setting the above function modules, any function module can be enabled or disabled in the configuration file, which can not only ensure the completeness of the functions, including all commonly used function modules, but also disable unnecessary function modules according to user needs, simplify the communication program module, and improve the operation efficiency.

[0020] As a further improvement of the communication program module in the above scheme, the common code contained in the common sublayer includes but is not limited to FIFO logic related programs and CRC check programs, and the common data in the common sublayer includes but is not limited to CAN message sending and receiving buffers, and sub-communication medium message sending and receiving buffers. FIFO logic is first-in first-out logic, and CRC check programs are cyclic redundancy check programs. These programs are set in the common sublayer and can be called by other layers, which can simplify each layer of the communication program module and improve operation efficiency.

[0021] The second inventive object of the present invention is to provide an application of the above-mentioned communication program module, which is used for power-assisted electric bicycles. The main communication medium of the communication program module is used to connect the electronic equipment of the power-assisted electric bicycle, and the sub-communication medium is connected to the communication equipment. The communication equipment includes but is not limited to mobile phones, computers, and pad devices. The communication equipment can obtain data of the electronic equipment on the main communication medium in real time, and can configure the parameters of the electronic equipment according to needs, control the electronic equipment, and upgrade or maintain the program of the electronic equipment.

[0022] The communication program module of the present invention can be used as a communication module for power-assisted electric bicycles. The main communication medium is the CAN bus, which is used to connect all electronic devices of power-assisted electric vehicles, such as ECU, HMI, BMS, etc., for data interaction between such devices. The sub-communication medium is more diverse, and can be BLE, 4G\5G network, WIFI, etc. The existence of the sub-communication network allows customers or after-sales personnel to use handheld devices represented by mobile phones to obtain data from electronic devices on the main communication medium bus in real time, and can configure the parameters of electronic devices according to needs, remotely upgrade the programs of electronic devices, etc. Since IOT modules such as BLE, 4G\5G, and WIFI have high hardware costs, they will occupy the execution time of the single-chip microcomputer and affect the execution efficiency. Therefore, not all devices need to be connected to the sub-communication medium; such as Figure 1 As shown, the electronic equipment on the main communication medium is divided into master node equipment and slave node equipment. Only the master node equipment will be connected to the sub-communication medium, so only the master node equipment needs to be equipped with an IOT module; the sub-communication medium equipment will use the master node equipment as a medium to interact with the electronic equipment on the CAN bus to complete data acquisition, parameter configuration, signal control, program upgrade and other functions; the master node equipment here is usually the inherent equipment of the power-assisted electric vehicle system, and the most commonly used is HMI.

[0023] The third invention object of the present invention is to provide a pre-compilation management method for a communication program module, the method comprising: when writing a program, first determine the communication medium of the communication device, and open the pre-compilation instructions corresponding to the determined communication medium through the configuration file in the communication program module to enable the functional module corresponding to the communication medium, the communication medium includes the main communication medium, and the main communication medium is the CAN bus; determine the working mode of the communication device, the working mode of the communication device includes the master node mode and the slave node mode, if the communication device is the master node device, open the pre-compilation instructions corresponding to the master node mode through the configuration file in the communication program module; if the communication device is the slave node device, open the pre-compilation instructions corresponding to the slave node mode through the configuration file in the communication program module; the same communication device can be set to the master node mode and the slave node mode at the same time; determine the functional module of the communication device, and open the pre-compilation instructions corresponding to the required functional module through the configuration file in the communication program module to enable the required functional module; each functional module is independent and does not interfere with each other.

[0024] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, the communication program module is the management center of the communication network data. The driver layer in the communication network obtains data from the communication medium in the network and uploads the data to the communication module program. The communication module program parses and processes the data according to the protocol corresponding to the communication medium and then uploads it to the upper layer for processing. The upper layer program transmits the data to be sent to the communication module program. The communication module program packages the data according to the protocol corresponding to the communication medium and then transmits it to the driver layer, which sends it to the communication network.

[0025] As a further improvement of the pre-compilation management method of the communication program module, the communication program module is provided with a configuration file, which is a header file used to configure the pre-compilation instruction switch. The user can configure the header file according to the needs to open or close the corresponding functional module in the communication program module. Through this setting, the communication program module can be modified and set to enable the required functional modules and disable the unnecessary functional modules, so that there is no redundant content in the compiled program and it can meet the needs of the user.

[0026] As a further improvement of the pre-compilation management method of the communication program module, after configuring the header file of the pre-compilation instruction switch, the communication program module can compile and generate a lib file. Other developers only need to load the lib file into their own program engineering files and compile. Before compiling, the corresponding functional modules are turned on or off with the pre-compilation instruction in the configuration header file in the communication program module. In this way, the pre-compilation instruction after configuration can be directly applied to the same type of products, which can avoid repeated development of programs and improve the development efficiency of programs.

[0027] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, the communication medium also includes a sub-communication medium, and the communication device on the sub-communication medium can be set to a master node mode or a slave node mode or both a master node mode and a slave node mode; the master node device on the sub-communication medium needs to turn on the master node mode pre-compilation instruction switch before compilation, at which time the master node device can actively request and read data from any device on the main communication medium according to demand, and can also actively request to write data to any device on the main communication medium; the slave node device on the sub-communication medium needs to turn on the slave node mode pre-compilation instruction switch before compilation, at which time the slave node device can only passively obtain data from the device on the main communication medium; the same communication device on the sub-communication medium can actively request and passively obtain data when it has both master and slave node modes, and needs Before compiling, the pre-compilation instruction switches of the master node mode and the slave node mode should be turned on at the same time; when the device on the sub-communication medium is in the master node mode, the device on the sub-communication medium actively sends a request message to the master node device on the main communication medium. When the master node device turns on the pre-compilation instruction switch corresponding to the sub-communication medium in the configuration file, it will process the received data. The master node device determines whether the message is a read message, a write message or a broadcast message according to the message type, and determines whether the message is only for itself or needs to be transmitted to other devices through the main communication medium. After the judgment, it makes appropriate processing; the master node device receives messages from other devices on the main communication medium, and determines whether it needs to be transmitted to the device on the sub-communication medium. If necessary, the data is packaged according to the sub-communication medium protocol and sent to the sub-communication medium device. By distinguishing the communication devices into master node devices and slave node devices, and setting the master node devices and slave node devices according to the application requirements of the devices, it can not only ensure that the functions of each device can be fully realized, but also improve the communication efficiency.

[0028] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, the functional modules of the communication equipment include but are not limited to single-frame message writing, single-frame message reading, multi-frame message writing, multi-frame message reading, write timeout retransmission, read timeout termination, broadcast message and termination message; each functional module is independent of each other, and each of the above-mentioned functional modules corresponds to a pre-compilation instruction switch in the configuration file. The developer can enable or disable the corresponding function by turning on or off the corresponding pre-compilation instruction according to product requirements.

[0029] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, in the functional module of the communication device, if the data content of the message is less than or equal to 8 bytes, a single-frame message is used; if the data content of the message is greater than 8 bytes, a multi-frame message is used; in the configuration file of the communication program module, turning on the corresponding pre-compilation instruction switch will enable the functional module, otherwise, the functional module will be disabled; a single-frame message only needs one response and one answer to end; a multi-frame message needs to be divided into multiple frames according to the data length, and each frame message needs one response and one answer, and it is considered to be over after the last frame message is received.

[0030] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, for the master node device or the slave node device, on the basis of the message reading function module, a read timeout error function module is also provided; in the configuration file of the communication program module, the pre-compilation instruction switch of the read timeout error function is turned on or off, then the read timeout error function can be enabled or disabled; after the read timeout error function is enabled, after the master node device actively initiates a read message request, if no response message is received from the slave node device within the specified time, a read timeout error will be triggered, and the master node device will send an error termination message to the slave node device; in a multi-frame read message, if the master node sends a read timeout error termination message, it means that this multi-frame message sequence ends prematurely.

[0031] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, for the master node device or the slave node device, on the basis of the message writing function module, there is also a write timeout retransmission function module; in the configuration file of the communication program module, the pre-compilation instruction switch of the write timeout retransmission function is turned on or off, then the write timeout retransmission function can be enabled or disabled; after the write timeout retransmission function is enabled, after the master node device actively initiates a write request message, if no response message is received from the slave node device within the specified time, the frame message will be retransmitted; if no response message is received from the slave node device within the specified number of retransmissions, an error will be reported and the multi-frame message will be terminated; the number of retransmissions and the timeout retransmission time can be configured in the configuration header file according to needs.

[0032] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, the master node device will actively initiate a write request command message to the slave node device. After receiving the write request command, the slave node device needs to respond in accordance with the protocol format; in a system containing a sub-communication medium, the device on the sub-communication medium can initiate a write request command message to the master node device on the main communication medium, or send a write request command message to other devices on the main communication medium through the master node device on the main communication medium; after the master node device on the main communication medium receives the write request command message initiated by the sub-communication medium device, it parses it according to the protocol to determine whether the command is for itself or for other devices; if it is a command for itself, it responds according to the command requirements; if it is a command for other devices, the message is packaged according to the CAN protocol format and transmitted to the corresponding device through the CAN bus; after the corresponding device receives the command, it responds according to the command requirements; after receiving the response from the corresponding device, the master node device packages it according to the protocol format of the sub-communication medium device and then transmits it to the sub-communication medium device.

[0033] As a further improvement of the pre-compilation management method of the above communication program module, both the master node device and the slave node device can actively send broadcast messages; the broadcast messages are sent periodically; the data of each frame of the broadcast message is 0-8 bytes; and the broadcast message does not require a response.

[0034] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, the communication program module includes a 1ms mode and a non-1ms mode; different types of devices in the system have different requirements for program execution efficiency and execution time. In order to be compatible with more devices, a pre-compilation instruction switch of the 1ms mode is added; when the pre-compilation instruction switch of the 1ms mode is turned on, the communication program module needs to work in a 1ms environment, and the main function interface program of the communication program module needs to be called once in 1ms. At this time, the communication program module can only process no more than one frame of messages in 1ms; the main function execution time of the communication program module is strictly controlled and cannot affect the operation of other module programs, let alone the accuracy of the 1ms time; when the pre-compilation instruction switch of the 1ms mode is turned off, the communication program module works in a non-1ms environment, and the main function interface program of the communication program module can be called once every 10ms or even 100ms according to the needs of the developer; each time the main function is called, all the received messages in the FIFO file and the broadcast messages that need to be processed will be processed. If it is a master node device, all the messages that the upper layer wants to send must be processed.

[0035] As a further improvement of the pre-compilation management method of the above-mentioned communication program module, the communication program module includes a BUSOFF function module for CAN bus communication; turning on the pre-compilation instruction switch of the BUSOFF module can enable the function; otherwise, the function is disabled; BUSOFF is used to handle problems when errors occur on the CAN bus in the system; CAN bus errors include but are not limited to short circuit, open circuit, reverse connection or interference; BUSOFF will reinitialize the CAN bus within a certain period of time after the CAN bus error occurs, depending on the situation.

[0036] The beneficial effects of the invention are: 1) The communication program module of the present invention is set as a modular structure, which simplifies the structure, reduces the cost, and improves the communication efficiency; 2) The communication program module of the present invention has strong portability and can be transplanted to similar products for promotion and application, which can avoid the work of repeated program writing; 3) The communication program module of the present invention is consistent, has strong communication reliability, is easy to maintain, and is conducive to management; 4) The communication program module of the present invention is consistent, which enhances the compatibility and scalability of the program module, and new sub-communication media and communication equipment can be added as needed, so that its application field is wider and its versatility is stronger. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] Figure 1 It is a schematic diagram of the structure of the communication program module of the present invention in the product program architecture.

[0038] Figure 2 FIG. 4 is a schematic diagram of a communication network of a first exemplary product system of the present invention.

[0039] Figure 3 Schematic diagram of the communication network of the second exemplary product system of the present invention.

[0040] Figure 4 It is a communication network diagram of the third exemplary product system of the present invention.

[0041] Figure 5 It is a schematic diagram of the internal hierarchy of the communication program module of the present invention.

[0042] Figure 6 It is an example schematic diagram of the calling relationship between the internal levels of the communication program module of the present invention; in the figure, the thick solid line with an arrow represents the data transmission direction, and the thin solid line with an arrow represents the program logic execution order.

[0043] Figure 7 It is another example schematic diagram of the calling relationship between the internal levels of the communication program module of the present invention; in the figure, the thick solid line with an arrow represents the data transmission direction, and the thin solid line with an arrow represents the program logic execution order.

[0044] Figure 8It is a comprehensive schematic diagram of the module hierarchy of the exemplary communication program of the present invention and the corresponding internal modules.

[0045] The reference numerals in the figure are: upper layer 1, communication program module 2, configuration file 21, working mode sublayer 22, communication medium sublayer 23, sub-communication medium 231, functional module sublayer 24, main communication medium 241, common sublayer 25, master node device 26, slave node device 27, lower layer 3, other layers 4. DETAILED DESCRIPTION

[0046] The technical scheme of the present invention is clearly and completely described below through embodiments. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0047] Based on the problems existing in the existing product systems described in the background technology, in order to improve work efficiency and avoid duplication of work, it is necessary to divide the architecture of the software development of all products. In this way, in the product system, the communication program modules of all products are separated as an independent module. Since the products of the same system follow the same protocol, the communication program modules are unified. Therefore, the communication program modules can be separated in the system and written into standard modules using the general syntax in C language. They can be compiled on commonly used compilers, or lib files can be generated, or they can be transplanted into product projects for use by all products.

[0048] Figure 1 This is a simple example of the software architecture diagram of an electronic product. The communication program module has become an independent module and a layer of its own; above the communication program module 2 is the upper layer 1, which is usually the application layer in the AUTOSAR architecture; below the communication program module 2 is the lower layer 3, which is usually the driver layer; the other layer 4 includes all layers except the communication layer, application layer and driver layer, and there will be different division methods depending on the product.

[0049] like Figure 1 and Figure 5 As shown, the communication program module 2 of the present invention includes a configuration file 21 , a working mode sublayer 22 , a communication medium sublayer 23 , a function module sublayer 24 and a common sublayer 25 .

[0050] After the hierarchical division of the communication program module example, in actual application, use the Keil, IAR and other compilers to create a project for the communication program module. Under the project directory, create a new group for each sub-layer. In the group, create a .c source file and a .h header file for each sub-module. In addition, you can also create other header files according to personal needs. In this way, a tree diagram will be constructed according to the hierarchy in the project directory, making the hierarchical relationship clearer.

[0051] The master-slave calling relationship must be followed between the layers. After the master-slave relationship is established, in principle, the master layer can call the functions of the slave layer, but the slave layer cannot call the functions of the master layer. If the slave layer needs to use the functions of the master layer for some special reason, it can be achieved through the callback function.

[0052] In the communication program module example, the working mode sublayer 22 is the main layer and can call the functions of the other three layers. All main function logic and main state machines are processed in this layer; the communication medium sublayer 23 and the functional module sublayer 24 are level 1 subordinate layers and can both call the common layer 25, but the two levels are equal and cannot call each other; the common sublayer is a level 2 subordinate layer and can be called by other layers but cannot call functions of other layers.

[0053] The following is a detailed description of each sub-layer of the communication program module 2.

[0054] The working mode sublayer 22 is the basis for the communication program module to work. It is used to select and set the working mode of the communication program module, and determines important parameters such as the working mode, execution efficiency, message response time, and accuracy of periodic messages of the communication program module. The working mode sublayer 22 is provided with commonly used program working modes, such as Figure 5 As shown, the working mode sublayer 22 is provided with a 1 ms working mode and a master-slave node mode.

[0055] In 1ms mode, the main function of communication program module 2 will be called once every 1ms. Each time it is called, only one frame of message will be processed, and this frame of message is required to be processed in the shortest possible time. In 1ms mode, the communication program module message can be divided into two types according to the execution time: a dynamic message, which requires an execution time of no more than 12us; a static message, which requires an execution time of no more than 25us. If the product is not sensitive to the execution time of 25us, the upper layer of the product program can be treated without distinguishing between dynamic and static messages. On the contrary, the upper layer of the product program should distinguish between dynamic and static situations. In situations where the execution time requirement is high, only dynamic messages are executed; in situations where the execution time requirement is not so high, static messages are executed. Compared with the 1ms mode, the non-1ms mode has low execution efficiency and slow message response, but it is applicable to more scenarios. Modes with a period greater than 1ms are all non-1ms modes, usually 10ms or 100ms, which can also be set according to actual needs.

[0056] The master-slave node mode determines the position of the electronic device in the communication network. The device with the master node mode is the master node device 26, and the device with the slave node mode is the slave node device 27. The master node device 26 can request the slave node device 27 to read and write data, and the slave node mode 27 cannot actively send messages to the communication network except for broadcasting messages, and responds after receiving the request from the master node device 26. The master and slave node modes are independent of each other. The device can have only the master node mode, only the slave node mode, or both the master and slave node modes.

[0057] The master node device 26 can be set in the main communication medium 241 or in the sub-communication medium 231. The master node device 26 on the sub-communication medium 231 needs to turn on the master node mode pre-compilation instruction switch before compilation. At this time, the master node device 26 can actively request and read data from any device on the main communication medium according to demand, and can also actively request to write data to any device on the main communication medium 241. In the same communication network, more than one master node device 26 can be set, and this master node device 26 can also have a slave node mode at the same time. Other master node devices can request to read and write data from the master node device with the slave node mode.

[0058] The communication medium sublayer 23 is used to connect more than two communication media. The working mode of the communication medium sublayer 23 depends on the working mode sublayer. The communication medium sublayer 23 is provided with a sub-communication medium 231. The sub-communication medium 231 is one or more of BLE, 4G\5G, WIFI, 485 bus, zigbee or GPS. Each standard protocol of the sub-communication medium 231 has at least one protocol stack. The sub-communication medium 231 depends on the main communication medium in the general function module and can only form a communication network with the main communication medium. The sub-communication medium can be expanded and upgraded to add a new protocol stack. The communication medium sublayer can be selected or not. One sub-communication medium or multiple sub-communication media can be selected.

[0059] like Figure 2-4 As shown, there are three structures of the communication medium sublayer, and these three structures can be applied to the communication program module of the power-assisted electric bicycle.

[0060] exist Figure 2 In the system, there are both a main communication medium 241 and a sub-communication medium 231. The main communication medium 241 is a CAN bus, which is used to connect all electronic devices of the power-assisted electric vehicle, such as ECU, HMI, BMS, etc., for data interaction between such devices; the sub-communication medium 231 is more diverse, and can be BLE, 4G\5G network, WIFI, etc.; the existence of the sub-communication network allows customers or after-sales personnel to use handheld devices represented by mobile phones to obtain data of electronic devices on the main communication medium bus in real time, and can configure the parameters of the electronic devices according to needs, remotely upgrade the programs of the electronic devices, etc. Since IOT modules such as BLE, 4G\5G, and WIFI have high hardware costs, they will occupy the execution time of the microcontroller and affect the execution efficiency. Therefore, not all devices need to be connected to the sub-communication medium; such as Figure 2 As shown, the electronic devices on the main communication medium 241 are divided into a main node device 26 and a slave node device 27. Only the main node device 26 will be connected to the sub-communication medium 231, so only the main node device 26 needs to have an IOT module; the sub-communication medium 231 device will use the main node device 26 as a medium to interact with the electronic devices on the CAN bus to complete data acquisition, parameter configuration, signal control, program upgrade and other functions; the main node device here is usually an inherent device of the power-assisted electric vehicle system, and the most commonly used is HMI.

[0061] exist Figure 3 In the system, there is only the main communication medium 241, but no sub-communication medium 231. The protocol stacks such as BLE, 4G\5G, WIFI, 485 bus, zigbee or GPS communicate directly with the bus. Such a system will be relatively simple and low-cost, and is mainly used in some mid- and low-end power-assisted electric vehicle systems.

[0062] exist Figure 4 In the example, there are both a main communication medium 241 and a sub-communication medium 231. The BLE, 4G\5G protocol stacks communicate with the main communication medium through the master node device, and the remaining protocol stacks communicate directly with the main communication medium. Figure 2 The difference is that some sub-communication medium devices are directly connected to the main communication medium, and do not need to be transferred through the main node device inherent in the power-assisted electric vehicle system as a channel; in this system, additional equipment is required, which will increase the system cost, but the efficiency and stability of data interaction are higher, and at the same time, it does not affect the execution efficiency of the system's inherent main node device.

[0063] pass Figure 2 – 4 shows that the communication network configuration of the power-assisted electric vehicle system is diversified and there will be various combinations, which requires the communication program module to be compatible with more devices, making the communication program module very complicated. If there is no good software architecture to subdivide the complex things, the program will become bloated, poor in scalability, and prone to problems. Figure 1 In the hierarchical diagram of the communication program module, the communication program module is divided into levels, making the program clear and concise, easier to implement, and highly extensible.

[0064] It should be noted that, although the solution of the present invention is applied to the communication system of an electric power-assisted vehicle in the above examples, those skilled in the art can determine that the solution of the present invention can be applied to all electronic products that use communication media.

[0065] The function module sublayer 24 is used to store commonly used communication function modules. The main communication medium is set in the function module sublayer. The function module sublayer 24 depends on the working mode sublayer and the communication medium sublayer. The logic of the three sublayers will be greatly intertwined, generating a large amount of shared code, which needs to be processed through logical operations between pre-compiled instruction switches.

[0066] The common sublayer 25 includes common programs and common data. The common programs and common data in the common sublayer 25 can be called by the working mode sublayer 22, the communication medium sublayer 23 and the functional module sublayer 24. The common codes included in the common sublayer 25 include but are not limited to FIFO logic related programs and CRC verification programs.

[0067] The configuration file 21 stores a pre-compilation instruction switch. Each common function in the functional module sublayer 24 has a corresponding pre-compilation instruction switch in the configuration file 21. The developer can turn on or off the corresponding pre-compilation instruction and enable or disable the corresponding function according to product requirements before compiling.

[0068] When the software architecture is established and the layers are sorted out, the programs at each level should be able to be compiled independently and be self-contained, establishing connections with other layers through data input and data output interfaces to form data flows and constitute a fully functional embedded software product.

[0069] The following describes the pre-compilation management method of the communication program module 2 of the present invention, which includes:

[0070] When writing a program, the communication medium of the communication device is first determined, and the pre-compiled instructions corresponding to the determined communication medium are opened through the configuration file 21 in the communication program module 2 to enable the functional module corresponding to the communication medium. The communication medium includes the main communication medium, which is the CAN bus.

[0071] Determine the working mode of the communication device, which includes the master node mode and the slave node mode. If the communication device is a master node device, the precompiled instructions corresponding to the master node mode are opened through the configuration file in the communication program module; if the communication device is a slave node device, the configuration file in the communication program module opens the precompiled instructions corresponding to the slave node mode; the same communication device can be set to the master node mode and the slave node mode at the same time;

[0072] Determine the functional modules of the communication device, and enable the required functional modules by opening the pre-compiled instructions corresponding to the required functional modules through the configuration files in the communication program module; each functional module is independent and does not interfere with each other.

[0073] By performing the above-mentioned management on the communication program module, the communication program module can be set to the required working mode, the required communication medium can be selected, the functional modules to be used can be determined, all the functions to be used in the communication program module can be enabled, and all the functions that are not needed can be disabled. After the communication program module is compiled, the generated program can fully meet the user's needs and has no redundant parts, which can avoid the bloat of the program and improve the operating efficiency of the communication program module.

[0074] The above-mentioned communication program module is the management center of the communication network data. The driver layer in the communication network obtains data from the communication medium in the network and uploads the data to the communication module program. The communication module program parses and processes the data according to the protocol corresponding to the communication medium, and then uploads it to the upper layer for processing; the upper layer program transmits the data to be sent to the communication module program. The communication module program packages the data according to the protocol corresponding to the communication medium, and then transmits it to the driver layer, which sends it to the communication network.

[0075] The communication program module is provided with a configuration file, which is a header file used to configure the pre-compiled instruction switch. The user can configure the header file according to the requirements to open or close the corresponding functional module in the communication program module.

[0076] After configuring the header file of the pre-compilation instruction switch, the above-mentioned communication program module can compile and generate a lib file. Other developers only need to load the lib file into their own program project file and compile it. Before compiling, use the pre-compilation instruction in the configuration header file in the communication program module to open or close the corresponding functional module.

[0077] According to user needs, the above-mentioned communication medium may also include a sub-communication medium. The communication device on the sub-communication medium can be set to master node mode, slave node mode, or both master node mode and slave node mode; the master node device on the sub-communication medium needs to turn on the master node mode pre-compilation instruction switch before compilation. At this time, the master node device can actively request and read the data of any device on the main communication medium according to demand, and can also actively request to write data to any device on the main communication medium; the slave node device on the sub-communication medium needs to turn on the slave node mode pre-compilation instruction switch before compilation. At this time, the slave node device can only passively obtain the data of the device on the main communication medium; the same communication device on the sub-communication medium can actively request and passively obtain in the case of both master and slave node modes, and needs to be turned on at the same time before compilation. Turn on the pre-compiled instruction switch for the master node mode and the slave node mode; when the device on the sub-communication medium is in the master node mode, the device on the sub-communication medium actively sends a request message to the master node device on the main communication medium. When the master node device turns on the pre-compiled instruction switch corresponding to the sub-communication medium in the configuration file, it will process the received data. The master node device determines whether the message is a read message, a write message or a broadcast message based on the message type, and determines whether the message is only for itself or needs to be transmitted to other devices through the main communication medium. After determining, it makes appropriate processing; the master node device receives messages from other devices on the main communication medium, determines whether they need to be transmitted to the device on the sub-communication medium, and if necessary, packages the data according to the sub-communication medium protocol and sends it to the sub-communication medium device.

[0078] The functional modules of the above communication equipment include but are not limited to single-frame message writing, single-frame message reading, multi-frame message writing, multi-frame message reading, write timeout retransmission, read timeout termination, broadcast message and termination message; each functional module is independent of each other, and each functional module corresponds to a pre-compiled instruction switch in the configuration file. Developers can enable or disable the corresponding function by turning on or off the corresponding pre-compiled instruction according to product requirements. The functional module of the communication equipment adopts a single-frame message if the data content of the message is less than or equal to 8 bytes; if the data content of the message is greater than 8 bytes, a multi-frame message is adopted; in the configuration file of the communication program module, turning on the corresponding pre-compiled instruction switch will enable the functional module, otherwise, the functional module will be disabled; a single-frame message only needs one response and one answer to end; a multi-frame message needs to be divided into multiple frames according to the data length, and each frame message needs one response and one answer, and it can end only after receiving the last frame message.

[0079] For the master node device 26 or the slave node device 27, a read timeout error function module is provided on the basis of the message reading function module; in the configuration file of the communication program module, the pre-compiled instruction switch of the read timeout error function is turned on or off, so that the read timeout error function can be enabled or disabled; after the read timeout error function is enabled, after the master node device actively initiates a read message request, if no response message is received from the slave node device within the specified time, a read timeout error will be triggered, and the master node device will send an error termination message to the slave node device; in a multi-frame read message, if the master node sends a read timeout error termination message, it means that this multi-frame message sequence ends prematurely.

[0080] For the master node device 26 or the slave node device 27, in addition to the message writing function module, there is also a write timeout retransmission function module; in the configuration file 21 of the communication program module 2, the pre-compiled instruction switch of the write timeout retransmission function is turned on or off, then the write timeout retransmission function can be enabled or disabled; after the write timeout retransmission function is enabled, after the master node device actively initiates a write request message, if no response message is received from the slave node device within the specified time, the frame message will be retransmitted; if no response message is received from the slave node device within the specified number of retransmissions, an error will be reported and the multi-frame message will be terminated; the number of retransmissions and the timeout retransmission time can be configured in the configuration header file according to requirements, the default number of retransmissions is 3 times, and the default retransmission time is 100ms.

[0081] The master node device 26 will actively initiate a write request command message to the slave node device 27. After receiving the write request command, the slave node device 27 needs to respond in accordance with the protocol format. In a system containing a sub-communication medium 231, the device on the sub-communication medium 231 can initiate a write request command message to the master node device 26 on the main communication medium 241, or send a write request command message to other devices on the main communication medium 241 through the master node device 26 on the main communication medium 241. After receiving the write request command message initiated by the sub-communication medium 231 device, the master node device 26 on the main communication medium 241 parses it according to the protocol to determine whether the command is for itself or for other devices. If it is a command for itself, it responds according to the command requirements. If it is a command for other devices, the message is packaged according to the CAN protocol format and transmitted to the corresponding device through the CAN bus. After receiving the command, the corresponding device responds according to the command requirements. After receiving the response from the corresponding device, the master node device packages it according to the protocol format of the sub-communication medium device and then transmits it to the sub-communication medium device.

[0082] Both the master node device 26 and the slave node device 27 can actively send broadcast messages; broadcast messages are sent periodically; the data of each frame of broadcast message is 0-8 bytes; broadcast messages do not require a response.

[0083] The communication program module 2 includes a 1ms mode and a non-1ms mode. Different types of devices in the system have different requirements for program execution efficiency and execution time. In order to be compatible with more devices, a pre-compiled instruction switch for the 1ms mode is added. When the pre-compiled instruction switch for the 1ms mode is turned on, the communication program module needs to work in a 1ms environment, and the main function interface program of the communication program module needs to be called once every 1ms. At this time, the communication program module can only process no more than one frame of messages in 1ms. The execution time of the main function of the communication program module is strictly controlled and cannot affect the operation of other module programs, let alone the accuracy of the 1ms time. When the pre-compiled instruction switch for the 1ms mode is turned off, the communication program module works in a non-1ms environment, and the main function interface program of the communication program module can set the calling time according to the developer's needs, and the calling time is greater than 1ms. Each time the main function is called, all received messages in the FIFO file and the broadcast messages that need to be processed will be processed. If it is a master node device, all messages that the upper layer wants to send must be processed.

[0084] The communication program module 2 includes the BUSOFF function module of the CAN bus communication; turning on the pre-compiled instruction switch of the BUSOFF module can enable the function; otherwise, the function is disabled; BUSOFF is used to handle the problem when an error occurs on the CAN bus in the system; CAN bus errors include but are not limited to short circuit, open circuit, reverse connection or interference; BUSOFF will reinitialize the CAN bus within a certain period of time after the CAN bus error occurs, depending on the situation.

[0085] The following uses the communication program module as an example to simulate a simple data flow example to establish the concept of the relationship between the various levels. The signal on the bus is converted into input message data at the driver layer. The driver layer passes the input message data to the communication program module. After parsing the input message data, the communication program module can transmit the data to the upper layer as needed, or directly respond and send the response data to the driver layer, which is converted into a bus signal and transmitted out. If the upper layer has data to send, it passes the data to the communication program module. After the communication program module packages the data according to the protocol, it passes the data to the driver layer, and the driver layer converts the data into a bus signal and transmits it out.

[0086] like Figure 6 The figure shows an example of communication between various levels of a communication program module. The working modes of the communication program module are 1ms working mode and slave node mode. The communication program module is a simple example with BLE sub-communication medium, general protocol function module and public layer. To a certain extent, it can reflect the master-slave calling relationship between the levels. The specific process is as follows:

[0087] In S1, 1ms mode, the main function of the working mode will be called and executed cyclically with a period of 1ms; when executing, it will first poll the BLE receive message buffer area of ​​the public layer to determine whether there are any unprocessed BLE messages; if so, the BLE message data will be passed to the BLE protocol stack of the protocol stack sublayer for parsing and processing; if not, it will jump directly to S5 for processing.

[0088] S2. In the BLE protocol stack, the BLE message data is parsed according to the BLE message format.

[0089] S3. Determine whether the parsed BLE message data is CAN message data. If not, directly process it according to the protocol and return the result to the working mode sublayer.

[0090] S4. If it is CAN message data, the data is packaged and returned to the working mode sublayer.

[0091] S5. Determine whether the protocol stack sublayer has message data that needs to be transferred to the functional module sublayer. If not, jump to S6; if yes, transfer the data to the functional module sublayer and execute S7.

[0092] S6, poll the CAN message receiving buffer area of ​​the public layer to determine whether there is any unprocessed CAN message data. If so, after obtaining the data, pass the data to the functional module sublayer, that is, jump to S7. If not, execute S11.

[0093] S7. Obtain the data transmitted by the working mode sublayer, parse it according to the CAN protocol format, and identify the message type through CANID, whether it is a broadcast frame, a published message, an EMCY fault message, or a point-to-point message.

[0094] S8, get the source device type from CANID, and determine whether the message is sent by the BLE device. If yes, jump to S9; if no, jump to S10.

[0095] S9. Determine whether the BLE message is a message sent to this device. If it is a message sent to this device, determine whether to respond according to the protocol requirements; if a response is to be made, package the response data and pass it to the working mode sublayer; if it is not a message sent to this device, but a message to other slave node devices, package the CAN message and write it into the CAN send message temporary storage area of ​​the public layer, waiting to be sent to the CAN network.

[0096] S10. If the message is a CAN message, determine whether to respond according to the CAN protocol requirements. If a response is to be made, package the response data and write it into the CAN send message temporary storage area of ​​the public layer, waiting to be sent to the CAN network.

[0097] S11. Determine whether the functional module sublayer has data to be passed to the BLE protocol stack in the sub-communication medium. If so, pass the data and jump to S12; if not, jump to S13.

[0098] S12, the CAN message data to be sent to the BLE device is packaged according to the BLE protocol format, and then written into the BLE message sending buffer of the public layer, waiting for sending; after the execution is completed, jump to S17.

[0099] S13, determine whether there is data waiting to be resent, if yes, jump to S14, if not, jump to S15.

[0100] S14. Determine whether the retransmission time of the message waiting to be retransmitted has arrived. If so, determine whether the number of retransmissions has reached the specified number. If not, retransmit the message; if reached, send a termination message.

[0101] S15, determine whether there is data waiting to be received, if yes, jump to S16, if not, jump to S17.

[0102] S16: Determine whether the data waiting to be received is received on time. If it is timed out, report an error.

[0103] S17, obtain the fault register data of the CAN module from the driver layer, and determine whether the CAN bus of the device is offline. If it is offline, jump to the CAN bus offline processing logic; if it is not offline, jump to S19.

[0104] S18, CAN bus offline processing logic, reinitializes the CAN module according to the logic to make the CAN bus module work again.

[0105] S19. By parsing the ID of the received CAN message, determine whether the messages of other node devices on the CAN network are received on time. If the message of the node device is not received within the specified time, it is determined that the node device has been offline and jump to S20.

[0106] S20. According to the requirements, when a node device is offline, different processing measures are taken according to the importance of the device.

[0107] like Figure 7 The figure shows another example of communication between the various levels of a communication program module. The working modes of the communication program module are 1ms working mode and master node mode. The communication program module is a simple example with a BLE sub-communication medium, a general protocol function module and a public layer. To a certain extent, it can reflect the master-slave calling relationship between the levels. The specific process is as follows:

[0108] In S1, 1ms mode, the main function of the working mode will be called and executed cyclically with a period of 1ms; during execution, the BLE receive message buffer area of ​​the public layer will be polled first to determine whether there are any unprocessed BLE messages; if so, the BLE message data will be passed to the BLE protocol stack of the protocol stack sublayer for parsing and processing; if not, it will jump directly to S5 for processing.

[0109] S2. In the BLE protocol stack, the BLE message data is parsed according to the BLE message format.

[0110] S3. Determine whether the parsed BLE message data is CAN message data. If not, directly process it according to the protocol and return the result to the working mode sublayer.

[0111] S4. If it is CAN message data, the data is packaged and returned to the working mode sublayer.

[0112] S5. Determine whether the protocol stack sublayer has message data that needs to be transferred to the functional module sublayer. If not, jump to S6; if yes, transfer the data to the functional module sublayer and execute S7.

[0113] S6, polling the CAN message receiving buffer area of ​​the public layer to determine whether there is any unprocessed CAN message data. If so, after obtaining the data, pass the data to the functional module sublayer, that is, jump to S7. If not, execute S11.

[0114] S7. Obtain the data transmitted by the working mode sublayer, parse it according to the CAN protocol format, and identify the message type through CANID, whether it is a broadcast frame, a published message, an EMCY fault message, or a point-to-point message.

[0115] S8, get the source device type from CANID, and determine whether the message is sent by the BLE device. If yes, jump to S9; if no, jump to S10.

[0116] S9. Determine whether the BLE message is sent to this device. If so, determine whether to respond according to the protocol requirements; if so, package the response data and pass it to the working mode sublayer. If it is not sent to this device, but to other slave node devices, package the CAN message and write it to the CAN send message temporary storage area of ​​the public layer, waiting to be sent to the CAN network.

[0117] S10. If the message is a CAN message, determine whether to respond according to the CAN protocol requirements. If a response is to be made, package the response data and write it into the CAN send message buffer area of ​​the public layer, waiting to be sent to the CAN network.

[0118] S11. Determine whether the functional module sublayer has data to be passed to the BLE protocol stack in the sub-communication medium. If so, pass the data and jump to S12; if not, jump to S13.

[0119] S12, the CAN message data to be sent to the BLE device is packaged according to the BLE protocol format, and then written into the BLE message sending buffer of the public layer, waiting for sending; after the execution is completed, jump to S19.

[0120] S13, judging whether the event code input by the communication module is a valid value, if yes, jumping to S14, if not, jumping to S15.

[0121] S14. According to the input event code, the target device ID, main index and sub-index, data length and data of the CAN message are obtained, and then these data are packaged into CAN message data, stored in the public layer CAN sending buffer area, and wait for sending.

[0122] S15, determine whether there is data waiting to be resent, if yes, jump to S16, if not, jump to S17.

[0123] S16. Determine whether the retransmission time of the message waiting to be retransmitted has arrived. If so, determine whether the number of retransmissions has reached the specified number. If not, retransmit the message; if reached, send a termination message.

[0124] S17, determine whether there is data waiting to be received, if yes, jump to S18, if not, jump to S19.

[0125] S18: Determine whether the data waiting to be received is received on time. If it is timed out, report an error.

[0126] S19, obtain the fault register data of the CAN module from the driver layer, and determine whether the CAN bus of the device is offline. If it is offline, jump to the CAN bus offline processing logic; if it is not offline, jump to S21.

[0127] S20, CAN bus offline processing logic, reinitialize the CAN module according to the logic, so that the CAN bus module can work again.

[0128] S21. By parsing the ID of the received CAN message, determine whether the messages of other node devices on the CAN network are received on time. If the message of the node device is not received within the specified time, it is determined that the node device has been offline and jump to S22.

[0129] S22. According to the requirements, when a node device goes offline, different processing measures are taken according to the importance of the device.

[0130] The above two examples only describe one way of combining communication program modules. In actual applications, different working modes, different protocol stacks, and different functional modules are selected. The types of general module programs that can be combined and matched increase in number and may exceed a hundred, so they are not listed here one by one.

[0131] Figure 8 This is an example of further segmentation of each level of the communication module program. After segmentation, independent sub-modules are formed within each level.

[0132] As shown in the figure, the working mode layer has two independent sub-modules, 1ms mode and master-slave node mode;

[0133] The sub-communication network protocol stack layer is divided into CAN protocol stack sub-module, WIFI protocol stack sub-module, 4G\5G protocol stack sub-module, 485 protocol stack sub-module, BLE protocol stack sub-module, etc. The figure is only an example, which can be added and expanded according to actual needs, and unnecessary sub-modules can also be removed;

[0134] The general function module layer is the commonly used functions in the communication protocol. The example includes sub-modules such as single-frame writing, single-frame reading, multi-frame writing, multi-frame reading, broadcast message, write timeout retransmission, read timeout abort, abort message, etc. Function modules can be added and removed according to actual needs;

[0135] The common layer usually contains some sub-modules that may be used by the other three layers. The example includes FIFO sub-module, mathematical algorithm sub-module MATH, etc.

[0136] After the sub-modules are divided, we define a pre-compilation directive in the configuration file for each sub-module. The programs related to the sub-modules are encapsulated with pre-compilation directives. During compilation, the relevant modules can be opened and closed through the pre-compilation directives in the C language.

[0137] In the configuration file, each submodule has a corresponding macro definition. The macro definition can be declared as a numeric constant. The simplest declaration array is 0 and 1. In complex applications, it can be declared as more numbers. After the macro definition is completed, the precompiled instructions #if, #else, #elif, and #endif can be used to encapsulate the submodule-related programs.

[0138] Take a simple example: In the configuration file, the macro definition of the 1ms mode submodule is: #define COM_1MS_MODE 1;

[0139] In the code, assume that the main function of the 1ms mode submodule is void gCOM_Task1ms_Mode_vd(void){…};

[0140] We can do this:

[0141] #if (COM_1MS_MODE == 1)

[0142] void gCOM_Task1ms_Mode_vd(void)

[0143] {

[0144] … …;

[0145] }

[0146] #endif / * COM_1MS_MODE * /

[0147] With this setting, the main function of the 1ms submodule is encapsulated by the precompiled instructions. When COM_1MS_MODE is declared as 1, the function will generate machine code when the compiler compiles; when COM_1MS_MODE is declared as 0, the compiler will ignore this function.

[0148] By expanding the above example, the code of the entire sub-module can be encapsulated. When the pre-compilation instruction switch is 1, the code of the sub-module will be compiled; when the pre-compilation instruction switch is 0, it will not be compiled.

[0149] The codes in the program are inevitably interleaved, that is, two or more sub-modules share a piece of code. At this time, the logical operations of the C language pre-compiled instructions must be used to handle it. The simplest and most commonly used logical operations are AND, OR, and ==. When a piece of code shares more sub-modules, the logical operations are more complex. Depending on the situation, more complex logical operations such as XOR, addition, subtraction, and shift can be used.

[0150] Let’s take a simple example: in the configuration file, the macro definition of the 1ms mode submodule is: #define COM_1MS_MODE 1; the macro definition of the master node mode submodule is: #define COM_MASTERNODE_MODE 1.

[0151] In the code, the function: void COM_Protocol_Ack_Task(void) can be used in both modes. At this time, we can encapsulate the function with the OR logical operator;

[0152] #if ((COM_1MS_MODE == 1) || (COM_MASTERNODE_MODE == 1))

[0153] void COM_Protocol_Ack_Task(void)

[0154] {

[0155] … …;

[0156] }

[0157] #endif

[0158] With this setting, the function will be compiled when the compiler is compiled regardless of whether it is in 1ms mode or master node mode.

[0159] The above examples are only for explaining the pre-compilation setting methods. These setting methods are not the only setting methods. Programmers can modify the settings as needed.

[0160] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.

Claims

1. A communication program module, It is characterized in that It includes configuration files, working mode sublayer, function module sublayer and common sublayer; The working mode sublayer is used to select and set the working mode of the communication program module. The working mode sublayer is provided with commonly used program working modes; The function module sublayer is used to store commonly used communication function modules, and the function module sublayer is provided with a main communication medium; The common sublayer contains common programs and common data, which can be called by the working mode sublayer and the functional module sublayer; The configuration file stores the pre-compilation instruction switch. Each common function in the functional module sublayer has a corresponding pre-compilation instruction switch in the configuration file. Before compiling, the developer can turn on or off the corresponding pre-compilation instruction and enable or disable the corresponding function according to product requirements. It also includes a communication medium sublayer, which includes a sub-communication medium. The sub-communication medium includes one or more of BLE, 4G\5G, WIFI, 485 bus, zigbee or GPS. The communication medium sublayer contains protocol stack codes of all sub-communication media, which are used to parse and package the received and sent data of the sub-communication medium according to the protocol stack; the communication medium sublayer is used to connect more than two communication media, and the working mode of the communication medium sublayer depends on the working mode sublayer. The sub-communication medium depends on the main communication medium and can only form a communication network with the main communication medium; The protocol stacks of the sub-communication media must be independent of each other. If there is common code, it needs to be processed through logical operations between pre-compiled instruction switches in the configuration file.

2. The communication program module according to claim 1, It is characterized in that The main communication medium in the functional module sublayer is the CAN bus.

3. The communication program module according to claim 1, It is characterized in that Each of the sub-communication media has a corresponding pre-compilation instruction switch in the configuration file. When the pre-compilation instruction switch is turned on, the corresponding module will be compiled during compilation. When the pre-compilation instruction switch is turned off, the corresponding module will not be compiled during compilation.

4. The communication program module according to claim 1, It is characterized in that The sub-communication medium of the communication medium sub-layer can be expanded and upgraded to add a new protocol stack.

5. The communication program module according to claim 1, It is characterized in that The communication devices connected to the communication medium sublayer include master node devices and slave node devices; Both the master node device and the slave node device can actively send data to the main communication medium; The master node device has the authority to actively request the slave node device to read and write data. In addition to broadcasting messages, the slave node device cannot actively send messages to the communication network, and the slave node device must respond after receiving the request from the master node device.

6. The communication program module according to claim 1, It is characterized in that When there is a sub-communication medium in the communication network, the master node device and the slave node device can both be used to act as data transfer channels between the sub-communication medium and the master communication medium. The configuration file of the node device serving as the data transfer channel contains a pre-compiled instruction switch corresponding to each sub-communication medium. Before compiling, turning on the pre-compiled instruction switch corresponding to the sub-communication medium will enable the node device to process the functional module of the sub-communication medium and the message on the sub-communication medium.

7. The communication program module according to claim 6, It is characterized in that The master node device can be set in the sub-communication medium. The master node device on the sub-communication medium needs to turn on the master node mode pre-compilation instruction switch before compilation. At this time, the master node device can actively request and read data from any device on the main communication medium according to demand, and can also actively request to write data to any device on the main communication medium. In a communication network, one or more master node devices are set up; the master node device can actively request to read and write data from the slave node device, and can also be requested to read and write data by other master node devices.

8. The communication program module according to claim 1, It is characterized in that The working modes in the working mode sublayer include 1ms mode and non-1ms mode; In 1ms mode, the main function of the communication program module will be called once every 1ms. Each time it is called, only one frame of message will be processed, and this frame of message is required to be processed as soon as possible; In non-1ms mode, the calling cycle of the main function of the communication program module program is greater than 1ms; In 1ms mode, the communication program module messages include dynamic messages and static messages. The execution time of dynamic messages is required to be no more than 12us; the execution time of static messages is required to be no more than 25us.

9. The communication program module according to claim 1, It is characterized in that The functional modules of the functional module sublayer include single-frame message writing, single-frame message reading, multi-frame message writing, multi-frame message reading, write timeout retransmission, read timeout termination, broadcast message and termination message; each functional module is independent of each other, and each of the above-mentioned functional modules corresponds to a pre-compiled instruction switch in the configuration file. Developers can enable or disable the corresponding function by turning on or off the corresponding pre-compiled instruction according to product requirements.

10. The communication program module according to claim 1, It is characterized in that The common codes contained in the common sublayer include FIFO logic related programs and CRC check programs, and the common data in the common sublayer include CAN message sending and receiving buffer areas, and sub-communication medium message sending and receiving buffer areas.

11. Application of the communication program module according to any one of claims 1 to 10, It is characterized in that The communication program module is used to assist electric bicycles. The main communication medium of the communication program module is used to connect the electronic equipment of the assist electric bicycle, and the sub-communication medium is connected to the communication equipment, which includes mobile phones, computers, and pad devices. The communication equipment can obtain data of the electronic equipment on the main communication medium in real time, and can configure the parameters of the electronic equipment according to needs, control the electronic equipment, and upgrade or maintain the program of the electronic equipment.

12. A method for pre-compiling and managing communication program modules, It is characterized in that The method comprises: When writing a program, the communication medium of the communication device is first determined, and the pre-compiled instructions corresponding to the determined communication medium are opened through the configuration file in the communication program module to enable the functional module corresponding to the communication medium. The communication medium includes the main communication medium, which is the CAN bus. Determine the working mode of the communication device, which includes the master node mode and the slave node mode. If the communication device is a master node device, the precompiled instructions corresponding to the master node mode are opened through the configuration file in the communication program module; if the communication device is a slave node device, the configuration file in the communication program module opens the precompiled instructions corresponding to the slave node mode; the same communication device can be set to the master node mode and the slave node mode at the same time; Determine the functional modules of the communication device, and enable the required functional modules by opening the pre-compiled instructions corresponding to the required functional modules through the configuration files in the communication program module; each functional module is independent and does not interfere with each other.

13. The communication program module pre-compilation management method according to claim 12, It is characterized in that The communication program module is the management center of the communication network data. The driver layer in the communication network obtains data from the communication medium in the network and uploads the data to the communication module program. The communication module program parses and processes the data according to the protocol corresponding to the communication medium and then uploads it to the upper layer for processing; The upper-layer program transmits the data to be sent to the communication module program, which packages the data according to the protocol corresponding to the communication medium and transmits it to the driver layer, which then sends it to the communication network.

14. The communication program module pre-compilation management method according to claim 12, It is characterized in that The communication program module is provided with a configuration file, which is a header file used to configure the pre-compiled instruction switch. The user can configure the header file according to the requirements to open or close the corresponding functional module in the communication program module.

15. The communication program module pre-compilation management method according to claim 12, It is characterized in that After configuring the header file of the pre-compilation instruction switch, the communication program module can compile and generate a lib file. Other developers only need to load the lib file into their own program project files and compile it. Before compiling, use the pre-compilation instruction in the configuration header file in the communication program module to open or close the corresponding functional module.

16. The communication program module pre-compilation management method according to claim 12, It is characterized in that The communication medium also includes a sub-communication medium, and the communication device on the sub-communication medium can be set to a master node mode or a slave node mode or both a master node mode and a slave node mode; The master node device on the sub-communication medium needs to turn on the master node mode pre-compilation instruction switch before compilation. At this time, the master node device can actively request and read data from any device on the main communication medium according to demand, and can also actively request to write data to any device on the main communication medium; The slave node device on the slave communication medium needs to turn on the slave node mode pre-compilation instruction switch before compilation. At this time, the slave node device can only passively obtain the data of the device on the main communication medium; The same communication device on the sub-communication medium can actively request or passively obtain in the case of both master and slave node modes. It is necessary to turn on the master node mode and slave node mode pre-compilation instruction switches at the same time before compilation; When the device on the sub-communication medium is in the master node mode, the device on the sub-communication medium actively sends a request message to the master node device on the main communication medium. When the master node device turns on the pre-compiled instruction switch corresponding to the sub-communication medium in the configuration file, it will process the received data. The master node device determines whether the message is a read message, a write message, or a broadcast message according to the message type, and determines whether the message is only for itself or needs to be transmitted to other devices through the main communication medium. After the determination, it makes appropriate processing; The master node device receives messages from other devices on the main communication medium and determines whether they need to be transmitted to the device on the sub-communication medium. If necessary, the data is packaged according to the sub-communication medium protocol and sent to the sub-communication medium device.

17. The communication program module pre-compilation management method according to claim 12, It is characterized in that The functional modules of the communication equipment include single-frame message writing, single-frame message reading, multi-frame message writing, multi-frame message reading, write timeout retransmission, read timeout termination, broadcast message and termination message; each functional module is independent of each other, and each of the above-mentioned functional modules corresponds to a pre-compiled instruction switch in the configuration file. Developers can enable or disable the corresponding function by turning on or off the corresponding pre-compiled instruction according to product requirements.

18. The communication program module pre-compilation management method according to claim 17, It is characterized in that In the functional module of the communication device, if the data content of the message is less than or equal to 8 bytes, a single-frame message is used; if the data content of the message is greater than 8 bytes, a multi-frame message is used; in the configuration file of the communication program module, turning on the corresponding pre-compiled instruction switch will enable the functional module, otherwise, the functional module will be disabled; a single-frame message only needs one response and one answer to end; a multi-frame message needs to be divided into multiple frames according to the data length, and each frame of the message needs one response and one answer, and it can only end after the last frame of the message is received.

19. The communication program module pre-compilation management method according to claim 17, It is characterized in that For the master node device or the slave node device, in addition to the message reading function module, a read timeout error reporting function module is also provided; In the configuration file of the communication program module, turn on or off the pre-compiled instruction switch of the read timeout error function, which can enable or disable the read timeout error function; After the read timeout error function is enabled, after the master node device actively initiates a read message request, if it does not receive a response message from the slave node device within the specified time, a read timeout error will be triggered, and the master node device will send an error abort message to the slave node device; In a multi-frame read message, if the master node sends a read timeout error abort message, it means that this multi-frame message sequence ends prematurely.

20. The communication program module pre-compilation management method according to claim 17, It is characterized in that For the master node device or the slave node device, in addition to the message writing function module, there is also a write timeout resend function module; In the configuration file of the communication program module, turn on or off the pre-compiled instruction switch of the write timeout resend function, and then you can enable or disable the write timeout resend function; After the write timeout retransmission function is enabled, after the master node device actively initiates a write request message, if it does not receive a response message from the slave node device within the specified time, it will resend this frame message; If no response message is received from the slave node device within the specified number of retransmissions, an error will be reported and the multi-frame message will be terminated; The number of retransmissions and timeout retransmission time can be configured in the configuration header file according to requirements.

21. The communication program module pre-compilation management method according to claim 17, It is characterized in that The master node device will actively send a write request command message to the slave node device. After receiving the write request command, the slave node device needs to respond according to the protocol format; In a system with a sub-communication medium, a device on the sub-communication medium can initiate a write request command message to a master node device on the main communication medium, or send a write request command message to other devices on the main communication medium through the master node device on the main communication medium; After receiving the write request command message initiated by the sub-communication medium device, the master node device on the main communication medium parses it according to the protocol to determine whether the command is for itself or for other devices; If it is an order given to yourself, respond according to the order requirements; If it is a command to other devices, the message will be packaged according to the CAN protocol format and transmitted to the corresponding device through the CAN bus; After receiving the command, the corresponding device responds according to the command requirements; After receiving the response from the corresponding device, the master node device packages it according to the protocol format of the sub-communication medium device and then transmits it to the sub-communication medium device.

22. The communication program module pre-compilation management method according to claim 17, It is characterized in that Both the master node device and the slave node device can actively send broadcast messages; broadcast messages are sent periodically; the data of each frame of broadcast message is 0-8 bytes; broadcast messages do not require a response.

23. The communication program module pre-compilation management method according to claim 12, It is characterized in that The communication program module includes a 1ms mode and a non-1ms mode; Different types of devices in the system have different requirements for program execution efficiency and execution time. In order to be compatible with more devices, a 1ms mode pre-compilation instruction switch is added; If the pre-compiled instruction switch of 1ms mode is turned on, the communication program module needs to work in a 1ms environment, and the main function interface program of the communication program module needs to be called once every 1ms. At this time, the communication program module can only process no more than one frame of message in 1ms; The execution time of the main function of the communication program module is strictly controlled and cannot affect the operation of other module programs, let alone the accuracy of 1ms time; If the pre-compilation instruction switch of 1ms mode is turned off, the communication program module works in a non-1ms environment. The main function interface program of the communication program module can set the calling time according to the developer's needs, and the calling time is greater than 1ms. Each time the main function is called, all received messages in the FIFO file and the broadcast messages that need to be processed will be processed. If it is a master node device, all messages that the upper layer wants to send must also be processed.

24. The communication program module pre-compilation management method according to claim 12, It is characterized in that The communication program module includes a BUSOFF function module for CAN bus communication; Turning on the precompiled instruction switch of the BUSOFF module can enable this function; otherwise, it will disable this function; BUSOFF is used to handle the problem when an error occurs on the CAN bus in the system; CAN bus errors include short circuit, open circuit, reverse connection or interference; BUSOFF will reinitialize the CAN bus within a certain period of time after an error occurs on the CAN bus, depending on the situation.