Serial number |
project |
Length (bytes) |
illustrate |
1 |
Packet header (STX) |
1 |
Constant: 0x02 |
2 |
Data unit length (Data_len) |
2 |
The length of the Data part of the data unit to be transmitted, with the high byte in front and the low byte in the back. For example: 0x0010 means the Data part has 16 bytes. |
3 |
Data unit to be transmitted (Data) |
indefinite |
The length is indicated by Data_len. The first two bytes of the data unit are the command code (the terminal sends a command to the reader) or the status code (the reader returns data to the terminal), followed by other parameters. |
4 |
Redundancy Check Value (LRC) |
1 |
The XOR value of each byte of the data in the Data part. |
5 |
Packet tail (ETX) |
1 |
Constant: 0x03 |
The program implementation is as follows:
- #include
- typedef struct newStruct
- {
- unsigned char startFlag;
- unsigned char finishFlag;
- unsigned char lenHighFlag;
- unsigned char lenLowFlag;
- unsigned char dataFlag;
- unsigned char lrcFlag;
- unsigned char buf[512];
- unsigned char lenHigh;
- unsigned char lenLow;
- unsigned char dataStartIndex;
- unsigned short len;
- unsigned short index;
- unsigned short tempLen;
- }rxstruct;
- rxstruct rxArray;
- void m430_InitUart()
- {
- P3SEL |= BIT4|BIT5; // P3.4, P3.5 = USCI_A0 TXD/RXD
- UCA0CTL1 |= UCSSEL_2; // SMCLK
- //The following three lines are used to set the baud rate
- UCA0BR1 = 0;
- UCA0BR0 = 104; // 12MHz:1250->9600,625->19200,312->38400,214->56000,104->115200
- UCA0MCTL = 0x02; // Modulation UCBRSx = 1
- UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
- //IE2 |= UCA0RXIE | UCA0TXIE; //Note that the interrupt enable should be set after initializing USCI, otherwise it will not work. That is, if this sentence is placed before UCA0CTL1 &= ~UCSWRST;, the interrupt will not be responded to.
- IE2 |= UCA0RXIE;
- }
- unsigned char uart_CalLrc(unsigned char *buf, unsigned short len)
- {
- unsigned short i;
- unsigned char lrc;
- lrc = 0x00;
- for( i=0; i
- {
- lrc ^= buf[i];
- }
- return lrc;
- }
- void main()
- {
- WDTCTL = WDTPW + WDTHOLD;
- BCSCTL1 = CALBC1_12MHZ;
- DCOCTL = CALDCO_12MHZ;
- m430_InitUart();
- rxArray.startFlag = 0;
- rxArray.finishFlag = 0;
- _EINT();
- _BIS_SR(LPM4_bits);
- while(1)
- {
- }
- }
- #pragma vector = USCIAB0RX_VECTOR
- __interrupt void uartRxHandle()
- {
- unsigned char num;
- num = UCA0RXBUF;
- if( 0==rxArray.startFlag )//Judge whether the frame header is received
- {
- if( 0x02==num )//Judge whether the frame header is correct
- {
- rxArray.startFlag = 1; //Sign that the frame header has been received
- rxArray.finishFlag = 0;
- rxArray.lenHighFlag = 0;
- rxArray.lenLowFlag = 0;
- rxArray.dataFlag = 0;
- rxArray.lrcFlag = 0;
- rxArray.index = 0;
- rxArray.len = 0; //Storage frame length
- rxArray.buf[rxArray.index] = num;
- rxArray.index++;
- }
- return;
- }
- if( 0==rxArray.lenHighFlag )//Judge whether the high byte information of the frame length is received
- {
- rxArray.lenHighFlag = 1; // Flag that the high byte of the frame length has been received
- rxArray.lenHigh = rxArray.buf[rxArray.index] = num;
- rxArray.index++;
- return;
- }
- if( 0==rxArray.lenLowFlag )//Judge whether the low byte information of the frame length is received
- {
- rxArray.lenLowFlag = 1; // Flag that the low byte of the frame length has been received
- rxArray.lenLow = rxArray.buf[rxArray.index] = num;
- rxArray.index++;
- rxArray.dataStartIndex = rxArray.index;
- rxArray.tempLen = rxArray.len = (rxArray.lenHigh<<8) + rxArray.lenLow; //Add one byte to the end of the frame
- if( rxArray.len+5>512 )//If the data length is greater than 12, it means that the received data length information is incorrect and needs to be received again
- {
- rxArray.startFlag = 0;
- rxArray.lenHighFlag = 0;
- rxArray.lenLowFlag = 0;
- }
- return;
- }
- if( 0==rxArray.dataFlag )
- {
- rxArray.buf[rxArray.index] = num; //Store the data in the array
- rxArray.index++;
- rxArray.tempLen--;
- if( 0==rxArray.tempLen )
- {
- rxArray.dataFlag = 1;
- }
- return;
- }
- if( 0==rxArray.lrcFlag )//Receive lrc
- {
- rxArray.buf[rxArray.index] = num;
- rxArray.index++;
- rxArray.lrcFlag = 1;
- if( 0!=uart_CalLrc( &rxArray.buf[rxArray.dataStartIndex], rxArray.len+1 ) )//Judge whether the lrc of the received data is correct
- {
- rxArray.startFlag = 0;
- rxArray.lenHighFlag = 0;
- rxArray.lenLowFlag = 0;
- rxArray.dataFlag = 0;
- rxArray.lrcFlag = 0;
- }
- return;
- }
- rxArray.buf[rxArray.index] = num;
- rxArray.finishFlag = 1;
- rxArray.startFlag = 0;
- rxArray.lenHighFlag = 0;
- rxArray.lenLowFlag = 0;
- rxArray.dataFlag = 0;
- rxArray.lrcFlag = 0;
- if( rxArray.buf[rxArray.index]!=0x03 )//The last byte is not 0x03, indicating that the data is incorrect and needs to be received again
- {
- rxArray.finishFlag = 0;
- }
- if( rxArray.finishFlag )
- {
- //Received data processing part
- }
- }
A fatal bug in this program is that if an error occurs when receiving the two bytes of length information, a packet of data cannot be correctly formed, and subsequent data packets cannot be received correctly.
For example, there is a packet of data (hexadecimal): 02 00 02 11 11 00 03,
If an error occurs during the receiving process, the two bytes of length 00 02 will be changed to 00 05 at the receiving end. Therefore, the receiving end will wait until 5 bytes of data are received before considering that a packet of data has been completely received. As a result, part of the complete data packet sent later will be split. This vicious cycle will continue over and over again, and no good solution has been found yet!
Previous article:MSP430F2131 reads and writes DS1991
Next article:Which interfaces can be used to program MSP430?
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- "Playing with the board" + Zhou Hangci's book Chapter 7, Example 4
- If you have a third child, the government will give you a house. Would you give birth to a third child?
- EEWORLD University - Raspberry Pi 4 unboxing and assembly
- Pingtou Ge RVB2601 Review: Console and CDK
- Ginkgo USB-SPI nRF24L01 host computer debugging software source code download
- New Technology for Air Quality Monitors and Smoke Detectors
- 【ESP32-C3-DevKitM-1】ESP32-C3 development environment construction
- Layoffs, employment difficulties…what do you think of this winter?
- DSP28 fir low-pass filter design source code
- Semiconductor Device Physics (Shi Min)