The use of Delphi to develop industrial PC control system software has become the choice of more and more industrial control developers, and how to quickly and easily realize the serial port data acquisition becomes the key to the problem. This paper focuses on the implementation of Delphi6 and Comport communication components. The AI ​​meter collects real-time data and processes the collected data accordingly.
Abstract: The use of Delphi to develop industrial PC control system software has become the choice of more and more industrial control developers, and how to quickly and easily realize the serial port data acquisition becomes the key to the problem. This paper focuses on the implementation of Delphi6 and Comport communication components. Real-time data acquisition of multiple AI instruments, and corresponding processing of the collected data.

Keywords: serial communication; Delphi6.0; Comport control

1 Introduction

Recently, with the development of modern information technology, computer serial communication technology has become increasingly mature. Due to the fast processing speed, strong analysis capability and flexible use of the microcomputer, the separate secondary instrument can only meet the production needs as the display control function. More is to collect and record the useful data on the site as production analysis and archive. The PC monitoring system using PC as the instrument function expansion has been widely adopted in the field of industrial control.
The serial communication between the PC and the instrument can be implemented by high-level language programming, such as Delphi, VC, VB, etc. Delphi is a new generation of object-oriented visual programming tools that are powerful, easy to use, fast to execute, and rich in third-party controls, making the development process as easy to use as building blocks, especially for us. Some people who work with industrial control do not have a particularly deep programming foundation. The lower computer meter adopts the Yudian AI series instrument with 485 communication function. Because the AI ​​instrument of Yudian uses the AIBUS communication protocol, it has the characteristics of simple communication command and fast communication speed, allowing the upper computer to read and write the data of the instrument. The RS485 bus function can realize serial communication of multiple meter data.
2 Comport serial communication component introduction DelphiI implementation of serial communication, commonly used methods are: use controls, such as Mscomm and Comport control; use the API function to read and write serial port. Using the API method is more suitable for writing more complex low-level communication programs, but the disadvantage is that writing serial communication programs is more complicated and requires a lot of communication and programming knowledge. With the serial port control, we can ignore its internal functions, mainly call its related properties and events, and can implement various operations on the serial port, and the programming is simple, usually strong, and portable. In the Delphi serial communication we choose to use the Comport control, which is directly compiled in the development project file, no need to install additional controls on the running PC. After installing the Comport component to DELPHI, you can see that there is one more component page of Cportlib. There are several components under Cportlib. The main application here is the Comport component. The operations of the Comport control mainly include: port setting, opening port, and The port writes the instruction, reads the received data, and closes the port.

3 Yudian AIBUS communication protocol description

AIBUS is a communication protocol developed by Xiamen Yudian Automation Technology Co., Ltd. for AI series display control instrument. It can realize powerful functions with simple instructions. AIBUS adopts 16-bit summation correction code, reliable communication, support 4800, 9600, 19200 A variety of baud rates, the instrument allows continuous write parameters, write setpoints or output values, AI series meters use asynchronous serial communication interface, interface level meets the requirements of RS232C or RS485 standards. The data format is 1 start bit, 8 bits of data, no parity, 1 or 2 stop bits. The AI ​​instrument adopts multi-machine communication protocol, and adopts RS485 communication interface, then 32 instruments can be connected to one communication interface at the same time. The AI ​​meter uses a hexadecimal data format to represent various instruction codes and data. The AI ​​instrument software communication instruction is optimized. There are only two standard communication commands. One is the read command and the other is the write command. The two commands make the PC software easy to write, but can operate the instrument 100% completely. The read and write instructions are as follows:
Read: Address code +52H (82) + parameter code to be read +0+0 + check code write: Address code +43H (67) + parameter code to be written + write number low byte + high write number Byte + check code address code: In order to connect multiple AI instruments on one communication interface, each AI instrument needs to be programmed with a different communication address. The AI ​​instrument communication protocol stipulates that the address code is two identical bytes and the value is (meter address +80H). For example, if the instrument parameter Addr=10 (hexadecimal number is 0AH, 0A+80H=8AH), the address code of the instrument is: 8AH 8AH
Check code: The check code adopts the 16-bit summation check mode, wherein the check code calculation method of the read command is: the code of the parameter to be read ×256+82+ADDR
For example, to read the data of parameter 0 of address 1, it is 0*256+82+1=83; it is 53 00 into 16 system;
If it is the data of reading OC, it is decimal (C) * 256 + 82 + 1 = (sixteen) C53, the check code is 53 0C;
The check code calculation method of the write command is the remainder calculated by the 16-bit binary addition method (the overflow part is not processed): the parameter code to be written × 256 + 67 + the parameter value to be written + ADDR
Return data: Whether reading or writing, the meter returns the following 10 bytes of data, measured value PV + set value SV + output value MV and alarm status + read / write parameter value + check code; PV, SV and The read parameter values ​​each occupy 2 bytes, representing a 16-bit binary signed complement integer. The lower byte is first, the upper byte is after, the integer cannot represent the decimal point, and the user is required to process in the upper computer; the MV occupies one word. Section, in 8-bit signed binary number format, the value range is -110~+110, the status bit occupies one byte, and the check code occupies 2 bytes for a total of 10 bytes.

