Delphi is a visual rapid application development tool with the advantages of powerful functions, ease of use and fast code execution. It plays an increasingly important role in building enterprise information systems. Many programmers are willing to choose Delphi as a development tool. Program various applications. However, the fly in the ointment is that Delphi does not have its own serial communication control, and there is no mention of serial communication in its help documentation, which brings a lot of inconvenience to developers who compile communication programs.
Currently, there are three commonly used methods to implement serial communication using Delphi: one is to use controls, such as MSCOMM control and SPCOMM control; the other is to use API functions; the third is to call other serial communication programs. Among them, using API to write serial communication programs is more complicated and requires a lot of communication knowledge. In comparison, it is relatively simple to use the SPCOMM control, and the control has rich properties and events closely related to serial port communication, provides various operations on the serial port, and also supports multi-threading. The following article introduces the use of SPCOMM control in detail with examples.
SPCOMM installation
1. Select the Install Component option in the drop-down menu Component, fill in the path where the SPCOMM control is located in the Unit file name, default values can be used for other items, and click the OK button.
2. After installation, a red control COM will appear in the System control panel. Now you can use COM controls just like Delphi's own controls.
SPCOMM properties, methods and events
1.Attributes
●CommName: Indicates the names of serial ports such as COM1 and COM2;
●BaudRate: The baud rate set according to actual needs. This value can also be changed after the serial port is opened, and the actual baud rate will change accordingly;
●ParityCheck: Indicates whether parity check is required;
●ByteSize: The byte length set according to the actual situation;
●Parity: parity bit;
●StopBits: stop bits;
●SendDataEmpty: This is a Boolean attribute. When it is true, it means that the sending buffer is empty, or there is no information in the sending queue; when it is false, it means that the sending buffer is not empty, or there is information in the sending queue.
2. method
●Startcomm method is used to open the serial port. When the opening fails, an error is usually reported. There are seven main types of errors: ⑴ The serial port has been opened; ⑵ Error in opening the serial port; ⑶ The file handle is not a communication handle; ⑷ The communication cache cannot be installed; ⑸ The event cannot be generated; ⑹ The reading process cannot be generated; ⑺ The writing process cannot be generated;
●StopComm method is used to close the serial port and has no return value;
●WriteCommData(pDataToWrite: PChar;dwSizeofDataToWrite:Word) method is a function with a Boolean return value. It is used to send a string to the writing process. It returns true if the sending is successful and false if the sending fails. Executing this function will get the return value immediately, and the sending operation will be executed subsequently. This function has two parameters, where pDataToWrite is the string to be sent, and dwSizeofDataToWrite is the length of the string to be sent.
3. event
●OnReceiveData :PRocedure (Sender: TObject;Buffer: Pointer;BufferLength: Word) of object
This event will be triggered when data is input to the buffer, where the data received from the serial port can be processed. Buffer contains the received data, and BufferLength is the length of the received data.
●OnReceiveError: procedure(Sender: TObject; EventMask: DWORD)
This event is triggered when there is an error in receiving data.
Use of SPCOMM
The following is an example of serial communication using SPCOMM control.
Taking the communication between PC and microcontroller 8051 as an example, we must first adjust the handshake signal between them. Assume that the communication protocol between them is: One frame of data from PC to 8051 is 6 bytes, and one frame of data from 8051 to PC is also 6 bytes. When PC sends (F0,01,FF,FF,01,F0), 8051 can receive a frame (F0,01,FF,FF,01,F0), indicating that the data communication handshake is successful, and the two can follow the Protocols transfer data to each other.
Create a new project COMM.DPR, set the NAME property of the form to FCOMM, define the title of the form as Test Communication, and add the corresponding controls.
1. Set COMM1 attributes:
●Baud rate: 4800;
●Parity bit: None;
●Byte length: 8;
●Stop bit: 1;
●Serial port: COM1.
The sent and received data will be displayed in Memo1. Store the new form as Comm.pas.
2.Write source code
//Variable description
var
fcomm:TFCOMM;
viewstring:string;
i:integer;
rbuf,sbuf:array[16] of byte;
//Open serial port
procedure TFCOMM.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;
//Close the serial port
procedure TFCOMM.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
//Customize the process of sending data
procedure senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to 6 do
begin
if not fcomm.comm1.writecommdata(@sbuf[i],1) then
begin
commflg:=false;
break;
end;
//Delay between bytes when sending
sleep(2);
viewstring:=viewstring+intttohex(sbuf[i],2)+''; end;
viewstring:='Send'+viewstring;
fcomm.memo1.lines.add(viewstring);
fcomm.memo1.lines.add('' );
if not commflg then messagedlg('Sending failed!',mterror,[mbyes],0);
end;
//Send button click event
procedure TFCOMM.Btn_sendClick(Sender: TObject);
begin
sbuf[1]:=byte($ f0); //Frame header
sbuf[2]:=byte($ 01); //Command number
sbuf[3]:=byte($ ff);
sbuf[4]:=byte($ ff);
sbuf[5]:=byte($ 01);
sbuf[6]:=byte($ f0); //End of frame
senddata;//Call the send function
end;
//receiving process
procedure TFCOMM.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
var
i:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf^),bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+intttohex(rbuf[i],2)+'';
viewstring:='receive'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('' );
end;
If memo1 displays sending F0 01 FF FF 01 F0 and receiving F0 01 FF FF 01 F0, this means that the serial port has correctly sent data and received data correctly, and the serial communication is successful.