rust-ethernet-ip 0.7.0

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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
use std::collections::HashMap;
use std::error::Error;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::time::{timeout, Duration};

// Data type constants for CompactLogix
#[derive(Debug, Clone, PartialEq)]
#[repr(u16)]
pub enum PlcDataType {
    Bool = 0x00C1,
    Dint = 0x00C4,
    Real = 0x00CA,
    String = 0x00D0,
}

#[derive(Debug, Clone, PartialEq)]
pub enum PlcDataTypeExt {
    Known(PlcDataType),
    Unknown(u16),
}

impl From<u16> for PlcDataTypeExt {
    fn from(value: u16) -> Self {
        match value {
            0x00C1 => PlcDataTypeExt::Known(PlcDataType::Bool),
            0x00C4 => PlcDataTypeExt::Known(PlcDataType::Dint),
            0x00CA => PlcDataTypeExt::Known(PlcDataType::Real),
            0x00D0 => PlcDataTypeExt::Known(PlcDataType::String),
            _ => PlcDataTypeExt::Unknown(value),
        }
    }
}

#[derive(Debug, Clone)]
pub enum PlcValue {
    Bool(bool),
    Dint(i32),
    Real(f32),
    String(String),
    Raw(Vec<u8>),
}

impl PlcValue {
    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            PlcValue::Bool(val) => vec![if *val { 0xFF } else { 0x00 }],
            PlcValue::Dint(val) => val.to_le_bytes().to_vec(),
            PlcValue::Real(val) => val.to_le_bytes().to_vec(),
            PlcValue::String(val) => {
                let mut bytes = vec![];
                let str_bytes = val.as_bytes();
                bytes.extend_from_slice(&(str_bytes.len() as u16).to_le_bytes());
                bytes.extend_from_slice(str_bytes);
                if bytes.len() % 2 != 0 {
                    bytes.push(0x00);
                }
                bytes
            }
            PlcValue::Raw(val) => val.clone(),
        }
    }

    pub fn get_data_type(&self) -> u16 {
        match self {
            PlcValue::Bool(_) => PlcDataType::Bool as u16,
            PlcValue::Dint(_) => PlcDataType::Dint as u16,
            PlcValue::Real(_) => PlcDataType::Real as u16,
            PlcValue::String(_) => PlcDataType::String as u16,
            PlcValue::Raw(_) => 0x0000,
        }
    }
}

pub struct EipClient {
    stream: TcpStream,
    session_handle: u32,
    pub connection_info: HashMap<String, String>,
}

impl EipClient {
    pub async fn connect(addr: &str) -> Result<Self, Box<dyn Error>> {
        println!("๐Ÿ”Œ Connecting to CompactLogix PLC at {}...", addr);
        let stream = TcpStream::connect(addr).await?;
        println!("โœ… TCP connection established");

        let mut client = EipClient {
            stream,
            session_handle: 0,
            connection_info: HashMap::new(),
        };

        client
            .connection_info
            .insert("address".to_string(), addr.to_string());
        client.register_session().await?;
        Ok(client)
    }

    async fn register_session(&mut self) -> Result<(), Box<dyn Error>> {
        let packet: [u8; 28] = [
            0x65, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
        ];

        self.stream.write_all(&packet).await?;
        println!("๐Ÿ“ค Sent Register Session request");

        let mut buf = [0u8; 1024];
        let n = timeout(Duration::from_secs(5), self.stream.read(&mut buf)).await??;

        if n >= 8 {
            self.session_handle = u32::from_le_bytes([buf[4], buf[5], buf[6], buf[7]]);
            let status = u32::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]);