4 Comport control and multiple AI instrument communication implementation

4.1 Hardware configuration The hardware configuration is mainly composed of a 485 bus system consisting of a host PC with a serial port, a 485 to 232 converter and an AI meter. The AI ​​meter sets different communication addresses, and the baud rate is set to 9600. As shown below:
Bus structure
China Hardware Business Network



4.2 Implementation of the software communication program Communication between the Comport control and the AI ​​instrument is carried out in a question-and-answer manner. We should use the polling communication method when communicating with multiple instruments at the same time. First we need to initialize the serial port in DELPHI, and then use the read and write commands to communicate with each instrument poll. After the communication ends, you need to close the serial port and release the serial port resources. The main implementation process is as follows:
4.2.1 Initialize the serial port and open the serial port. Select the serial port that communicates with AI. The serial port cannot be referenced by other programs at the same time. Determine the communication protocol, such as baud rate, data bit, stop bit and check mode. Open the serial port. Initialization can be defined in the property page of Comport, or it can be dynamically set while the program is running. The static definition in the property page is:
BaudRate: br9600 / / define the baud rate
DataBits: dbEight / / 8 bit data bits
Port: COM1//This example uses serial port 1
StopBits: sbOneStopBit / / 1 stop bit...
Additional properties can be found in Comport's online help file. If you want to modify the settings of the serial port parameters during the dynamic operation of the program, you can directly call the port settings window function of Comport in the program: Comport. ShowSetupDialog. Set the serial port properties in the pop-up settings window. After completing the initial setting of the serial port, you can open the serial port and AI communication. Open the serial port with the following command:
Comport.open;//Open the serial port
4.2.2 Data Conversion Function According to the communication protocol of AI instrument, the host computer communicates with AI in hexadecimal manner, so we have to do hexadecimal format conversion in the program, mainly to achieve The function is as follows:
Function HexStrToStr(const S:string):string;
//The hexadecimal character is converted to a string. Convert the hexadecimal format characters to be sent to hexadecimal format
Var
t:Integer;
Ts:string;
M, Code: Integer;
Begin
t:=1;
Result:='';
While t begin
While not (S[t] in ['0'..'9','A'..'F','a'..'f']) do
Inc(t);
If (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'] )) then
Ts:='$$$$$$$$$+S[t]
Else
Ts:='$$$$$$$$'+S[t]+S[t+1];
Val(ts,M,Code);
If Code=0 then
Result:=Result+Chr(M);
Inc(t,2);
End;
End;
Function StrToHexStr(const S:string):string;
// The string is converted to a hexadecimal string, and the number of characters received is converted to a hexadecimal number.
Var
I: Integer;
Begin
For I:=1 to Length(S) do
Begin
If I=1 then
Result:=IntToHex(Ord(S[1]),2)
Else Result:=Result+' '+IntToHex(Ord(S[I]),2);
End;
End;

Function Hex2Dec(Hexs: string): string; //16 system to ten system
Var
i,j: integer;
Res,base: LongWord;
Begin
Res := 0;
For i:=1 to Length(Hexs) do
Begin
Base := 1;
For j:=1 to Length(Hexs)-i do
Base := base * 16;
Case Hexs[i] of
'0'..'9': res := res + (Ord(Hexs[i]) - Ord('0')) * base;
'a'..'f': res := res + (Ord(Hexs[i]) - Ord('a') + 10) * base;
'A'..'F': res := res + (Ord(Hexs[i]) - Ord('A') + 10) * base;
End;
End;
Result := inttostr(res);
End;
4.2.3 Sending and Receiving Data The host computer communicates with multiple AIs, following the polling method of question-and-answer. The data read is free of decimal places, so we have to send the command to read decimal places. At the same time, it is also possible to return the measured value. We assume that the main program that communicates with the four AI meters via the 485 bus is implemented as follows:
Procedure RXdata;
Var
i:integer;
Dot, dot2, dot3, dot4 : double;
Obj:PAsync;
Str, str2, a1, a2, a7, b1, b2, b7, c1, c2, c7, d1, d2, d7: string;
Begin
InitAsync(obj);
Try ///////////Read the decimal places and measured values ​​of the first channel address 1
ComPort.WriteStrAsync(HexStrToStr('81 81 52 0C 0 0 53 0C'), obj);
ComPort.WaitForAsync(obj);
Sleep(100);//waiting 100MS after sending the command to ensure the integrity of the data;
ComPort.ReadStr(Str, 20);//Read the receive buffer area by 20 bytes;
comport.ClearBuffer(true,true);//Clear the send area and the receive area to prepare for the next channel communication;
For I:=1 to Length(str) do//Process the received data
Begin
Str2:=IntToHex(Ord(Str[i]),2) ;
If i= 1 then a1:= str2;//low byte of the measured value
If i= 2 then a2 :=str2;//high byte of the measured value
If i=7 then a7 := str2;//number of decimal places
End;
Dot := Exp(ln(10)*(strtoint(hex2dec(a7))));
Dot :=strtofloat(hex2dec(a2+a1))/dot;//The high and low bytes are combined and converted into decimal numbers with a decimal point;
/ / Start reading the data of the second channel address two, the other channel method is consistent.
ComPort.WriteStrAsync(HexStrToStr('82 82 52 0C 0 0 54 0C'), obj); //Read the decimal point of address 0C;
ComPort.WaitForAsync(obj);
Sleep(100);
ComPort.ReadStr(Str, 20);
comport.ClearBuffer(true,true);

``````
//All four channels are read. If you want to read more address data, so on;
Finally
DoneAsync(obj);
End;
End;
At this point, the data acquisition of the AI ​​instrument is basically completed, and the above process can be automatically collected in the TIMMER control of Delphi. If you want to operate other parameters of the AI ​​instrument in the host computer, you can also refer to the communication protocol of the AI ​​instrument to write the program.
4.2.4 Closing the Serial Port During system development, you should pay attention to shutting down the serial port in time when the serial port is not used, and release system resources. Otherwise, other applications of the system may be affected. The code to close the serial port is as follows:
Comport.close;

5 Conclusion

Practice has proved that using the Comport control to develop serial and lower computer communication programs in DELPHI is flexible, convenient and efficient. At the same time, by using the function of chart curve and database in DELPHI, it is possible to collect real-time data and real-time curves in real-time production, which facilitates the analysis and archiving of the production process. Transfer relevant data to the database to integrate with the company's MIS system to achieve data sharing.


references:
1. Li Qingliang. 2006. SPCOMM control in the Delphi7.0 serial communication application. Microcomputer Information - Embedded SOC. 22 (5-2): 8-10.
2. Lin Feng, Wang Yuezhong. 2005. Design and implementation of intelligent lithium-ion battery management system. Microcomputer information. (3): 78-79.
3. Fan Yizhi, Chen Liyuan. 2002. VB and RS-232 serial communication control. China Youth Publishing House, 327-334.
Http://news.chinawj.com.cn Editor: (Hardware Business Network Information Center) http://news.chinawj.com.cn

HDPE Plastic Sheet

Color: White, Black, Red, Blue, Beige, Green, Grey, etc.

Thickness: 0.4-250mm

Size: 1220*2440/1000*2000/1500*3000 mm

Application: 

1) cutting and Chopping Board in abattoir, meat works and fish markets

2) spacers between steel plates

3) gears and rollers

4) wear strips

5) linings for chutes and hoppers

6) scraper blades

7) transfer tables

8) drag conveyor flights

PE Sheet

Chopping Board, Polyethylene Sheet, HDPE Sheet, PE Plastic Sheet,Good Grade Sheet

SHENZHEN XIONGYIHUA PLASTIC INSULATION LTD , https://www.xyhplastic.com