rust-ethernet-ip 0.5.2

High-performance EtherNet/IP communication library for Allen-Bradley CompactLogix and ControlLogix PLCs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# Building the Rust EtherNet/IP Library


This guide covers building the enhanced Rust EtherNet/IP library with comprehensive data type support and C# wrapper integration.

## 📋 **Prerequisites**


### Rust Development Environment

- **Rust 1.70+** (latest stable recommended)
- **Cargo** (included with Rust)
- **Git** for version control

### C# Development Environment (Optional)

- **.NET 8.0 SDK** or later
- **Visual Studio 2022** or **Visual Studio Code** with C# extension

### System Requirements

- **Windows**: Windows 10/11 (x64)
- **Linux**: Ubuntu 20.04+ or equivalent
- **macOS**: macOS 11+ (Intel/Apple Silicon)

## 🔧 **Building the Rust Library**


### 1. Clone and Setup

```bash
git clone https://github.com/your-repo/rust-ethernet-ip.git
cd rust-ethernet-ip
```

### 2. Build for Development

```bash
# Debug build (faster compilation, includes debug symbols)

cargo build

# Run tests

cargo test

# Run examples

cargo run --example advanced_tag_addressing
cargo run --example data_types_showcase
```

### 3. Build for Production

```bash
# Release build (optimized, smaller binary)

cargo build --release

# Build with specific target

cargo build --release --target x86_64-pc-windows-msvc
```

### 4. Build Dynamic Library (for C# integration)

```bash
# Build as dynamic library (.dll on Windows, .so on Linux, .dylib on macOS)

cargo build --release --lib

# The output will be in:

# Windows: target/release/rust_ethernet_ip.dll

# Linux:   target/release/librust_ethernet_ip.so

# macOS:   target/release/librust_ethernet_ip.dylib

```

## 🏗️ **Building the C# Wrapper**


### 1. Copy the Rust Library

```bash
# Windows

copy target\release\rust_ethernet_ip.dll csharp\RustEtherNetIp\

# Linux/macOS

cp target/release/librust_ethernet_ip.so csharp/RustEtherNetIp/
# or

cp target/release/librust_ethernet_ip.dylib csharp/RustEtherNetIp/
```

### 2. Build the C# Project

```bash
cd csharp/RustEtherNetIp
dotnet build
```

### 3. Run C# Tests

```bash
cd ../RustEtherNetIp.Tests
dotnet test
```

### 4. Create NuGet Package

```bash
cd ../RustEtherNetIp
dotnet pack --configuration Release
```

## 🚀 **Quick Build Script**


Create a build script for your platform:

### Windows (build.bat)

```batch
@echo off
echo Building Rust EtherNet/IP Library...

echo.
echo [1/4] Building Rust library (release)...
cargo build --release --lib
if %errorlevel% neq 0 exit /b %errorlevel%

echo.
echo [2/4] Copying DLL to C# project...
copy target\release\rust_ethernet_ip.dll csharp\RustEtherNetIp\
if %errorlevel% neq 0 exit /b %errorlevel%

echo.
echo [3/4] Building C# wrapper...
cd csharp\RustEtherNetIp
dotnet build --configuration Release
if %errorlevel% neq 0 exit /b %errorlevel%

echo.
echo [4/4] Running tests...
cd ..\RustEtherNetIp.Tests
dotnet test --configuration Release
if %errorlevel% neq 0 exit /b %errorlevel%

echo.
echo ✅ Build completed successfully!
echo.
echo Outputs:
echo   Rust DLL: target\release\rust_ethernet_ip.dll
echo   C# DLL:   csharp\RustEtherNetIp\bin\Release\net8.0\RustEtherNetIp.dll
```

### Linux/macOS (build.sh)

