Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion snap7/low_level/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ The native Python S7 client provides a pure Python implementation for communicat
- **S7Client** (`s7_client.py`) - Main client class for connecting to S7 PLCs
- **S7Protocol** (`s7_protocol.py`) - Protocol definitions, constants, and data conversion utilities
- **S7Socket** (`s7_socket.py`) - TCP socket handling and communication
- **S7Server** (`s7_server.py`) - Basic S7 server implementation for testing
- **S7Server** (`s7_server.py`) - S7 server implementation for PLC simulation
- **S7Partner** (`s7_partner.py`) - S7 partner implementation for peer-to-peer communication
- **S7IsoTcp** (`s7_isotcp.py`) - ISO TCP protocol layer (partial implementation)

### Key Features
Expand Down Expand Up @@ -238,6 +239,64 @@ To extend the implementation:
4. **Optimize Performance** - Add connection pooling, async operations
5. **Add Testing** - Create tests with real PLC hardware

### Using S7Server

```python
from snap7.low_level.s7_server import S7Server

# Create and start server
server = S7Server()

# Register a data block
db_data = bytearray(1024)
server.register_area(0x84, 1, db_data) # DB1 with 1024 bytes

# Start server on port 102
error = server.start("0.0.0.0", 102)
if error == 0:
print("Server started successfully!")

# Get server status
server_status, cpu_status, clients_count = server.get_status()
print(f"Server Status: {server_status}, CPU: {cpu_status}, Clients: {clients_count}")

# Server runs in background thread
# ... do other work ...

# Stop server
server.stop()
```

### Using S7Partner

```python
from snap7.low_level.s7_partner import S7Partner

# Create active partner (initiates connection)
partner = S7Partner(active=True)

# Connect to remote partner
error = partner.start_to("0.0.0.0", "192.168.1.100")
if error == 0:
print("Connected to partner!")

# Send data synchronously
partner.send_buffer = bytearray(b"Hello Partner!")
partner.send_size = 14
error = partner.b_send()

# Receive data synchronously
error = partner.b_recv()
if error == 0:
print(f"Received: {partner.recv_buffer[:partner.recv_size]}")

# Or send asynchronously
partner.as_b_send()
status, result = partner.check_as_b_send_completion()

partner.stop()
```

## Contributing

When contributing to the native S7 client:
Expand Down
20 changes: 20 additions & 0 deletions snap7/low_level/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Low-level native Python S7 implementations.

This package contains pure Python implementations of S7 protocol components,
without dependencies on native libraries.
"""

from .s7_client import S7Client
from .s7_server import S7Server
from .s7_partner import S7Partner
from .s7_protocol import S7Protocol
from .s7_socket import S7Socket

__all__ = [
'S7Client',
'S7Server',
'S7Partner',
'S7Protocol',
'S7Socket',
]
14 changes: 14 additions & 0 deletions snap7/low_level/s7_consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
S7 Protocol Constants.
Used by ISO TCP and other low-level components.
"""


class S7Consts:
"""Constants for S7 protocol implementation."""

# ISO TCP constants
isoTcpPort = 102 # Standard S7 port
MaxIsoFragments = 64 # Maximum number of ISO fragments
IsoPayload_Size = 4096 # ISO telegram buffer size
noError = 0 # No error constant
Loading