Implementation of command batch processing in serial communication between PC and microcontroller

Publisher:乡村乐园Latest update time:2006-05-07 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    Abstract: This article introduces the implementation ideas, methods, communication protocols and C51 program platform of batch processing commands for communication between PC and MCS-51 microcontroller. It provides a new method for the design of serial communication program between PC and microcontroller.

    Keywords: Batch processing MCS-51 microcontroller communication protocol

The serial communication between the PC and the MCS-51 microcontroller is basically one-to-one, that is, every time the PC sends a command to the microcontroller, it waits for a return message from the microcontroller. This may waste a lot of time in the communication process between the PC and the microcontroller. For this reason, after practice, the author has designed a system that can execute commands sent by the PC to the microcontroller in batches. Just like the batch commands in the DOS operating system, the information can be processed in batches continuously. As a result, the system speed is certainly improved. This article provides part of the platform C51 software source code for your reference.

1 Communication protocol

(1) Communication data format

PC→MCS-51 microcontroller data format is listed in Table 1.

Table 1

Stx Len Comma Data[0]…Data[N-1] Edc Etx
start length Order data body Check code end code
0x60 N+2(2Byte) (2Bytes)   Stx^Len^CommandH^
CommandL^data[0]…Data[N-1]

0X03

MCS-51 microcontroller→PC data format is listed in Table 2.

Table 2 

Stx Len Status Data[0]…Data[N-1] Edc Etx
start length Order data body Check code end code
0x60 N+2(2Byte) 2(byte)   Stx^Len^CommandH^
CommandL^data[0]..Data[N-1]
0X03

(2) Initial communication parameters

  Between PC and microcontroller (9600, n, 8, 1)That is, the baud rate is 9600 baud, 1 start bit, 8 data bits, 1 stop bit, and no parity.

(3) Data sending and receiving method

  All data is sent as binary single bytes.

(4) Sending data sequence

  Send synchronization code (1Byte): Stx=0X60

  Send the length of subsequent bytes (2Byte): Len=2+N

  Send Len subsequent data bytes (N+2 Byte): CommandH, CommandL, Data[0]…Data[N-1]

  Send verification code (1Byte): Edc=Stx^CommandH^ CommandL^Data[0]^…Data[N-1]

  Send end code (1Byte): Etx=0X03

2 Single command COMMAND[n]

COMMAND[n] format:

Command Data[0]~-Data[N-1]
XXXXH data body

Generally, the length of a single command is controlled within 255.

The data sequence sent by the PC to the microcontroller is:

STX, LENH, LENL, COMMANDH, COMMANDL, DATA...DATA, EDC, ETX

3 Thinking methods of batch processing

(1) Batch command BATCH_COMMAND (0X0000)

PC format:

Command Data[0] Data[1]~-Data[i] Data[i+1]… ---…Data[N]
0x0000 NUM number of commands Flag,COMMAND[0] Flag,COMMAND[i] Flag,COMMAND[n]

NUM - the number of subsequent commands;

Flag - the flag that needs to be returned (00 means data is to be returned,Others do not need to return data).

COMMAND[i]=LEN, subcommand body

Function: Batch execution function.

Return status and data: Determine whether to return data according to the Flag flag, and return data in the order of sending commands.

(2) Commands to control batch transfer

Function: No data or status is returned, and the lower computer is controlled to jump.

① Control BATCH_If (0X0001) for judgment and conditional execution of commands

Function: When the microcontroller encounters this command, it performs judgment. If the result is logically true, it executes the first command following BATCH_If; otherwise, it executes the second command following BATCH_If.

PC format:

Command Data1 Data2~-Datai Dataj…
0x0001 num EQUAL/NOT VALUE

num: Returns the number of data from right to left. When it is 0, it is the low-order byte of the status data;

EQUAL/NOT - 00 means equal, 1 means unequal;

VALUE – Numeric value.

Returns: None.

② Control BATCH_Break (0x0002) to jump out of the loop command

Function: Used to jump out of the BATCH_While loop.

PC format:

Command
0x0002

Returns: None.

③ Control BATCH_While (0x0003) to loop and execute the commands in the loop body

Function: cyclically execute the commands in the loop body.

PC format:

Command Data1
0x0003 Num (number of commands in the loop)

Returns: None.

④ Control BATCH_Return (0x0004) to exit the packaging program block

Function: Exit the packaging program block.

PC format:

Command
0X0004

Returns: None.

(3) Precautions

◇ BATCH _While loop cannot be used nested;

◇ BATCH _Break can only be used within the body of the BATCH _While loop;

◇ Commands that need to obtain returned data immediately should generally not be placed in batch processing.

(4) Examples of PC commands that control the transfer of packaging programs

    This instance determines whether to transfer based on the result of the command, and only uses Reader_Version() in the loop.

The code transferred from the PC to the microcontroller: BATCH_ COMMAND

    Reader_Version(): assumed to be 0X1001

The actual code downloaded from the PC is (HEX format):

60 28 00 00 06 01 00 03 00 00 05 01 00 04 00 01 00 00 03 01 00

02 00 02 01 00 05 00 01 05 00 61 01 00 02 00 04 00 00 02 10 01 Check code 03

4 Program flow

Each program flow is shown in Figure 1 to Figure 4.

5 parts of source code

Due to space limitations, only part of the source program is provided here.

(1) Main program

The main program first receives the data, analyzes the data, and distinguishes between batch processing and single commands; then executes the command; and finally returns the results and uploads them to the PC. The program list can be found in the supplementary version of this journal's website (website address: http://www.dpj .com.cn).

(2) Batch processing program

The batch processing program distinguishes the control transfer commands with flags, executes the commands by interpreting the command sequence, and finally packages the results according to the communication data format and uploads them to the PC. The program list can be found in the supplementary version of this journal's website.

Conclusion

This article explores the software programming of serial communication between PC and microcontroller to realize the processing of batch information data communication. I believe that this new idea and method will develop and be applied to actual embedded systems soon.

Reference address:Implementation of command batch processing in serial communication between PC and microcontroller

Previous article:Implementing CAN bus communication using 87C196NT microcontroller
Next article:Design of slave device communication adapter based on DeviceNet bus

Latest Industrial Control Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号