```bash
#!/bin/bash

set -e

echo "Building Rust EtherNet/IP Library..."

echo
echo "[1/4] Building Rust library (release)..."
cargo build --release --lib

echo
echo "[2/4] Copying library to C# project..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    cp target/release/librust_ethernet_ip.so csharp/RustEtherNetIp/
elif [[ "$OSTYPE" == "darwin"* ]]; then
    cp target/release/librust_ethernet_ip.dylib csharp/RustEtherNetIp/
fi

echo
echo "[3/4] Building C# wrapper..."
cd csharp/RustEtherNetIp
dotnet build --configuration Release

echo
echo "[4/4] Running tests..."
cd ../RustEtherNetIp.Tests
dotnet test --configuration Release

echo
echo "✅ Build completed successfully!"
echo
echo "Outputs:"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    echo "  Rust Library: target/release/librust_ethernet_ip.so"
elif [[ "$OSTYPE" == "darwin"* ]]; then
    echo "  Rust Library: target/release/librust_ethernet_ip.dylib"
fi
echo "  C# DLL: csharp/RustEtherNetIp/bin/Release/net8.0/RustEtherNetIp.dll"
```

Make the script executable:
```bash
chmod +x build.sh
./build.sh
```

## 🔍 **Verification**


### 1. Verify Rust Build

```bash
# Check that all tests pass

cargo test

# Verify examples work

cargo run --example advanced_tag_addressing
cargo run --example data_types_showcase

# Check library exports (Windows)

dumpbin /exports target\release\rust_ethernet_ip.dll

# Check library exports (Linux)

nm -D target/release/librust_ethernet_ip.so

# Check library exports (macOS)

nm -D target/release/librust_ethernet_ip.dylib
```

### 2. Verify C# Integration

```bash
cd csharp/RustEtherNetIp
dotnet build --verbosity normal

# Check that the native library is found

dotnet run --project Program.cs
```

### 3. Test FFI Functions

The C# wrapper should be able to call all these Rust FFI functions:
- `eip_connect`
- `eip_disconnect`
- `eip_read_bool`, `eip_write_bool`
- `eip_read_sint`, `eip_write_sint`
- `eip_read_int`, `eip_write_int`
- `eip_read_dint`, `eip_write_dint`
- `eip_read_lint`, `eip_write_lint`
- `eip_read_usint`, `eip_write_usint`
- `eip_read_uint`, `eip_write_uint`
- `eip_read_udint`, `eip_write_udint`
- `eip_read_ulint`, `eip_write_ulint`
- `eip_read_real`, `eip_write_real`
- `eip_read_lreal`, `eip_write_lreal`
- `eip_read_string`, `eip_write_string`
- `eip_read_udt`, `eip_write_udt`
- `eip_discover_tags`
- `eip_get_tag_metadata`
- `eip_set_max_packet_size`
- `eip_check_health`

## 🐛 **Troubleshooting**


### Common Build Issues


#### Rust Compilation Errors

```bash
# Update Rust toolchain

rustup update

# Clean and rebuild

cargo clean
cargo build --release
```

#### Missing Dependencies

```bash
# Install required system packages (Ubuntu/Debian)

sudo apt-get update
sudo apt-get install build-essential pkg-config

# Install required system packages (CentOS/RHEL)

sudo yum groupinstall "Development Tools"
sudo yum install pkgconfig

# Install required system packages (macOS)

xcode-select --install
```

#### C# Build Issues

```bash
# Restore NuGet packages

dotnet restore

# Clean and rebuild

dotnet clean
dotnet build --configuration Release
```

#### Native Library Loading Issues


**Windows:**
- Ensure `rust_ethernet_ip.dll` is in the same directory as the C# executable
- Check that Visual C++ Redistributable is installed
- Verify the DLL architecture matches (x64 vs x86)

**Linux:**
- Ensure `librust_ethernet_ip.so` is in the library path
- Set `LD_LIBRARY_PATH` if needed:
  ```bash
  export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./csharp/RustEtherNetIp

  ```

**macOS:**
- Ensure `librust_ethernet_ip.dylib` is in the library path
- Set `DYLD_LIBRARY_PATH` if needed:
  ```bash
  export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:./csharp/RustEtherNetIp

  ```

