1. Introduction

Computer serial communication is the basic communication mode of data transmission between computer and control equipment (PLC), and it is also the communication mode often used to realize industrial automatic control. Each communication mode has its corresponding communication protocol strictly. To ensure the normal communication between computer and PLC, we must write communication program according to its communication protocol.

2. Serial communication

Serial communication has always occupied an extremely important position in the field of industrial system control. Serial port (RS-232) is a standard configuration on the computer, which is often used to connect the modem to transmit data. It can be seen in the computer hardware device manager that it is defined as COM1, com2, etc. There are two kinds of common serial communication modes, RS-232 and RS-485. This paper introduces RS-232 as an example.

3. PC programming

3.1 C + + Builder programming

C + + Builder is a product of Borland company. It uses C + + as the development language, is an object-oriented language, has a visual programming interface and powerful functions.

3.2 API functions related to serial communication of C + + Builder

C + + Builder itself does not provide a separate serial communication component, but uses some windows API functions to achieve this purpose. These functions are provided by the operating system, which can provide quite a lot of executive functions for programmers. There are about 20 functions related to serial communication in API. This paper will discuss the commonly used functions.

(1) Open serial port

hcomm=createfile(comno,generic_ read|generic_ write,0,null,open_ existing,0,0)

Function parameters are defined as follows:

Hcomm: the return value of the createfile() function, which is used by the program for related serial port operations.

Comno: defines the serial port number as COM1, com2, etc.

generic_ read|generic_ Write: read / write operation on serial port.

0: whether to share the serial port or not. Usually, the serial port will not be shared with other programs, so it is set to 0, otherwise it is 1.

Null: whether the return value hcomm of the function can be inherited by the subroutine, which is set as non inheritable here.

open_ Exisiting: the way to open a port. A serial port is a device and must be specified as open_ Exisiting mode.

0: use synchronous or asynchronous mode to transmit data, synchronous mode programming is simple and fast, so it is set to 0, otherwise it is 1.

0: set to 0 due to serial port programming.

(2) Get the serial port status:

getcommstate(hcomm,&dcb)

Function parameters are defined as follows:

Hcomm: the return value of the createfile() function.

DCB: the address of the serial port control block, which is responsible for setting the parameters of the serial port. The specific parameters are as follows:

Dcb.baudrate: set the baud rate of serial port, including 19200kb / s, 9600kb / s and 4800kb / s, generally 9600kb / s.

Dcb.bytesize: set the data bits of serial port, including 5, 6, 7 and 8, and OMRON PLC data bits are 7.

Dcb.parity: set the parity check of the serial port, including none, even and odd, and set it to none.

Dcb.stopbits: set the stop bits of serial port, including 1, 1.5 and 2, and the stop bit of OMRON PLC is 1.

(3) Set serial port status:

setcommstate (hcomm,&dcb)

The function parameter definition is the same as the getcommstate() function.

(4) Write data to serial port:

writefile(hcomm,senddata,bs,&lrc,null)

Function parameters are defined as follows:

Hcomm: the return value of the createfile() function.

SendData: address to write data.

BS: the number of bytes written to the data.

LRC: the data address to be written.

Null: write data synchronization check, serial port synchronization communication can be set to null.

(5) To clear the error of the serial port or send the current data state of the serial port to the input buffer:

clearcommerror(hcomm,&dwerror,&cs)

Function parameters are defined as follows:

Hcomm: the return value of the createfile() function.

Dwerror: returns the error code.

CS: structure variable pointing to the serial port state.

(6) Read data from the input buffer of the serial port:

readfile(hcomm,inbuff,cs.cbinque,&nbytesread,null); Function parameters are defined as follows:

Hcomm: the return value of the createfile() function.

Inbuff: refers to the address used to store data.

Cs.cbinque: the number of bytes of read data.

Nbytesread: the total number of bytes read.

Null: if no background work is performed, the serial port is set to null.

(7) To turn off the serial port:

closehandle(hcomm)

Function parameters are defined as follows:

Hcomm: the return value of the createfile() function.

4. Introduction of PLC communication data frame

When the computer communicates with OMRON PLC, the response mode is adopted. A group of ASCII code character data is sent to PLC by computer, which becomes command block. After receiving the command block, PLC thinks that the command is normal, then it operates according to the command and returns the operation result to the computer. This group of data returned to the computer by PLC is called response block. If the PLC receives the command and confirms that the command is abnormal after analysis, it will return to the computer error command block. When the computer communicates with PLC, Omron PLC is passive, so the computer must send the command block to PLC first, and then the PLC sends the response block to the computer. The command block and response block are transmitted in frame, and a frame is composed of 131 characters at most. The structure of OMRON PLC command frame and response frame is introduced as follows:

4.1 command frame

The structure of command frame is shown in Figure 1.

How to use C + + Builder API function to write serial communication control program

Frame structure analysis:

@: must be placed at the beginning

Node number: valid value is 00-31, which means that PC can communicate with 32 PLC at most

Head code: command code of PLC

Send text: command parameter sent by PC

FCS (frame check sequence): frame check sequence code

The frame check code is a 2-bit hexadecimal number. It is the result of XOR operation of all characters contained in frame data.

Terminator: ‘*’ and carriage return ‘CR’

Examples are as follows:

The command frame structure of read h area is shown in Figure 2.

How to use C + + Builder API function to write serial communication control program

