1 Introduction
The field device layer contains a wide variety of devices , including sensors, starters, drivers, I/O components, transmitters, valves , and of course, field testing instruments. PLCs are an indispensable and ubiquitous part of the field device control layer, and how to easily interact with PLCs has become a new battleground for many manufacturers. This has led to the development of human-machine interface (HMI) software products, which simplify the control and operation of PLCs, making their application more convenient; however, they also share common drawbacks, such as generally high prices and poor customizability. VB, as a "primitive" programming language, undoubtedly has significant advantages in these two aspects. This article, using a simple case study and relatively basic concepts, discusses how to implement communication between a PC and a PLC using VB, which may be helpful for engineers new to the control field. In the field device layer, various devices can be connected to a computer, and the simplest way to automate this connection is through serial communication. VB provides serial communication controls that allow developers to create serial communication system programs. The following is a brief discussion of related issues.
2. Basic Concepts of Serial Communication
There are two types of data communication: parallel communication and serial communication. For example, if 8 bits of data need to be transmitted, parallel communication can transmit all 8 bits at a time, while serial communication can only transmit 1 bit at a time. Figure 1 illustrates the two different communication modes. There are also two commonly used serial communication methods: RS-232 and RS-485.
2.1 RS-232 serial communication
The signal level of RS-232 is relative to the reference ground, as shown in Figure 2. The transmitting end transmits data relative to reference ground terminal 1; the receiving end reproduces the data transmitted by the transmitting end relative to reference ground terminal 2.
2.2 RS-485 serial communication
When an RS-485 signal is transmitted, it is split into two lines, positive and negative. At the receiving end, the signals are subtracted to restore the original signal. If the original signal is represented as (DT), and the split signals as (D+) and (D-), then DT = (D+) - (D-). Similarly, the receiving end restores the original signal using the same formula. If the lines are interfered with, the signals on the two lines will be (D+) + Noise and (D-) + Noise, respectively. The restored signal at the receiving end is (DT) = [(D+) + Noise] - [(D-) + Noise], the same as before. Therefore, RS-485 can effectively prevent noise interference.
Basic Concepts of 3VB
VB (Visual Basic) is Microsoft's window software that provides many interface members. Objects, properties, events, and methods are four important interfaces. VB also provides many commonly used controls.
3.1 Label Control
3.2 Button Control
3.3 Timer Control
VB provides a large number of controls, which will not be discussed further here.
4. Serial Communication Based on MScomm Control
To achieve serial communication between VB and devices such as PLCs, the MScomm control is required. A detailed explanation of this control is necessary here. MScomm is a non-standard control, requiring the manual addition of several items. The steps are as follows.
1) Select the [Project] menu.
2) Select [Components]
4.1 MODBUS ASCII
Modbus comes in two versions: ASCII and RTU. ASCII uses a relatively simple format and checksum. This article uses the ASCII format and only explains the function codes used. For more information, please refer to the detailed specifications of the protocol.
1) Communication format
LRC algorithm: ADRH+CMDH+DATAH, then take the two's complement.
2) Function codes
05, Write to coil
01, Read coil status
06, Write to a single word register
4. Implementation of VB-PLC communication
The following example illustrates how field devices communicate with a PLC.
1) Control requirements: Control the start and stop of the PLC and display the running status (green for running, red for stopping); be able to control Y0 and Y1 with alternating buttons and display the status of Y0 and Y1 with indicator lights (green for running, red for stopping); be able to write values to the D256 and D512 registers.
2) Implementation approach: The PLC start/stop flag is M1072. Referring to the DVP protocol, the address is H0C30. Operate using function code 01. Similarly, the addresses of Y0 and Y1 are H0500 and H0501 respectively. Write FF00 for ON and 0000 for OFF, then operate using function code 05. The addresses of D256 and D512 are H1100 and H1200 respectively; operate using function code 06.
3) VB interface
Use buttons to control the start and stop of the PLC, the ON/OFF of Y0 and Y1, and the transmission of data written to D256 and D512; use Shape components as indicator lights to show the running status of the PLC and the status of Y; use timer components to continuously read the status of M1072 to determine the running status of the PLC; use MScomm controls to realize communication between the PC and the PLC.
4) Code structure for programming implementation
(1) Implementation of LRC algorithm verification
PublicFunctionLRC(strAsString)AsString
c=0
l = Len(str)
Forc = c + 1Tol
c_data=Mid$(str , c , 2)
d_lrc=d_lrc+Val("&H"+c_data)
c = c + 1
Nextc
Ifd_lrc>&HFFThen
d_lrc=d_lrcMod&H100
EndIf
h_lrc=Hex(&HFF-d_lrc+1)
If Len(h_lrc)>2Then
h_lrc=Mid(h_lrc , Len(h_lrc)-1 , 2)
EndIf
LRC=h_lrc
EndFunction
(2) At the start of operation, the status of the PLC is determined and the flag is set.
'On first run, open the serial port and display the PLC running status.'
PrivateSubForm_Load()
Dims1AsString
Dims2AsString
Dims22AsString
Dims3AsString
Dims4AsString
MSComm1.PortOpen =True
s2="01010C300001"
s22=LRC(s2)
s1=" : "+s2+s22+Chr$(13)+Chr$(10)
MSComm1.Output =s1
s3=MSComm1 . Input
s4=Mid$(s , 6 , 8)
If s4 = 0C30FF00, then
plc=1' PLC is the running flag.
Else
plc=0' PLC is a stop flag.
EndIf
EndSub
(3) The following section uses indicator lights to show the operating status of the PLC.
PrivateSubTimer5_Timer()
Dims1AsString
Dims2AsString
Dims22
Dims3AsString
Dims4AsString
s2="01010C300001"
s22=LRC(s2)
s1=" : "+s2+s22+Chr$(13)+Chr$(10)
MSComm1.Output =s1
s3=MSComm1 . Input
s4=Mid$(s3 , 8 , 2)
If s4 = 31, then
plc=1' PLC is the running flag.
Else : If s4 = "30", then plc = 0. 'PLC is the stop flag.'
EndIf
If plc=1, then
Label2 . Caption = " PLC is running ... "
Shape1 . FillColor=RGB(0 , 255 , 0)'green
Else
Label2 . Caption="PLC has stopped"
Shape1 . FillColor=RGB(255 , 0 , 0)'red
EndIf
EndSub
(4) PLC start-up and shutdown
'Start PLC'
PrivateSubstart_Click()
DimstroutAsString
Timer5.Enabled =False
str="00050C30FF00" 'M1072 is the PLC start/stop flag. Looking up the address table, M1072 is OC30.FF00 is the set flag.'
ON , 0000 means OFF.
These are all fixed formats; please memorize them.
LRCC=LRC(str)' Calculates the LRC checksum of str.
strout=" : "+str+LRCC+Chr$(13)+Chr$(10)' The data to be transmitted. 13 is D, 10 is A
MSComm1.Output =strout
Timer5.Enabled =True
EndSub
Stop PLC
PrivateSubstop_Click()
DimstroutAsString
Timer5.Enabled =False
str="00050C300000"
LRCC=LRC(str)
strout=" : "+str+LRCC+Chr$(13)+Chr$(10)
MSComm1.Output =strout
Timer5.Enabled =True
EndSub
The ON/OFF control of Y0 and Y1 is the same as the PLC start/stop control method, and the indicator lights also display the same information. The data writing operations for D256 and D512 are similar; other codes are not listed here due to space limitations.
5. Conclusion
VB is an easy-to-use programming language, greatly facilitating communication with serial devices. Delta PLCs use the standard and widely popular MODBUS protocol, providing a simplified platform for communication between the PLC and the monitoring computer. Since all Delta electromechanical products support the MODBUS protocol, mastering the VB-PLC communication process is equivalent to mastering the communication between a PC and all Delta electromechanical products.