Below is a detailed introduction on how to use virtual serial ports to debug serial sending and receiving programs.
Three software programs are needed: KEIL, VSPD XP5 (virtual serial ports driver xp5.1), and Serial Debug Assistant (I personally find version 2.1 quite user-friendly).
1. First, compile the written program in KEIL.
2. Open VSPD, the interface is shown in the figure below:
The topmost item in the left sidebar is the physical serial port provided by the computer. Click on the add pair on the right to add paired serial ports. One pair of serial ports is already virtually interconnected; if you add COM3 and COM4, data sent from COM3 can be received by COM4, and vice versa.
3. The next step is crucial. Bind KEIL and the virtual serial port. Now bind COM3 with KEIL. Enter DEBUG mode in KEIL. In the command line at the bottom, type
MODE COM3 4800,0,8,1 (set the baud rate, parity, data bits, and stop bits for COM3, and open the COM3 port. Note that the set baud rate should match the one set in the program)
ASSIGN COM3 <SIN> SOUT (bind the microcontroller’s serial port with COM3. Since I am using the STC89C52 microcontroller, which has only one serial port, I use SIN and SOUT. If the microcontroller has several serial ports, you can choose S0IN, S0OUT, S1IN, S1OUT.)
To avoid rebinding the serial port every time, you can create an initialization file debug.ini. The initialization file is a regular text file, and its content is the commands needed during simulation, inputting one command per line in order. As shown in Figure 2, an initialization file debug.ini has been created. This way, every time you enter the simulation debugging state, Keil will automatically load the content of debug.ini for initialization.
4. Serial Debug Assistant
You can see the virtual serial ports COM3 and COM4; select COM4, set the baud rate to 4800, no parity, 8 data bits, and 1 stop bit (same as the settings in COM3 and the program). Open COM4.
Now you can start debugging the serial sending and receiving program. You can send data through KEIL, and it will be displayed in the Serial Debug Assistant. You can also send data through the Serial Debug Assistant and receive it in KEIL.
The Serial Debug Assistant does not support sending Chinese characters, carriage return characters, etc. If support is needed, you can use usr-tcp232-test (supports Chinese characters) or HyperTerminal (supports Chinese characters, carriage return characters, and has the same effect as the Keil serial simulation control).
The advantage of this method is that debugging can be done without hardware. This method is introduced in an article online: please read the original text.