4.2 response frame

The response frame structure is shown in Fig. 3.

How to use C + + Builder API function to write serial communication control program

Frame structure analysis:

@: returns the command header

Node number: the valid value is 00-31, which returns the PLC node number of the data

Head code: command code of PLC

Tail code: returns the command completion status code

Receive text: data returned when there is data

FCS: frame check sequence code

Terminator: ‘*’ and carriage return ‘CR’

Examples are as follows:

The response frame structure of the read h area is shown in Fig. 4.

How to use C + + Builder API function to write serial communication control program

4.3 calculation of FCS (frame data redundancy check code)

In order to reduce the bit error rate of serial communication, the data must be checked at both the receiving end and the sending end, and the common method is to carry out FCS check. When calculating the redundancy check of the frame data, the bit XOR operation should be performed on the ASCII code of each character in the frame data, and then the result is converted into two hexadecimal characters.

5 C + + Builder API function application

5.1 design framework of communication main program

The main function of communication main program is to realize the operation control and status monitoring of PLC by computer, that is to form a closed-loop monitoring system. The program design architecture is shown in Figure 5.

How to use C + + Builder API function to write serial communication control program

5.2 open the serial port

(1) Open the communication port and initialize the port. The workflow is shown in Figure 6.

How to use C + + Builder API function to write serial communication control program

(2) Open the communication port program source code:

void__ fastcall tform1::button1click(tobject *sender)

{

char *comno;

dcb dcb;

string temp;

temp=“com”+inttostr(rdcom-》itemindex+1);

comno=temp.c_ str() ;

hcomm=createfile(comno,generic_ read|generic_ write,

0,null,open_ exisTIng,1,0);

if(hcomm==invalid_ handle_ value)

{

Message box (0, “error opening communication port, please check whether the port is occupied!”“ comm error”,mb_ ok);

return;

}

getcommstate(hcomm,&dcb);

dcb.baudrate=cbr_ 9600;

dcb.bytesize =7;

dcb.parity =evenparity;

dcb.stopbits =onestopbit;

setcommstate(hcomm,&dcb);

if(! setcommstate(hcomm,&dcb))

{

Message box (0, “communication port setting error!!!”“ set error”,mb_ ok);

closehandle(hcomm);

return;

}

}

5.3 write PLC memory data

(1) The command sent by computer is written into PLC to realize the control function of computer to PLC. The workflow is shown in Figure 7.

How to use C + + Builder API function to write serial communication control program

(2) Write PLC memory function program source code:

string tform1::write(string address,string value)

{

unsigned long lrc,bs;

string temp;

char *senddata;

char inbuff[1024];

int ln,i=0;

string word,check;

dword nbytesread,dwevent,dwerror;

comstat cs;

word=“@00wd”+address+value;

if(hcomm==0)

{

Message box (0, “serial port is not opened!”“ Error message “, MB_ ok);

return(0);

}

temp=outchecksum(word);

senddata=temp.c_ str() ;

bs=strlen(senddata);

loop:

if(++i《=3)

{

writefile(hcomm,senddata,bs,&lrc,null);

sleep(100);

if(hcomm==invalid_ handle_ value) return(0);

clearcommerror(hcomm,&dwerror,&cs);

if(cs.cbinque》sizeof(inbuff))

{

purgecomm(hcomm,purge_ rxclear);

return(0);

}

readfile(hcomm,inbuff,15,&nbytesread,null);

check=inbuff;

if(check.substring(6,2)!=“ 00”)

{

goto loop;

}

}

else

{

Message box (0, “data write error”, “communication error”, MB)_ ok);

}

}

5.4 read PLC memory data

(1) Read data from PLC and monitor the operation data of PLC. The workflow is shown in Figure 8.

How to use C + + Builder API function to write serial communication control program

(2) Read PLC memory function program source code:

string tform1::read(string address,string value)

{

string readdata,readdata1,readdata2;

string temp;

unsigned long lrc,bs;

char *senddata;

int ln,i=0,len;

dword nbytesread,dwevent,dwerror;

comstat cs;

char inbuff[1024];

string word;

word=“@00rd”+address+value;

if(hcomm==0) return(0);

temp=outchecksum(word);

senddata=temp.c_ str();

bs=temp.length();

loop:

if(++i《=3)

{

writefile(hcomm,senddata,bs,&lrc,null);

sleep(100);

if(hcomm==invalid_ handle_ value) return(0);

clearcommerror(hcomm,&dwerror,&cs);

if(cs.cbinque》sizeof(inbuff))

{

purgecomm(hcomm,purge_ rxclear);

return(0);

}

cs.cbinque=4*strtoint(value)+11;

readfile(hcomm,inbuff,cs.cbinque,&nbytesread,null);

inbuff[cs.cbinque]=`\0`;

readdata =inbuff;

len=readdata.length();

if(len==0)

{

goto loop;

}

if(readdata.substring(6,2)!=“ 00”)

{

goto loop;

}

if(inchecksum(readdata)!= 1)

{

goto loop;

}

}

else

{

Message box (0, “read data error”, “communication error”, MB)_ ok);

}

return(readdata);

}

6. Concluding remarks

This paper focuses on how to use C + + Builder  API function to write in line with the computer and OMRON PLC serial communication protocol control program described, the project has been debugged and run.

Editor in charge: GT

Leave a Reply

Your email address will not be published. Required fields are marked *