This project contains three different EtherCAT Master applications for TwinCAT3, each demonstrating different approaches and capabilities.
π For development context, architectural decisions, and troubleshooting insights, see DEVELOPMENT_LOG.md
Output: EtherCATMaster_Basic.exe
Features:
- Connect to TwinCAT3 runtime via ADS
- Read system information and version
- Monitor TwinCAT status
- Basic cyclic operations template
- Error handling for ADS operations
Limitations:
- β Does NOT control EtherCAT states
- β Cannot get EtherCAT to OP mode
- Only monitors existing system state
Output: EtherCATMaster_StateAware.exe
Features:
- All features of Basic Master PLUS:
- Control TwinCAT system states (IDLE β RUN β STOP)
- Monitor EtherCAT master and slave states
- Interactive command interface
- Comprehensive state reporting
Key Capability:
- β CAN attempt to get EtherCAT to OP mode
- β Can start/stop TwinCAT system
- Automatically transitions EtherCAT when TwinCAT starts
Output: EtherCATMaster_Direct.exe
Features:
- Network adapter enumeration and selection
- Shows direct EtherCAT programming concepts
- Demonstrates Ethernet port configuration
- Educational example of EtherCAT requirements
Purpose:
- π Educational demonstration only
- Shows how direct EtherCAT masters work
- Not a functional EtherCAT master
- TwinCAT3 installed and running
- Visual Studio 2019 or later with C++ development tools
- TwinCAT3 SDK (included with TwinCAT3 installation)
EtherCATMaster/
βββ π Source Files
β βββ main.cpp # Basic EtherCAT Master
β βββ EtherCATStateMaster.cpp # State-Aware EtherCAT Master
β βββ DirectEtherCATMaster.cpp # Direct EtherCAT Demo
βββ π§ Build Files
β βββ EtherCATMaster.vcxproj # Visual Studio project
β βββ BasicMaster.vcxproj # Basic master project
β βββ CMakeLists.txt # CMake configuration
βββ ποΈ Build Scripts
β βββ build.cmd # Single app builder (basic only)
β βββ build_all_simple.cmd # ALL 3 apps builder
β βββ build_individual.cmd # Individual app selector
β βββ IdeStart.cmd # Visual Studio launcher
βββ π Configuration
β βββ ethercat_config.xml # EtherCAT config example
βββ π Documentation
βββ README.md # This file
# Build all 3 applications at once (RECOMMENDED)
build_all_simple.cmd
Output: EtherCATMaster_Basic.exe
, EtherCATMaster_StateAware.exe
, EtherCATMaster_Direct.exe
Script | Builds | Output Files | Use Case |
---|---|---|---|
build_all_simple.cmd |
All 3 apps | *_Basic.exe , *_StateAware.exe , *_Direct.exe |
Recommended |
build.cmd |
Basic only | EtherCATMaster.exe |
Single app development |
build_individual.cmd |
Your choice | Custom names | Selective building |
IdeStart.cmd |
Opens VS | N/A | IDE development |
# Build only the basic EtherCAT master
build.cmd
# Build specific application
build_individual.cmd basic # Basic master only
build_individual.cmd state # State-aware only
build_individual.cmd direct # Direct demo only
build_individual.cmd all # All three
# Build with options
build.cmd debug # Debug build
build.cmd release x86 # 32-bit release
build_all_simple.cmd # All apps, release x64
# Launch Visual Studio IDE
IdeStart.cmd
Then: Use F7 or Build β Build Solution
mkdir build && cd build
cmake ..
cmake --build . --config Release
# Basic master
MSBuild BasicMaster.vcxproj /p:Configuration=Release /p:Platform=x64
# Original project (main.cpp)
MSBuild EtherCATMaster.vcxproj /p:Configuration=Release /p:Platform=x64
- Start TwinCAT3: Make sure TwinCAT3 is running
- EtherCAT Configuration: EtherCAT Master configured in System Manager
- PLC Project: Optional - only needed for traditional PLC-based approach
- β Standalone C++ Master: No PLC project required
- βΉοΈ PLC-based approach: Load a PLC project (even empty)
- Administrator Rights: Run as administrator for TwinCAT control
# Navigate to build output
cd x64\Release
# Run Basic EtherCAT Master (monitoring only)
.\EtherCATMaster_Basic.exe
# Run State-Aware Master (can control EtherCAT states)
.\EtherCATMaster_StateAware.exe
# Run Direct EtherCAT Demo (educational)
.\EtherCATMaster_Direct.exe
Application | Can Get to OP Mode? | Use Case |
---|---|---|
EtherCATMaster_Basic.exe |
β NO | Monitor existing system |
EtherCATMaster_StateAware.exe |
β YES | Control and manage EtherCAT |
EtherCATMaster_Direct.exe |
π Educational | Learn EtherCAT concepts |
- Press
q
+ Enter to quit - Automatic monitoring loop
- Press
s
to start EtherCAT system - Press
r
to read status - Press
q
to quit - Interactive commands for system control
- Shows network adapters automatically
- Press any key to continue through demos
- Educational information display
The application provides:
- Connection Management: Establishes ADS connection to TwinCAT3
- System Information: Displays TwinCAT version and device information
- Monitoring Loop: Simulates cyclic EtherCAT operations
- Error Handling: Basic error handling for ADS operations
- Clean Shutdown: Proper disconnection when exiting
-
"Failed to open ADS port"
- Ensure TwinCAT3 is installed and running
- Check that the ADS service is running
-
"ADS connection failed"
- Verify TwinCAT is in RUN mode
- Check that a PLC project is loaded
- Ensure AMS NetID is correct (127.0.0.1.1.1 for local)
-
Compilation Errors
- Verify TwinCAT3 SDK paths in project settings
- Check that TcAdsDll.lib is accessible
- Ensure include paths point to TwinCAT SDK headers
You can check TwinCAT status using:
# Check if TwinCAT service is running
Get-Service | Where-Object {$_.Name -like "*TwinCAT*"}
# Check ADS router
netstat -an | findstr :48898
This basic application can be extended to:
- Read/Write PLC Variables: Add functions to communicate with specific PLC variables
- EtherCAT Device Management: Add functions to manage EtherCAT slaves
- Real-time Operations: Implement real-time cyclic operations
- Configuration Management: Add XML configuration file support
- Logging: Add structured logging capabilities
The main components:
- EtherCATMaster Class: Encapsulates ADS communication
- Connect(): Establishes connection to TwinCAT
- ReadVariable()/WriteVariable(): Template for PLC variable access
- PrintSystemInfo(): Displays system information
- Main Loop: Demonstrates cyclic operation pattern
Will NOT get EtherCAT to OP mode automatically.
The basic application:
- Only connects via ADS to TwinCAT
- Assumes TwinCAT and EtherCAT are already configured
- Monitors whatever state TwinCAT is already in
- Does not control EtherCAT states
CAN attempt to get EtherCAT to OP mode.
To build the state-aware version:
- Exclude
main.cpp
from build - Include
EtherCATStateMaster.cpp
in build - Or create a separate project
The state-aware application:
- Can start/stop TwinCAT system
- Monitors EtherCAT master and slave states
- Attempts to transition EtherCAT to OP mode
- Provides interactive control commands
For a standalone C++ application without PLC:
-
TwinCAT System Configuration:
- EtherCAT Master configured in System Manager
- Ethernet adapter assigned to EtherCAT
- EtherCAT slaves scanned and configured
- Configuration activated
-
No PLC Required:
- β No PLC project needed
- β No PLC variables required
- β Your C++ app controls EtherCAT directly
- β Process data handled in your application
-
System State:
- TwinCAT in RUN mode (without PLC)
- All EtherCAT slaves responding
- No configuration errors
For a PLC-based system (traditional approach):
-
TwinCAT System Configuration:
- EtherCAT Master configured in System Manager
- Ethernet adapter assigned to EtherCAT
- EtherCAT slaves scanned and configured
- Configuration activated
-
PLC Program (if using PLC approach):
- PLC project with I/O mapping
- Variables linked to EtherCAT I/O
- Program compiled and downloaded
-
System State:
- TwinCAT in RUN mode
- PLC program running
- All EtherCAT slaves responding
Note: Your C++ applications can work with either approach - they connect via ADS regardless of whether a PLC is present.
- Open TwinCAT System Manager
- Add EtherCAT Master device
- Assign Ethernet adapter to EtherCAT
- Scan for EtherCAT slaves
- Configure I/O mapping (Process Data)
- Skip PLC creation - your C++ app will handle process data
- Activate configuration
- Set TwinCAT to RUN mode
- Now EtherCAT will go to OP mode
- Open TwinCAT System Manager
- Add EtherCAT Master device
- Assign Ethernet adapter to EtherCAT
- Scan for EtherCAT slaves
- Configure I/O mapping
- Create PLC project with I/O variables
- Link PLC variables to EtherCAT I/O
- Activate configuration
- Set TwinCAT to RUN mode
- Now EtherCAT will go to OP mode
Key Difference: In standalone mode, your C++ application accesses process data directly via ADS instead of through PLC variables.
This is example code for educational purposes.