//The following is the MSCOMM parameter setting process when creating a form
//MSComm1.InputMode := comInputModeBinary;
// and MSComm1.InputMode := comInputModeText;
//The experimental results basically have little effect on Delghi
procedure TForm1.FormCreate(Sender: TObject);
var
str: string;
begin
//MSCOMM parameter settingsMSComm1.CommPort
:= 1;//Use COM1
MSComm1.Settings := ''9600,N,8,1'';//Set communication port parametersMSComm1.InBufferSize
:= 32;//Set MSComm1 receive buffer to 32 bytesMSComm1.OutBufferSize
:= 2;//Set MSComm1 send buffer to 2
bytesMSComm1.InputMode := comInputModeBinary;//Set the receive data mode to binary formMSComm1.InputLen
:= 1;//Set the number of bytes read from the receive buffer at one time for Input to be 1
MSComm1.SThreshold := 1;//Set the number of bytes read from the send buffer at one time for Output to be 1
MSComm1.InBufferCount := 0;//Clear receive bufferMSComm1.OutBufferCount
:= 0;//Clear send bufferMSComm1.RThreshold
:= 1;//Set to generate OnComm event when receiving a
byteMSComm1.PortOpen := true;//Open serial port 1
/////////////////////////////////////////////////////////////////////////////
Buffers := '''';
CheckSum := 0;
//Send serial port command
Command := 34;
str := ''$'' + #2 + #$22 + #1;//Read total MP3 tracksstr
:= str + Char(GetCheckSum(str));//Calculate
checksumMSComm1.Output := str;//Send serial port
commandend;
//The following is the receiving event processing process, which is equivalent to serial port interrupt in MCU
//Note that there are two statements
//while MSComm1.InBufferCount > 0 do //Input FiFO is not empty
//if str = '''' then str := #0; //0 character processing
//Example received data is #24#02#00#01#03
//At this time, InBufferCount=5. If Input is set, the number of bytes read from the receive buffer at one time is unlimited
//That is: MSComm1.InputLen := 0; then str := MSComm1.Input; str seems to be #24#02#00#01#03
//But the actual value is ''??''#24#02. No result is obtained, at least the #01#03 after the 0 character cannot be read.
//Use MSComm1.InputLen := 1; and cooperate with while MSComm1.InBufferCount > 0 do
//When reading 0 characters, since str = '''' (null), accessing str[1] will cause an exception and cause the program to terminate.
//So use if str = '''' then str := #0; to fill the character #0 into str[1] and the length of str is also 1.
//Therefore, the key point is to use the if str = '''' then str := #0; statement to overcome the difficulty of reading 0 characters~~~
procedure TForm1.MSComm1Comm(Sender: TObject);
var
str: string;
i: integer;
begin
case MSComm1.CommEvent of
comEvReceive://receive event processing
begin
while MSComm1.InBufferCount > 0 do//Input FiFO is not empty
begin
str := MSComm1.Input;//Only take 1 character from FIFO, because MSComm1.InputLen := 1
if str = '''' then str := #0; //0 character processing
if (Buffers = '''') and (str = ''$'') then//Synchronizer test
begin
Buffers := str;//Store synchronization symbol ''$''
CheckSum := 0;//Initialize checksum
end
else if (Buffers <> '''') and (Buffers[1] = ''$'') then begin//Must start with synchronization symbol
Buffers := Buffers + str;//Add data string
CheckSum := CheckSum xor Byte(str[1]);//Calculate the checksum (except the synchronization character ''$'')
if Length(Buffers) = Byte(Buffers[2]) + 3 then//Terminator test
begin
if CheckSum = 0 then//At this time, the checksum must be 0 to indicate that the checksum is correct
begin
case Command of
$22: begin//Get the total number of songs
ComboBox1.Items.Clear;
for i := 1 to Byte(Buffers[4]) do
begin
str := ''第'' + inttostr(i) + ''首歌曲'';
ComboBox1.Items.Add(str);//
end;
Command := 0;
end;
1: ;
else ;
end;
end;
Buffers := '''';//Clear the buffer after receiving
CheckSum := 0;//Initialize the checksum
end;
end
else
begin
Buffers := ''''; //Receive error, clear the buffer and discard all data
CheckSum := 0; //Initialize checksum
end;
end;
end;
end;
end;
Previous article:Ordinary MCU Teaching——Lecture 10 MCU Instructions (IV)
Next article:From 51 Beginner to Electronic Engineer (7)
Recommended ReadingLatest update time:2024-11-23 03:13
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Delphi XE5 Mobile Development Getting Started Manual
- Delphi Programming Tutorial (3rd Edition)
- Detailed explanation of microcontroller serial port communication and measurement and control applications (Li Jiangquan, Nie Jing, Liang Xihuizi, Liu Xinying)
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- [Micropython Tutorial] +3 RTC datetime array parameter reading
- Ethernet protection design for monitoring system
- May I ask if the common collector, common emitter and common base in analog electronics are connected to the ground using these poles?
- IPC-4552B-2021 EN Printed Board Electroless Nickel Immersion Gold (ENIG) Plating Performance Specification English Version
- What are the advantages and disadvantages of the series of tutorials on STM32F103 on various platforms on Station B? ? ?
- FS8108 Application Note
- Application of OPT3004 in battery-powered cameras
- General PCB design layout rules
- "English Abbreviations" in Hardware Schematics
- Very stuck, reinstall also stuck