            if status == 0 && self.session_handle != 0 {
                println!(
                    "๐ŸŽฏ Registration successful! Session ID: 0x{:08X}",
                    self.session_handle
                );
                self.connection_info.insert(
                    "session_id".to_string(),
                    format!("0x{:08X}", self.session_handle),
                );
                Ok(())
            } else {
                Err(format!("Registration failed. Status: 0x{:08X}", status).into())
            }
        } else {
            Err("Invalid registration response".into())
        }
    }

    pub async fn read_tag(&mut self, tag_name: &str) -> Result<PlcValue, Box<dyn Error>> {
        let (data_type, raw_data) = self.read_tag_raw(tag_name).await?;
        self.parse_value(data_type.into(), &raw_data)
    }

    async fn read_tag_raw(&mut self, tag_name: &str) -> Result<(u16, Vec<u8>), Box<dyn Error>> {
        let tag_bytes = tag_name.as_bytes();
        let mut cip_request = vec![0x4C, 0x00];
        let mut path = vec![0x91, tag_bytes.len() as u8];
        path.extend_from_slice(tag_bytes);

        if path.len() % 2 != 0 {
            path.push(0x00);
        }

        cip_request[1] = (path.len() / 2) as u8;
        cip_request.extend_from_slice(&path);
        cip_request.extend_from_slice(&[0x01, 0x00]);

        let response = self.send_cip_request(&cip_request).await?;
        self.parse_cip_response(&response)
    }

    pub async fn write_tag(
        &mut self,
        tag_name: &str,
        value: PlcValue,
    ) -> Result<(), Box<dyn Error>> {
        let tag_bytes = tag_name.as_bytes();
        let value_bytes = value.to_bytes();
        let data_type = value.get_data_type();

        let mut cip_request = vec![0x53, 0x02]; // Write Tag Service with verification
        let mut path = vec![0x91, tag_bytes.len() as u8];
        path.extend_from_slice(tag_bytes);

        if path.len() % 2 != 0 {
            path.push(0x00);
        }

        cip_request[1] = (path.len() / 2) as u8;
        cip_request.extend_from_slice(&path);
        cip_request.extend_from_slice(&data_type.to_le_bytes());
        cip_request.extend_from_slice(&[0x01, 0x00]);
        cip_request.extend_from_slice(&value_bytes);

        println!(
            "๐Ÿ“ Writing {} to tag '{}'",
            match &value {
                PlcValue::Bool(v) => format!("BOOL: {}", v),
                PlcValue::Dint(v) => format!("DINT: {}", v),
                PlcValue::Real(v) => format!("REAL: {}", v),
                PlcValue::String(v) => format!("STRING: '{}'", v),
                PlcValue::Raw(v) => format!("RAW: {:02X?}", v),
            },
            tag_name
        );

        let response = self.send_cip_request(&cip_request).await?;

        if response.len() >= 4 {
            let general_status = response[2];
            if general_status == 0x00 {
                println!("โœ… Tag '{}' written successfully!", tag_name);
                Ok(())
            } else {
                let error_msg = self.get_cip_error_message(general_status);
                Err(format!(
                    "Write failed - CIP Error 0x{:02X}: {}",
                    general_status, error_msg
                )
                .into())
            }
        } else {
            Err("Invalid write response".into())
        }
    }

    pub async fn read_multiple_tags(
        &mut self,
        tag_names: &[&str],
    ) -> Result<HashMap<String, PlcValue>, Box<dyn Error>> {
        let mut results = HashMap::new();
        println!("๐Ÿ“‹ Reading {} tags...", tag_names.len());

        for tag_name in tag_names {
            match self.read_tag(tag_name).await {
                Ok(value) => {
                    println!("โœ… {}: {:?}", tag_name, value);
                    results.insert(tag_name.to_string(), value);
                }
                Err(e) => {
                    println!("โŒ {}: {}", tag_name, e);
                }
            }
        }
        Ok(results)
    }

    pub async fn write_multiple_tags(
        &mut self,
        tags: HashMap<&str, PlcValue>,
    ) -> Result<Vec<String>, Box<dyn Error>> {
        let mut successful_writes = Vec::new();
        let total_tags = tags.len();

        println!("๐Ÿ“ Writing {} tags...", total_tags);
        for (tag_name, value) in tags {
            match self.write_tag(tag_name, value).await {
                Ok(()) => {
                    successful_writes.push(tag_name.to_string());
                }
                Err(e) => {
                    println!("โŒ Failed to write {}: {}", tag_name, e);
                }
            }
        }

        println!(
            "โœ… Successfully wrote {} of {} tags",
            successful_writes.len(),
            total_tags
        );
        Ok(successful_writes)
    }

    pub async fn read_array_element(
        &mut self,
        tag_name: &str,
        index: u16,
    ) -> Result<PlcValue, Box<dyn Error>> {
        let array_tag = format!("{}[{}]", tag_name, index);
        self.read_tag(&array_tag).await
    }

    pub async fn write_array_element(
        &mut self,
        tag_name: &str,
        index: u16,
        value: PlcValue,
    ) -> Result<(), Box<dyn Error>> {
        let array_tag = format!("{}[{}]", tag_name, index);
        self.write_tag(&array_tag, value).await
    }

    pub async fn read_array_range(
        &mut self,
        tag_name: &str,
        start_index: u16,
        count: u16,
    ) -> Result<Vec<PlcValue>, Box<dyn Error>> {
        let mut results = Vec::new();
        println!(
            "๐Ÿ“‹ Reading array {}[{}..{}]",
            tag_name,
            start_index,
            start_index + count - 1
        );

        for i in start_index..start_index + count {
            match self.read_array_element(tag_name, i).await {
                Ok(value) => {
                    println!("โœ… {}[{}]: {:?}", tag_name, i, value);
                    results.push(value);
                }
                Err(e) => {
                    println!("โŒ {}[{}]: {}", tag_name, i, e);
                    break;
                }
            }
        }
        Ok(results)
    }

    pub async fn discover_tags(&mut self) -> Result<Vec<String>, Box<dyn Error>> {
        let mut discovered_tags = Vec::new();
        println!("๐Ÿ” Discovering available tags...");

        let controller_tags = [
            "Controller.Type",
            "Controller.MajorRev",
            "Controller.MinorRev",
            "Controller.SerialNumber",
            "Controller.LastScan",
            "Controller.DateTime",
        ];

        for tag in &controller_tags {
            if self.read_tag(tag).await.is_ok() {
                discovered_tags.push(tag.to_string());
                println!("โœ… Found: {}", tag);
            }
        }

        let program_names = ["MainProgram", "Program", "Main"];
        for program in &program_names {
            let program_tag = format!("Program:{}.TestTag", program);
            if self.read_tag(&program_tag).await.is_ok() {
                discovered_tags.push(program_tag);
                println!("โœ… Found program: {}", program);
            }
        }

        Ok(discovered_tags)
    }

    pub async fn get_controller_info(&mut self) -> Result<HashMap<String, String>, Box<dyn Error>> {
        let mut info = self.connection_info.clone();

        if let Ok(PlcValue::Dint(serial)) = self.read_tag("Controller.SerialNumber").await {
            info.insert("serial_number".to_string(), serial.to_string());
        }

        if let Ok(PlcValue::Dint(controller_type)) = self.read_tag("Controller.Type").await {
            info.insert("controller_type".to_string(), controller_type.to_string());
        }

        if let Ok(PlcValue::Dint(major)) = self.read_tag("Controller.MajorRev").await {
            info.insert("major_revision".to_string(), major.to_string());
        }

        if let Ok(PlcValue::Dint(minor)) = self.read_tag("Controller.MinorRev").await {
            info.insert("minor_revision".to_string(), minor.to_string());
        }

        Ok(info)
    }

    pub async fn test_connectivity(&mut self) -> Result<HashMap<String, String>, Box<dyn Error>> {
        let mut status = HashMap::new();

        status.insert("connection".to_string(), "OK".to_string());
        status.insert(
            "session_id".to_string(),
            format!("0x{:08X}", self.session_handle),
        );

        match self.read_tag("TestTag").await {
            Ok(_) => {
                status.insert("read_test".to_string(), "PASS".to_string());
            }
            Err(e) => {
                status.insert("read_test".to_string(), format!("FAIL: {}", e));
            }
        }

        match self.write_tag("TestTag", PlcValue::Bool(false)).await {
            Ok(()) => {
                status.insert("write_test".to_string(), "PASS".to_string());
            }
            Err(e) => {
                status.insert("write_test".to_string(), format!("FAIL: {}", e));
            }
        }

        Ok(status)
    }

    pub async fn benchmark_performance(
        &mut self,
    ) -> Result<HashMap<String, String>, Box<dyn Error>> {
        let mut results = HashMap::new();
        println!("โšก Running performance benchmarks...");

        // Read performance test
        let start = std::time::Instant::now();
        let mut read_success = 0;
        for _ in 0..10 {
            if self.read_tag("TestTag").await.is_ok() {
                read_success += 1;
            }
        }
        let read_duration = start.elapsed();
        let read_rate = (read_success as f64 / read_duration.as_secs_f64()) as u32;

        println!("๐Ÿ“Š Read performance: {} ops/sec", read_rate);
        results.insert("read_rate_per_sec".to_string(), read_rate.to_string());
        results.insert(
            "read_success_rate".to_string(),
            format!("{}/10", read_success),
        );

        // Write performance test
        let start = std::time::Instant::now();
        let mut write_success = 0;
        for i in 0..10 {
            if self.write_tag("TestDint", PlcValue::Dint(i)).await.is_ok() {
                write_success += 1;
            }
        }
        let write_duration = start.elapsed();
        let write_rate = (write_success as f64 / write_duration.as_secs_f64()) as u32;

        println!("๐Ÿ“Š Write performance: {} ops/sec", write_rate);
        results.insert("write_rate_per_sec".to_string(), write_rate.to_string());
        results.insert(
            "write_success_rate".to_string(),
            format!("{}/10", write_success),
        );

        Ok(results)
    }

    pub async fn safe_read_tag(&mut self, tag_name: &str) -> Option<PlcValue> {
        self.read_tag(tag_name).await.ok()
    }

    pub async fn safe_write_tag(&mut self, tag_name: &str, value: PlcValue) -> bool {
        match self.write_tag(tag_name, value).await {
            Ok(()) => true,
            Err(_) => false,
        }
    }

    async fn send_cip_request(&mut self, cip_request: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
        let cip_len = cip_request.len();
        let total_data_len = 4 + 2 + 2 + 8 + cip_len;

        let mut packet = vec![0x6F, 0x00];
        packet.extend_from_slice(&(total_data_len as u16).to_le_bytes());
        packet.extend_from_slice(&self.session_handle.to_le_bytes());
        packet.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
        packet.extend_from_slice(&[0x01, 0x02, 0x03, 0x04]);
        packet.extend_from_slice(&[0x05, 0x06, 0x07, 0x08]);
        packet.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
        packet.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
        packet.extend_from_slice(&[0x05, 0x00]);
        packet.extend_from_slice(&[0x02, 0x00]);
        packet.extend_from_slice(&[0x00, 0x00]);
        packet.extend_from_slice(&[0x00, 0x00]);
        packet.extend_from_slice(&[0xB2, 0x00]);
        packet.extend_from_slice(&(cip_len as u16).to_le_bytes());
        packet.extend_from_slice(cip_request);

        self.stream.write_all(&packet).await?;

        let mut buf = [0u8; 1024];
        let n = timeout(Duration::from_secs(10), self.stream.read(&mut buf)).await??;

        if n >= 24 {
            let cmd_status = u32::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]);
            if cmd_status == 0 {
                self.extract_cip_from_response(&buf[..n])
            } else {
                Err(format!("EIP Command failed. Status: 0x{:08X}", cmd_status).into())
            }
        } else {
            Err("Response too short".into())
        }
    }

    fn extract_cip_from_response(&self, response: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
        let mut pos = 24;
        pos += 4;
        pos += 2;
        let item_count = u16::from_le_bytes([response[pos], response[pos + 1]]);
        pos += 2;

        for _ in 0..item_count {
            let item_type = u16::from_le_bytes([response[pos], response[pos + 1]]);
            pos += 2;
            let item_length = u16::from_le_bytes([response[pos], response[pos + 1]]);
            pos += 2;

            if item_type == 0x00B2 && item_length > 0 {
                return Ok(response[pos..pos + item_length as usize].to_vec());
            }

            pos += item_length as usize;
        }

        Err("Could not find CIP response data".into())
    }

    fn parse_cip_response(&self, cip_response: &[u8]) -> Result<(u16, Vec<u8>), Box<dyn Error>> {
        if cip_response.len() < 4 {
            return Err("CIP response too short".into());
        }

        let general_status = cip_response[2];
        let additional_status_size = cip_response[3];

        if general_status == 0x00 {
            let data_start = 4 + (additional_status_size as usize * 2);
            if data_start + 2 <= cip_response.len() {
                let data_type =
                    u16::from_le_bytes([cip_response[data_start], cip_response[data_start + 1]]);
                let value_data = &cip_response[data_start + 2..];
                return Ok((data_type, value_data.to_vec()));
            }
        }

        let error_msg = self.get_cip_error_message(general_status);
        Err(format!("CIP Error 0x{:02X}: {}", general_status, error_msg).into())
    }

    fn parse_value(
        &self,
        data_type: PlcDataTypeExt,
        raw_data: &[u8],
    ) -> Result<PlcValue, Box<dyn Error>> {
        match data_type {
            PlcDataTypeExt::Known(PlcDataType::Bool) => {
                if !raw_data.is_empty() {
                    Ok(PlcValue::Bool(raw_data[0] != 0))
                } else {
                    Err("No data for BOOL value".into())
                }
            }
            PlcDataTypeExt::Known(PlcDataType::Dint) => {
                if raw_data.len() >= 4 {
                    let value =
                        i32::from_le_bytes([raw_data[0], raw_data[1], raw_data[2], raw_data[3]]);
                    Ok(PlcValue::Dint(value))
                } else {
                    Err("Insufficient data for DINT value".into())
                }
            }
            PlcDataTypeExt::Known(PlcDataType::Real) => {
                if raw_data.len() >= 4 {
                    let value =
                        f32::from_le_bytes([raw_data[0], raw_data[1], raw_data[2], raw_data[3]]);
                    Ok(PlcValue::Real(value))
                } else {
                    Err("Insufficient data for REAL value".into())
                }
            }
            PlcDataTypeExt::Known(PlcDataType::String) => {
                if raw_data.len() >= 2 {
                    let str_len = u16::from_le_bytes([raw_data[0], raw_data[1]]) as usize;
                    if raw_data.len() >= 2 + str_len {
                        let str_bytes = &raw_data[2..2 + str_len];
                        let string_value = String::from_utf8_lossy(str_bytes).to_string();
                        Ok(PlcValue::String(string_value))
                    } else {
                        Err("Insufficient data for STRING value".into())
                    }
                } else {
                    Err("No data for STRING value".into())
                }
            }
            PlcDataTypeExt::Unknown(_) => Ok(PlcValue::Raw(raw_data.to_vec())),
        }
    }

    fn get_cip_error_message(&self, status: u8) -> &'static str {
        match status {
            0x01 => "Connection failure",
            0x02 => "Resource unavailable",
            0x03 => "Invalid parameter value",
            0x04 => "Path destination unknown",
            0x05 => "Path segment error",
            0x06 => "Path destination unknown",
            0x07 => "Partial transfer",
            0x08 => "Connection lost",
            0x09 => "Service not supported",
            0x0A => "Invalid attribute value",
            0x0B => "Attribute list error",
            0x0C => "Already in requested mode/state",
            0x0D => "Object state conflict",
            0x0E => "Object already exists",
            0x0F => "Attribute not settable",
            0x10 => "Privilege violation",
            0x11 => "Device state conflict",
            0x12 => "Reply data too large",
            0x13 => "Fragmentation of a primitive value",
            0x14 => "Not enough data",
            0x15 => "Attribute not supported",
            0x16 => "Too much data",
            0x17 => "Object does not exist",
            0x18 => "Service fragmentation sequence not in progress",
            0x19 => "No stored attribute data",
            0x1A => "Store operation failure",
            0x1B => "Routing failure, request packet too large",
            0x1C => "Routing failure, response packet too large",
            0x1D => "Missing attribute list entry data",
            0x1E => "Invalid attribute value list",
            0x1F => "Embedded service error",
            _ => "Unknown error",
        }
    }

    pub async fn unregister_session(&mut self) -> Result<(), Box<dyn Error>> {
        let session_bytes = self.session_handle.to_le_bytes();
        let packet: [u8; 24] = [
            0x66,
            0x00,
            0x00,
            0x00,
            session_bytes[0],
            session_bytes[1],
            session_bytes[2],
            session_bytes[3],
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
            0x00,
        ];

        self.stream.write_all(&packet).await?;
        println!("๐Ÿ“ค Session closed");
        Ok(())
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    println!("๐Ÿฆ€ Rust EtherNet/IP Driver v2.0 - Complete Edition");
    println!("====================================================");

    let mut client = EipClient::connect("192.168.0.1:44818").await?;

    // Test 1: Read TestTag
    println!("\n๐Ÿงช TEST 1: Reading TestTag");
    match client.read_tag("TestTag").await {
        Ok(value) => println!("โœ… TestTag value: {:?}", value),
        Err(e) => println!("โŒ Failed to read TestTag: {}", e),
    }

    // Test 2: Write TestTag
    println!("\n๐Ÿงช TEST 2: Writing to TestTag");
    match client.write_tag("TestTag", PlcValue::Bool(true)).await {
        Ok(()) => {
            println!("โœ… Successfully wrote TRUE to TestTag");
            match client.read_tag("TestTag").await {
                Ok(value) => println!("โœ… Verification - TestTag is now: {:?}", value),
                Err(e) => println!("โš ๏ธ Could not verify write: {}", e),
            }
        }
        Err(e) => println!("โŒ Failed to write TestTag: {}", e),
    }

    // Test 3: Different Data Types
    println!("\n๐Ÿงช TEST 3: Testing Different Data Types");

    println!("๐Ÿ“ Testing DINT operations...");
    match client.write_tag("TestDint", PlcValue::Dint(12345)).await {
        Ok(()) => match client.read_tag("TestDint").await {
            Ok(value) => println!("โœ… DINT test: {:?}", value),
            Err(e) => println!("โš ๏ธ DINT read failed: {}", e),
        },
        Err(e) => println!("โš ๏ธ DINT write failed: {}", e),
    }

    println!("๐Ÿ“ Testing REAL operations...");
    match client.write_tag("TestReal", PlcValue::Real(123.45)).await {
        Ok(()) => match client.read_tag("TestReal").await {
            Ok(value) => println!("โœ… REAL test: {:?}", value),
            Err(e) => println!("โš ๏ธ REAL read failed: {}", e),
        },
        Err(e) => println!("โš ๏ธ REAL write failed: {}", e),
    }

    // Test 4: Controller Information
    println!("\n๐Ÿงช TEST 4: Controller Information");
    match client.get_controller_info().await {
        Ok(info) => {
            println!("๐Ÿ“Š Controller Details:");
            for (key, value) in &info {
                println!("   {}: {}", key, value);
            }
        }
        Err(e) => println!("โš ๏ธ Could not get controller info: {}", e),
    }

    // Test 5: Tag Discovery
    println!("\n๐Ÿงช TEST 5: Tag Discovery");
    match client.discover_tags().await {
        Ok(tags) => {
            if !tags.is_empty() {
                println!("๐Ÿ” Discovered {} tags:", tags.len());
                for tag in tags {
                    println!("   ๐Ÿ“Œ {}", tag);
                }
            } else {
                println!("โš ๏ธ No controller tags discovered");
            }
        }
        Err(e) => println!("โš ๏ธ Discovery error: {}", e),
    }

    // Test 6: Multiple Tag Operations
    println!("\n๐Ÿงช TEST 6: Multiple Tag Operations");
    let test_tags = ["TestTag", "TestDint", "TestReal"];
    match client.read_multiple_tags(&test_tags).await {
        Ok(results) => {
            println!("๐Ÿ“‹ Multiple tag read results:");
            for (tag, value) in results {
                println!("   {}: {:?}", tag, value);
            }
        }
        Err(e) => println!("โš ๏ธ Multiple tag read error: {}", e),
    }

    // Test 7: Array Operations
    println!("\n๐Ÿงช TEST 7: Array Operations");
    println!("๐Ÿ“ Testing array operations (create TestArray[10] in your PLC)...");
    match client
        .write_array_element("TestArray", 0, PlcValue::Dint(100))
        .await
    {
        Ok(()) => {
            match client
                .write_array_element("TestArray", 1, PlcValue::Dint(200))
                .await
            {
                Ok(()) => match client.read_array_range("TestArray", 0, 3).await {
                    Ok(values) => {
                        println!("โœ… Array operations successful! Values: {:?}", values);
                    }
                    Err(e) => println!("โš ๏ธ Array read failed: {}", e),
                },
                Err(e) => println!("โš ๏ธ Array write [1] failed: {}", e),
            }
        }
        Err(e) => println!("โš ๏ธ Array write [0] failed (create TestArray[10]): {}", e),
    }

    // Test 8: Connectivity Test
    println!("\n๐Ÿงช TEST 8: Connectivity Test");
    match client.test_connectivity().await {
        Ok(status) => {
            println!("๐Ÿ”— Connectivity Status:");
            for (key, value) in status {
                println!("   {}: {}", key, value);
            }
        }
        Err(e) => println!("โš ๏ธ Connectivity test error: {}", e),
    }

    // Test 9: Performance Benchmark
    println!("\n๐Ÿงช TEST 9: Performance Benchmark");
    match client.benchmark_performance().await {
        Ok(results) => {
            println!("โšก Performance Results:");
            for (key, value) in results {
                println!("   {}: {}", key, value);
            }
        }
        Err(e) => println!("โš ๏ธ Benchmark error: {}", e),
    }

    // Test 10: Safe Operations Demo
    println!("\n๐Ÿงช TEST 10: Safe Operations Demo");
    if let Some(value) = client.safe_read_tag("TestTag").await {
        println!("โœ… Safe read successful: {:?}", value);
    } else {
        println!("โš ๏ธ Safe read returned None");
    }

    if client
        .safe_write_tag("TestTag", PlcValue::Bool(false))
        .await
    {
        println!("โœ… Safe write successful");
    } else {
        println!("โš ๏ธ Safe write failed");
    }

    // Clean up
    client.unregister_session().await?;

    println!("\n๐ŸŽ‰ Complete EtherNet/IP Driver Test Finished!");
    println!("================================================");
    println!("โœ… Your Rust EtherNet/IP library is production-ready!");
    println!("๐Ÿš€ Features tested: Read/Write, Arrays, Discovery, Performance");
    println!("๐Ÿ”ง Ready for integration with C# or TypeScript!");

    Ok(())
}