Connection - fbarresi/Sharp7 GitHub Wiki
These methods allow controlling the behaviour a Client Object.
| Function | Purpose |
|---|---|
| ConnectTo | Connects a S7Client object to a PLC |
| Connect | Connects a S7Client object to a PLC with implicit parameters |
| Disconnect | Disconnects a S7Client |
| SetConnectionType | Sets the connection type (PG/OP/S7Basic) |
| SetConnectionParams | Sets Address, Local and Remote TSAP for the connection |
ConnectTo
Description
Connects the client to the hardware at (IP, Rack, Slot) Coordinates.
Declaration
public int ConnectTo(String Address, int Rack, int Slot)
Parameters
| Name | Type | Note |
|---|---|---|
| Address | string | PLC IPV4 Address e.g.: "192.168.1.10" |
| Rack | int | see table |
| Slot | int | see table |
Return value
-
0 : The Client is successfully connected (or was already connected).
-
Other values : see the Errors Code List.
Rack and Slot
In addition to the IP Address, that we all understand, there are two other parameters that index the unit : Rack (0..7) and Slot (1..31) that you find into the hardware configuration of your project, for a physical component, or into the Station Configuration manager for WinAC.
There is however some special cases for which those values are fixed or can work with a default as you can see in the next table.
| PLC | Rack | Slot | |
|---|---|---|---|
| S7 300 CPU | 0 | 2 | almost always, but according to the hw config |
| S7 400 CPU | not fixed | not fixed | according to the hw config |
| WinAC CPU | not fixed | not fixed | according to the hw config |
| S7 1200 CPU | 0 | 0 or 1 | |
| S7 1500 CPU | 0 | 0 or 1 | |
| WinAC IE | 0 | 0 | according to the hw config |
Connect
Description
Connects the client to the PLC with the parameters specified in the previous call of ConnectTo() or SetConnectionParams().
Declaration
public int Connect()
Return value
- 0 : The Client is successfully connected (or was already connected).
- Other values : see the Errors Code List.
Remarks
This function can be called only after a previous of ConnectTo() or SetConnectionParams() which internally sets Address and TSAPs.
Disconnect
Description
Disconnects “gracefully” the S7Client from the PLC.
Declaration
public void Disconnect()
Remarks
This function can be called safely multiple times. After calling this function LastError = 0 and Connected = false.
SetConnectionType
Sets the connection resource type, i.e. the way in which the Clients connects to a PLC.
Declaration
public void SetConnectionType(short ConnectionType)
Parameters
| Name | Type | Note |
|---|---|---|
| ConnectionType | short | See the table |
Connection type table
| Connection Type | Value | Helper Const |
|---|---|---|
| PG | 0x01 | S7.PG |
| OP | 0x02 | S7.OP |
| S7 Basic | 0x03..0x10 | S7.S7_BASIC |
SetConnectionParams
Description
Sets internally (IP, LocalTSAP, RemoteTSAP) Coordinates.
Declaration
public void SetConnectionParams(String Address, int LocalTSAP, int RemoteTSAP)
Parameters
| Name | Type | Note |
|---|---|---|
| Address | string | PLC IPV4 Address e.g.: "192.168.1.10" |
| LocalTSAP | int | Local TSAP (PC TSAP) |
| RemoteTSAP | int | Remote TSAP (PLC TSAP) |
Remarks
This function must be called just before Connect().