The package contains two classes: the StateMachine class and the HFLink class.

The StateMachine class defines an HFMessage structure that describes the information of a packet: including sender id, receiver id, useful data length and data content storage array (with a maximum capacity of 120 bytes).

The most important aspects of the StateMachine class are the StateMachine receive functions StateMachine::receiveStates and StateMachine::sendMessage functions.

  • StateMachine::receiveStates(const unsigned char rx_data).

    This function will be called recursively in the program, and each time it is called, the latest byte of data taken from the serial port receive message queue will be transferred inside the function. Each time as long as the incoming data meets the requirements of the current state, the state variable will automatically change to the next state. When the program has successfully traversed all the states once, it signals the receipt of a complete available packet, at which time the function returns 1 (otherwise it returns 0), prompting the data analysis function byteAnalysisCall in hf_link.cpp to start the data analysis.

  • StateMachine::sendMessage(constHFMessage* txmessage).

    In contrast to the state machine receive function, this function is a packetized send of the data we need to upload to ensure that it can pass the state machine receive function of the upper machine. The input parameter of this function is a pointer to the HFMessage type of the data packet to be sent.

The HFLink class inherits from the StateMachine class in order to use the state machine receive function and the packet send function from the StateMachine class.

Also, in the HFLink class, we define Command enumeration variables, which are all command types, and when parsing the packets, we need to do different processing according to the different command types.

Besides, because the PC side and the master side share the hflink code, we use the char type variable hf_link_node_model to distinguish the upper and lower machines (0 slave , 1 master).

Next, we will explain the implementation of the functions in HFLink in detail. Without posting the code here, you can open the file hf_link.cpp and check it.

  • HFLink::byteAnalysisCall(const unsigned char rx_byte).

    This function will be called continuously in while(1). Once the first if judgment statement is correct, it signals that the state machine has received a complete packet. Then, HFLink::packageAnalysis will be called for data processing.

  • HFLink::packageAnalysis(void).

    A handshake between the upper and lower unit is required before the first communication, similar to a communication attempt, to tell the other unit that it is going to start communicating next. The lower machine calls this function sendStruct(SHAKING_HANDS , NULL , 0); send a handshake request to the upper machine, the upper machine will give a promise down after receiving the handshake request, the lower machine will shaking_hands_state flag position 1 after receiving the reply. handshake is successful.

    Next, start communication.

    First, the type of command is determined from the first data of the packet obtained in the state machine:.

    commandstate = (Command)rx_message.data[0];

    Then, using switch statements, different data processing functions are called for different command types.

    There are two main data processing functions, setCommandAnalysis and readCommandAnalysis.

  • setCommandAnalysis.

    Assignment instruction analysis function. It assigns the data sent by the upper computer for setting some parameters or variables of the lower computer to the corresponding variables or parameters of the lower computer through the memcpy function.

  • readCommandAnalysis.

    Information request command analysis function. After receiving the data request message from the upper machine, it will split some variables or parameters of the lower machine into multiple bytes of data by the memcpy function and send them to the upper machine in a package.

  • HFLink::sendStruct(const Command command_type , unsigned char* p , unsigned short intlen).

    Message sending function. The incoming parameters include: command type, a pointer to the first address of the sent content and the length of the sent data. The function is mainly filled with information about the HFMessage type data internally.

How to customize communication instructions

First, define the role of the custom communication instruction, the data involved, and give your own name to this communication instruction according to its role.

Then, in hf_link.h, add the name of this communication instruction before LAST_COMMAND_FLAG.

After that, find the HFLink::packageAnalysis(void) function in the hf_link.cpp file, and add the new communication instruction parsing content in the switch function, following the format of the previous communication. For both the upper and lower machines, two functions are called, setCommandAnalysis and readCommandAnalysis, both of which pass in three parameters, the first instruction type, which you do not need to change, because it is passed in from a function outside, the second is a pointer to the data involved in this communication, that is, the first address of the relevant The second is a pointer to the data involved in this communication, that is, the first address of the relevant data, and the third is the length of the data, using the sizeof function. If this instruction does not involve the passing of parameters (just a simple action execution instruction), use the NULL pointer and the data length is 0.

results matching ""

    No results matching ""