Tutorial-delphi Spcomm using properties and usage

Source: Internet
Author: User

Spcomm use attribute and usage in Delphi

Delphi is a powerful, easy-to-use and fast code execution and other advantages of the visual rapid application development tools, it plays an increasingly important role in the framework of enterprise information systems, many programmers are willing to choose Delphi as a development tool to develop a variety of applications. However, the drawback is that Delphi does not have its own serial communication control, in its help document also does not mention the serial communication, which gives the development of communication programs to bring a lot of inconvenience to developers.

At present, there are 3 common methods of using Delphi to realize serial communication: one is to make use of control, such as MSComm control and Spcomm control, the other is to use API function, third is to call other serial communication program. The use of API to write serial communication program is more complex, need to master a large number of communication knowledge. In comparison, the use of the Spcomm control is relatively simple, and the control is rich in communication with the serial port of the properties and events, provides a variety of operations on the serial port, but also support multithreading. The use of the Spcomm control is described in detail in the following example.

Installation of Spcomm

1. Select the Install component option in the drop-down menu component to pop up the window shown in 1.

  

In the Unit file name, fill in the path of the Spcomm control, and the other items are available by default, click the OK button.
2. After installation, a red control COM appears in the System Control Panel. You can now use COM controls just like Delphi comes with controls.
Spcomm properties, methods, and events

1. Properties
commname: Indicates the name of COM1, COM2 and other serial ports;
baudrate: According to the actual need to set the baud rate, after the serial port opened can also change this value, the actual baud rate changes;
ParityCheck: Indicates whether parity is required;
bytesize: Byte length According to the actual situation;
Parity: parity check bit;
stopbits: Stop bit;
Senddataempty: This is a Boolean property that is true when the send cache is empty, or there is no information in the send queue, or false to indicate that the send cache is not empty or that there is information in the Send queue.

2. Method
The Startcomm method is used to open the serial port, which usually causes an error when open fails. There are 7 main errors: ⑴ serial port is open; ⑵ open serial port error; ⑶ file handle is not a communication handle; ⑷ cannot install communication cache; ⑸ cannot generate events; ⑹ cannot generate a read process; ⑺ cannot generate a write process;
The Stopcomm method is used to close the serial port without a return value;
The Writecommdata (Pdatatowrite:pchar;dwsizeofdatatowrite:word) method is a function with a Boolean return value that is used to send a string to the write process, sending a successful return of true, Send failed to return false. Executing this function will immediately get the return value, which is then executed by the send operation. The function has two parameters, where Pdatatowrite is the string to send, and dwsizeofdatatowrite is the length of the sending string.

3. Event
Onreceivedata: procedure (Sender:tobject; Buffer:pointer; Bufferlength:word) of object
This event is triggered when there is a data input cache, where the data received from the serial port can be processed. Buffer is the received data, Bufferlength is the length of the received data.
Onreceiveerror:procedure (Sender: TObject; Eventmask:dword)

Use of Spcomm
The following is an example of a serial communication using the Spcomm control.
In order to realize the communication between PC and MCU 8051, for example, we must first adjust the handshake signal between them. Assume that the communication protocol between them is: PC to 80,511 frames of data 6 bytes, 8051 to PC frame data is also 6 bytes. When the PC is issued (F0,01,FF,FF,01,F0) after 8051 can receive a frame (F0,01,FF,FF,01,F0), indicating that the data communication handshake succeeds, the two can be transferred to each other according to the protocol.

Create a new project COMM. DPR, the Name property of the form is Fcomm, the title of the form is defined as test communication, and the control is added as shown in Figure 2 (the control that is enclosed by the black rectangle in Figure 2 is COMM1).

1. Set the COMM1 property:
Baud rate: 4800;
Parity check bit: none;
byte length: 8;
Stop bit: 1;
Serial port: COM1.
The data sent and received will be displayed in Memo1. Save the new form as Comm.pas.

2. Writing source code

1 //Variable Description2 var3 Fcomm:tfcomm;4ViewString:string;5 I:integer;6RBUF,SBUF:Array[ -] ofbyte;7 //Open the serial port8 procedureTfcomm. Formshow (sender:tobject);9 beginTen comm1. Startcomm; One End; A //Close the serial port - procedureTfcomm. Formclose (Sender:tobject;varaction:tcloseaction); - begin the comm1. Stopcomm; - End; - //Customizing the Send data process - procedureSendData; + var - I:integer; + Commflg:boolean; A begin atviewstring:="; -commflg:=true; -  fori:=1  to 6  Do - begin - if  notFcomm.comm1.writecommdata (@sbuf [i],1) Then - begin incommflg:=false; -  Break; to End; + //time delay between bytes sent -Sleep2); theviewstring:=viewstring+ Inttohex (Sbuf[i],2) + ';End; *viewstring:=' send ' + viewstring; $ Fcomm.memo1.lines.add (viewstring);Panax Notoginseng Fcomm.memo1.lines.add ("); - if  notCommflg ThenMessagedlg (' Send failed! ', Mterror,[mbyes],0); the End; + //Click events for the Send button A procedureTfcomm. Btn_sendclick (sender:tobject); the begin +sbuf[1]:=byte ($ F0);//Frame Header -sbuf[2]:=byte ($ on);//Command Number $sbuf[3]:=byte ($ ff); $sbuf[4]:=byte ($ ff); -sbuf[5]:=byte ($ on); -sbuf[6]:=byte ($ F0);//End of Frame theSendData;//calling the Send function - End;Wuyi //Receive Process the procedureTfcomm.comm1receivedata (sender:tobject; Buffer:pointer; Bufferlength:word); - var Wu I:integer; - begin Aboutviewstring:="; $ Move (Buffer^,pchar (@rbuf ^), bufferlength); -  fori:=1  toBufferlength Do -viewstring:=viewstring+ Inttohex (Rbuf[i],2) + '; -viewstring:=' receive ' + viewstring; A Memo1.lines.add (viewstring); + Memo1.lines.add ("); the End;

If the MEMO1 on the display sends F0 FF FF F0 and receives F0 FF FF F0, which means that the serial port has correctly sent the data and received the data correctly, the serial communication succeeds.

Tutorial-delphi Spcomm using properties and usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.