2-About MCU communication data transmission (interrupt reception, big and small endian, IEEE754 floating point format, union, idle interrupt, ring queue)

 

 Previous linkhttp  ://www.cnblogs.com/yangfengwu/p/8628219.html

Let me first explain that this method is not only for single-chip microcomputers, but also for the host computer to receive data - not to lie, almost all of my own single-chip computer programs, host computer programs, including the 8266, GPRS, etc. All receive data in this way, if you don't know how to use it or don't understand... It can be said that this template receive is too practical for development.... But please don't just take it and use it, Please seriously think about the reason, if you don't understand the reason, it's useless on other platforms......

What method do you guys use to receive the serial port data,,,,, have you been troubled by the data reception and analysis?? I remember when I just graduated, I always feel that my program is not a good program

At that time, I kept thinking about whether there is a most final template available. Later, it was a program that I figured out the year before, so don't talk nonsense,

In fact, the most fundamental thing about receiving data is to judge that a complete string of data has been received, and then process it. In the past, and now, many people add some data headers, tails, and the number of data... and then receive I have been judging the head, the tail.... the number of data,... if the data changes again.... troublesome...

In fact, the best way to judge when a piece of data is received is to check whether the number of received data has changed after a period of time, and whether the number of received data has changed after a period of time.

After a period of time, judge whether the number of received data has changed. If the number of data has not changed after a period of time, then it is determined that a complete piece of data has been received.

First look at my 51 serial port

 

After a period of time and then to judge, the best and best way is to put it in the interrupt

 

 Finished writing,,, that's it....

Isn't it simple, but amazing

Process the data now

 

 The rest is handled by yourself, this is to judge that a complete piece of data has been received.

Data is stored in UsartReceive

Number of data UsartReadCntCopy

 

Now look at the 32's

 

 

 

 

 

 

 Now take a look at 8266's (developed by lua)

 

 

 

function InitTimer1function()
   if  ReceiveDataCnt ~=0 then
       if   ReceiveDataCnt == ReceiveDataCntCopy then
            TCPReadDataOverTime = 0;
            
            ReceiveDataCopy = ReceiveData;
            ReceiveData = "";
            ReceiveDataCnt = 0;
            ReceiveDataCntCopy = 0;
            
            if  updataFlage == 0 then
                disposedata(ReceiveDataCopy);
            else
                if  ReceiveDataCopy ~= "UpdataDataSuccess" then
                    file.write(ReceiveDataCopy)
                    updataCnt = updataCnt + 1
                    if  TcpConnect ~= nil then
                        TcpConnect:send("WaitUpdataData"..updataCnt..";");
                    end  
                    print("start down...")  
                else
                    if  TcpConnect ~= nil then
                        TcpConnect:send("UpdataDataSuccess");
                        TcpConnect:close()
                    end
                    file.close()
                    updataFlage = 0  
                    print("UpdataDataSuccess")  
                    
                    tmr.stop(0)
                    tmr.stop(1)
                    --dofile("main.lua");
                    node.compile("updata.lua")
                    dofile("updata.lua");
                    --dofile("main.lc"); 
                end
                ReceiveDataCopy = "";
            end
       else
            ReceiveDataCntCopy = ReceiveDataCnt      
       end
   end
end
tmr.alarm(1, 400, 1, InitTimer1function)

 

 

Don't read the host computer, but also open the software... The computer is about to freeze...

 

Now let's talk about the points:

 

 Suppose I receive a piece of data at an interval of 100ms,,, then my above time will be less than 100ms,

In fact, I have been using 5-10ms interval,

Suppose the baud rate is very low 1200, that is to say, one bit of data is sent every 1/1200 S == 0.83ms

If it is harsher, I will use the idle interrupt that comes with the chip... 51 does not have it (not sure if there is, I haven't read the manual carefully...), 32 yes

32 Although there is, I rarely use it...

 

 

Is it because of a carrier wave problem,,,, have you done a carrier wave?? Transmission of serial port data on the wire??

The data delay of the carrier is very high... It is too harsh to directly detect the idle interrupt of 32,,,, and it is impossible to judge accurately...

I’m talking about this today because my source code is in this mode, because some people don’t know why they wrote it this way, and they don’t know why it’s used in this way, so I’ll introduce one today, and if anyone asks later, I’ll show him this article directly. ...

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324727529&siteId=291194637