// The following is the MSComm parameter setting process when the form is created.
// Mscomm1.inputmode: = cominputmodebinary;
// And mscomm1.inputmode: = cominputmodetext;
// The experiment results are basically ineffective for Delghi.
Procedure tform1.formcreate (Sender: tobject );
VaR
STR: string;
Begin
// MSComm parameter settings
Mscomm1.commp ORT: = 1; // use COM1
Mscomm1.settings: = ''9600, N, 8, 1 ''; // set the communication port parameters.
Mscomm1.inbuffersize: = 32; // set the mscomm1 receiving buffer to 32 bytes.
Mscomm1.outbuffersize: = 2; // set the mscomm1 sending buffer to 2 bytes.
Mscomm1.inputmode: = cominputmodebinary; // you can specify the binary mode for receiving data.
Mscomm1.inputlen: = 1; // set the number of bytes read from the received buffer at a time to 1.
Mscomm1.sthreshold: = 1; // set the number of bytes read from the sending buffer at a time to 1.
Mscomm1.inbuffercount: = 0; // clear the receiving buffer
Mscomm1.outbuffercount: = 0; // clear the sending Buffer
Mscomm1.rthreshold: = 1; // set to receive a byte to generate an oncomm event
Mscomm1.portopen: = true; // enable Serial Port 1
//////////////////////////////////////// /////////////////////
Buffers: = '''';
Checksum: = 0;
// Send the serial port command
Command: = 34;
STR: = ''$'' + #2 + #$22 + #1; // read the total MP3 tracks
STR: = STR + char (getchecksum (STR); // calculate the checksum
Mscomm1.output: = STR; // send a serial port command
End;
// The following is the process of receiving event processing, which is equivalent to serial interruption in MCU
// Note the two statements
// While mscomm1.inbuffercount> 0 do // The input FIFO is not empty.
// If STR = ''' then STR: = #0; // 0 Character Processing
// The data received in the example is #24 #02 #00 #01 #03
// Inbuffercount = 5 at this time. If you set the number of bytes read from the receiving buffer for one input operation
// That is: mscomm1.inputlen: = 0; then STR: = mscomm1.input; then STR seems to be #24 #02 #00 #01 #03
// But it is actually ''?? ''#24 #02. The result is not always obtained. It cannot be read after at least 0 characters #01 #03.
// Use mscomm1.inputlen: = 1; and then use the while mscomm1.inbuffercount> 0 do
// When 0 characters are read, access to STR [1] may cause an exception and cause program termination because STR = ''' (null.
// If STR = ''' then STR: = #0; is used to enter #0 in STR [1] and the STR length is also 1.
// Therefore, the key point is to use if STR = ''' then STR: = #0; statement to tide over the difficulty of reading 0 characters ~~~
Procedure tform1.mscomm1comm (Sender: tobject );
VaR
STR: string;
I: integer;
Begin
Case mscomm1.commevent
Comevreceive: // receives event processing
Begin
While mscomm1.inbuffercount> 0 do // The input FIFO is not empty.
Begin
STR: = mscomm1.input; // only one character is allowed from the FIFO, because mscomm1.inputlen: = 1
If STR = ''' then STR: = #0; // 0 Character Processing
If (buffers = ''') and (STR = ''' $ '') Then // test the synchronization operator.
Begin
Buffers: = STR; // Save the synchronization token ''$''
Checksum: = 0; // initialize the checksum.
End
Else if (buffers <> ''') and (buffers [1] = ''' $ '') then begin // start with a synchronization character
Buffers: = buffers + STR; // Add a data string
Checksum: = checksum XOR byte (STR [1]); // calculate the checksum (except for the synchronization token ''$)
If length (buffers) = byte (buffers [2]) + 3 then // end Character Test
Begin
If checksum = 0 then // The Checksum must be 0, indicating that the checksum is correct.
Begin
Case command
$22: Begin // obtain the total number of songs
Combobox1.items. Clear;
For I: = 1 to byte (buffers [4]) Do
Begin
STR: = ''1'' + inttostr (I) + ''1son '';
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: = '''; // receives an error to clear the buffer and discard all data.
Checksum: = 0; // initialize the checksum.
End;
End;
End;
End;
End;