Current location - Trademark Inquiry Complete Network - Tian Tian Fund - Realization of communication between PLC and upper computer with VB
Realization of communication between PLC and upper computer with VB
It can be done through OPC server, which is more convenient. It can be applied to all kinds of PLC.

Here, taking Siemens PLC as an example, the implementation method is as follows:

1. Establish a new project OPC through STEP7 software, and add SIMATIC 300 station and a PC station to the project, in which CP343 module is installed in the fourth slot of PLC station. The first slot of PC station is OPC server, and the third slot is IE universal, and then the network connection between PLC and PC station is configured under NETPRO: (Note: it is a new network connection on PC station, and the connection type is S7 connection).

Second, configure the PC station of this machine through the station configurator software (the same as that configured in STEP7), and then download the PLC and PC station at the same time (note: when downloading the PC station, connect S7 online (step 7)->; Inside PC (local).

Third, open OPC Scout, (start >; SIMATIC & gt; SIMATICNet & gt; Opscout), double-click &; Ldquo (same as OligomericProanthoCyanidins) proanthocyanidins

Siemens Networks; Rdquo pops up in the following &; In the ldquo Group & amprdquo dialog box, enter the group and. In this case, it is named &; ldquoOPC & amprdquo。

Click OK to confirm;

; Double-click the added connection group (OPC), and then set the&; LdquoOPC Navigator & amprdquo dialog box, which displays

All the connection protocols. Double-click &; LdquoS7 & amprdquo, the connection name built in NetPro of PC station configuration will be displayed (S7

Connection_ 1). Double-click this connection and an accessible object tree will appear. In PLC,

Database blocks that already exist in the CPU will also appear.

The configuration of OPC server is completed above.

Fourthly, the OPC client application program is written in VB. The main program code is as follows:

Siemens opc daautomation 2.0 should also be quoted in VB.

(1) connect to the OPC server.

Dimwithevents MyOPCServer as OPC server' defines the server object variable my OPC server.

Dimwithevents MyOPCGroup as OPC group' defines object variable myopcgroup OPC group.

Set MyOPCServer = new OPCServer.

My OPC server. Connect OPC. SIMATICNET "'connects to OPC server of SIMATIC NET.

(2) Add OPC group objects

Set MyOPCGroup=MyOPCServer. OPCGroups.Add("Group 1 ")

Add OPC group object

MyOPCGroupissued = True

Set this set of data as background refresh.

MyOPCGroupIsActive = True

Make this group active.

MyOPCGroup update rate = 1000

Set the data refresh time to 1000.

(3) Add data items

Dimbits () as a string.

Project identifier

Dim abClientHandles () as long.

Client handle

Dim abServerHandles () as long.

Server side handle

Dimaberrs () as long.

Dim as long as I am

ItemCount=3

AbItemIDs( 1) = "S7:S7 connection _ 1IB 1"

AbItemIDs(2) = "S7:S7 connection _ 1MB 1"

AbItemIDs(3) = "S7:S7 connection _ 1QB 1"

For i= 1 to 3

abClientHandles(i) = i

Assign a value to a client handle

then

MyOPCGroupOPCItems.AddItems ItemCount,abItemIDs,abClientHandles,abServerHandles,abErrors

Add data item operation

(4) Synchronous data reading and writing

There are two modes of OPC data access: synchronous mode and asynchronous mode. Asynchronous reading and writing data is complicated and needs to be combined with events. Compared with synchronization, it is slow but accurate. Synchronous reading and writing data is very simple, just use the method of OPCItem directly.

Dim a as an action item.

Dim index is Long.

The index is the tag serial number.

Dim OneRead As String

Sad as a string

Set One = MyOPCGroup。 Operation Item (Index)

One. Read OPCCache

OneRead = One。 value

sense data

MyOPCGroupSyncWrite project count, server handle, no value, error

Synchronous write data

MyOPCGroupSyncRead OPCCache,ItemCount,ServerHandles,ReadValue,Errors

Synchronous reading data

One. Write (thank you)

read-in data

If you only read data, you can use the DataChange event, which will be triggered when the data to be accessed in the controller changes, and the data will be automatically read into the TxtValue text box.

Private sub myopcgroup _ data change (byval transaction id is Long, ByVal NumItems is Long, ClientHandles () is Long, ItemValues () is Variant, qualifications () is Long, TimeStamps () is Date).

Automatically refresh data

Dim as long as I am

For i = 1 to NumItems

txt value(client handles(I))= item values(I)

Gets the value of the item.

TxtTime(ClientHandles(i)) = timestamp (I)

Gets the timestamp of the project.

txt quantity(client handles(I))= GetQualityString(Qualities(I))

Get the quality of the project

Next, I

End joint

(5) Disconnect the OPC server.

MyOPCServer。 OPCGroups.RemoveAll

Delete all OPC groups to make room for resources.

Set MyOPCGroup = Nothing.

MyOPCServer。 separate

separate