This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022.

Welcome friends to search “Andy Hui” on wechat to pay attention to a wave!

Write some thoughts of the programmer, hope it will help you.

preface

Hi, I’m Hui.

Today, let’s learn about finite state machines. In the daily work of bloggers, it is unavoidable to deal with hardware, such as reading the collection data in the device in real time, receiving the collection parameters sent by the hardware, and collecting response commands.

Many times the finite-state machine model is used to ensure the integrity of the received data. So let’s see what a finite state machine is.

What is a finite state machine

Officially, a finite state machine is a tool used to model the behavior of an object. Its function is to describe the sequence of states an object goes through during its life cycle and how it responds to various events from the outside world.

In computers, finite state machines are used for modeling application behavior, hardware circuit system Settings, software engineering, compilers, network protocols, and computing and language studies.

In plain English, a finite state machine is a processing mechanism to ensure that certain conditions are met in a certain state. For example, in the receiving process of data stream, according to a specific protocol, all data is successfully received only when the data byte reaches a certain mark, and then it can be parsed and displayed. Otherwise, parsing failure occurs.

The so-called finite state machine is to determine the current state of our program object and the switch between states. The state machine can only point to a result, and the result points to the behavior of the state, that is, the function executed.

A Finite State Machine (Finite State Machine) is a set of Finite states in which one State is present at any time. When it gets an input character, it transitions from the current state to another state or remains in the current state.

There are three states: dead state, start state, and end state.

To start a finite state machine, you must first deal with the “start state” and then, depending on the input state character, the finite state mechanism will reach the end state or die state.

What are the characteristics of finite state machines

1, can ensure the accuracy of received data, prevent data loss.

2. For a high degree of consistency in the parsing process, there will be no parsing error when the data is complete.

3, used for state management of the program, easy to read and understand after implementation.

4. The system has a finite number of states, and different states represent different meanings.

5, each state can be converted according to the special conditions of the input.

Demo

In daily work, sometimes the detection data sent by the hardware device of the lower computer. So the upper computer program has to be analyzed according to a specific protocol, output display detection data information. So let’s see how this works.

AA BB xx xx xx XX XX XX CC DD

The header is AA BB and the tail is CC. The total length of the DD protocol is 10 bytes and the data length is 6 bytes.

/// </summary> static void StateJudge(byte currentByte) {switch (currentState) {case 0: receivedBytes.Clear(); dataCount = 0; if (currentByte == (byte)0xAA) { currentState++; receivedBytes.Add(currentByte); break; } break; case 1: if (currentByte == (byte)0xBB) { currentState++; receivedBytes.Add(currentByte); break; } currentState = 0; break; case 2: if (dataCount < 6) { ++dataCount; receivedBytes.Add(currentByte); break; } if (currentByte == (byte)0xCC) { currentState++; receivedBytes.Add(currentByte); break; } currentState = 0; break; case 3: if (currentByte == (byte)0xDD) { receivedBytes.Add(currentByte); AnalysisData(receivedBytes.ToArray()); } currentState = 0; dataCount = 0; break; default: currentState = 0; dataCount = 0; break; }} /// <summary> /// <param name="fullDataStream"> complete data stream </param> static void AnalysisData(byte[] fullDataStream) { Console.WriteLine(""); Console.WriteLine(" Output complete data stream "); for (int i = 0; i < fullDataStream.Length; i++) { Console.Write(fullDataStream[i] + ","); }}Copy the code
// </summary> static int currentState = 0; static int dataCount = 0; static List<byte> receivedBytes = new List<byte>(); Static void Main(string[] args) {// Parse the data sent by the lower machine // AA BB xx xx xx xx xx xx CC DD byte[] tempCollectData = new byte[] { 0xAA, 0xBB, 0xdc, 0xea, 0xed, 0xef, 0xea, 0x30, 0xCC, 0xDD }; Console.WriteLine(" Input data :"); for (int i = 0; i < tempCollectData.Length; i++) { Console.Write(tempCollectData[i] + ","); } for (int i = 0; i < tempCollectData.Length; i++) { byte entityByte = tempCollectData[i]; StateJudge(entityByte); } Console.ReadKey(); }Copy the code

Small remarks

Life is short. I don’t want to go for what I can’t see. I want to catch what I can see.

Original is not easy, give a attention.

I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.