## 📦 **Distribution**


### Creating Release Packages


#### Rust Library Only

```bash
# Create source distribution

cargo package

# Publish to crates.io (if configured)

cargo publish
```

#### C# NuGet Package

```bash
cd csharp/RustEtherNetIp

# Create NuGet package

dotnet pack --configuration Release --output ./nupkg

# Publish to NuGet.org (if configured)

dotnet nuget push ./nupkg/*.nupkg --api-key YOUR_API_KEY --source https://api.nuget.org/v3/index.json
```

#### Complete Distribution

```bash
# Create a complete distribution with both Rust and C# components

mkdir -p dist/rust-ethernet-ip-v0.3.0

# Copy Rust artifacts

cp target/release/rust_ethernet_ip.dll dist/rust-ethernet-ip-v0.3.0/
cp target/release/rust_ethernet_ip.lib dist/rust-ethernet-ip-v0.3.0/

# Copy C# artifacts

cp csharp/RustEtherNetIp/bin/Release/net8.0/RustEtherNetIp.dll dist/rust-ethernet-ip-v0.3.0/
cp csharp/RustEtherNetIp/bin/Release/net8.0/RustEtherNetIp.xml dist/rust-ethernet-ip-v0.3.0/

# Copy documentation

cp README.md dist/rust-ethernet-ip-v0.3.0/
cp CHANGELOG.md dist/rust-ethernet-ip-v0.3.0/
cp csharp/RustEtherNetIp/README.md dist/rust-ethernet-ip-v0.3.0/CSharp-README.md

# Create archive

cd dist
tar -czf rust-ethernet-ip-v0.3.0.tar.gz rust-ethernet-ip-v0.3.0/
```

## 🎯 **Performance Optimization**


### Rust Build Optimizations

```toml
# Add to Cargo.toml for maximum performance

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"
```

### C# Build Optimizations

```xml
<!-- Add to .csproj for maximum performance -->

<PropertyGroup Condition="'$(Configuration)'=='Release'">
  <Optimize>true</Optimize>
  <DebugType>none</DebugType>
  <DebugSymbols>false</DebugSymbols>
  <TrimMode>link</TrimMode>
</PropertyGroup>
```

## 📊 **Build Metrics**


Expected build times on modern hardware:

| Component | Debug Build | Release Build | Notes |
|-----------|-------------|---------------|-------|
| Rust Library | 30-60s | 2-5 minutes | First build includes dependencies |
| C# Wrapper | 5-10s | 10-20s | Includes XML documentation |
| Full Test Suite | 10-30s | 30-60s | Includes integration tests |
| Complete Build | 1-2 minutes | 3-6 minutes | All components + tests |

## 🔄 **Continuous Integration**


### GitHub Actions Example

```yaml
name: Build and Test

on: [push, pull_request]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Install Rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        override: true
    
    - name: Install .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: '8.0.x'
    
    - name: Build Rust Library
      run: cargo build --release --lib
    
    - name: Run Rust Tests
      run: cargo test
    
    - name: Copy Native Library
      shell: bash
      run: |
        if [[ "${{ runner.os }}" == "Windows" ]]; then
          cp target/release/rust_ethernet_ip.dll csharp/RustEtherNetIp/
        elif [[ "${{ runner.os }}" == "Linux" ]]; then
          cp target/release/librust_ethernet_ip.so csharp/RustEtherNetIp/
        else
          cp target/release/librust_ethernet_ip.dylib csharp/RustEtherNetIp/
        fi
    
    - name: Build C# Wrapper
      run: dotnet build csharp/RustEtherNetIp --configuration Release
    
    - name: Test C# Wrapper
      run: dotnet test csharp/RustEtherNetIp.Tests --configuration Release
```

This comprehensive build guide should help you successfully build and deploy the enhanced Rust EtherNet/IP library with full C# integration!