mbus-async 0.11.0

Native async Modbus client and server stack for Tokio-based applications
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
//! Core async client handle shared by all transport flavours.
//!
//! [`AsyncClientCore`] is the single place that owns the channel to the
//! background `ClientTask` and implements every Modbus request method.
//! Transport-specific client types (`AsyncTcpClient`, `AsyncSerialClient`)
//! store an `AsyncClientCore` as their only field and expose its API
//! transparently via [`std::ops::Deref`].
//!
//! # Architecture
//!
//! ```text
//! AsyncTcpClient / AsyncSerialClient
//!   └── AsyncClientCore   (this module)
//!         ├── mpsc::Sender<TaskCommand>  ──────► `ClientTask::run()`  (tokio task)
//!         └── watch::Receiver<usize>            (pending-request count)
//! ```
//!
//! Each public async method:
//! 1. Creates a `oneshot` channel.
//! 2. Sends a [`TaskCommand::Request`] (carrying the oneshot sender) over the mpsc channel.
//! 3. `await`s the oneshot receiver for the reply.
//!

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use tokio::sync::{mpsc, oneshot};

#[cfg(any(
    feature = "holding-registers",
    feature = "input-registers",
    feature = "diagnostics"
))]
use mbus_core::errors::MbusError;
use mbus_core::transport::UnitIdOrSlaveAddr;

#[cfg(feature = "diagnostics")]
use mbus_core::function_codes::public::{DiagnosticSubFunction, EncapsulatedInterfaceType};
#[cfg(feature = "coils")]
use mbus_core::models::coil::Coils;
#[cfg(feature = "diagnostics")]
use mbus_core::models::diagnostic::{DeviceIdentificationResponse, ObjectId, ReadDeviceIdCode};
#[cfg(feature = "discrete-inputs")]
use mbus_core::models::discrete_input::DiscreteInputs;
#[cfg(feature = "fifo")]
use mbus_core::models::fifo_queue::FifoQueue;
#[cfg(feature = "file-record")]
use mbus_core::models::file_record::{SubRequest, SubRequestParams};
#[cfg(any(feature = "holding-registers", feature = "input-registers"))]
use mbus_core::models::register::Registers;

use crate::client::command::{ClientRequest, TaskCommand};
use crate::client::response::ClientResponse;
use crate::client::task::PendingCountReceiver;

#[cfg(feature = "traffic")]
use crate::client::notifier::{AsyncClientTrafficNotifier, NotifierStore};

use super::AsyncError;
#[cfg(feature = "diagnostics")]
use super::{CommEventLogResponse, DiagnosticsDataResponse};

// ── Core handle ─────────────────────────────────────────────────────────────

/// Shared async client handle.
///
/// Owns the `mpsc::Sender` that drives the background async task and a
/// `watch::Receiver` used for a synchronous `has_pending_requests()` query.
///
/// Dropping this value closes the channel, which causes the background
/// `ClientTask` to exit cleanly via its `cmd_rx.recv()` returning `None`.
pub struct AsyncClientCore {
    cmd_tx: mpsc::Sender<TaskCommand>,
    pending_count_rx: PendingCountReceiver,
    /// Per-request timeout in nanoseconds; 0 = disabled.
    request_timeout_ns: Arc<AtomicU64>,
    #[cfg(feature = "traffic")]
    notifier: NotifierStore,
}

impl AsyncClientCore {
    /// Creates a new core handle wired to an already-spawned `ClientTask`.
    pub(super) fn new(
        cmd_tx: mpsc::Sender<TaskCommand>,
        pending_count_rx: PendingCountReceiver,
        #[cfg(feature = "traffic")] notifier: NotifierStore,
    ) -> Self {
        Self {
            cmd_tx,
            pending_count_rx,
            request_timeout_ns: Arc::new(AtomicU64::new(0)),
            #[cfg(feature = "traffic")]
            notifier,
        }
    }

    // ── Internal helpers ─────────────────────────────────────────────────

    /// Sends a [`ClientRequest`] to the background task and awaits the reply.
    ///
    /// If a per-request timeout is set via [`set_request_timeout`](Self::set_request_timeout)
    /// and no response arrives within that deadline, returns [`AsyncError::Timeout`].
    async fn send_request(&self, params: ClientRequest) -> Result<ClientResponse, AsyncError> {
        let (resp_tx, rx) = oneshot::channel();
        self.cmd_tx
            .send(TaskCommand::Request { params, resp_tx })
            .await
            .map_err(|_| AsyncError::WorkerClosed)?;

        let timeout_ns = self.request_timeout_ns.load(Ordering::Relaxed);
        if timeout_ns > 0 {
            let outcome = tokio::time::timeout(Duration::from_nanos(timeout_ns), rx).await;
            if outcome.is_err() {
                // Transport may be hung.  Send a non-blocking Disconnect so the
                // background task drains the pipeline and closes the transport;
                // the caller can then call connect() to recover.
                let _ = self.cmd_tx.try_send(TaskCommand::Disconnect);
                return Err(AsyncError::Timeout);
            }
            outcome
                .unwrap()
                .map_err(|_| AsyncError::WorkerClosed)?
                .map_err(AsyncError::Mbus)
        } else {
            rx.await
                .map_err(|_| AsyncError::WorkerClosed)?
                .map_err(AsyncError::Mbus)
        }
    }

    // ── Connection ───────────────────────────────────────────────────────

    /// Establishes the underlying transport connection.
    ///
    /// Must be called once before issuing Modbus requests.  Can be called
    /// again after a disconnect to reconnect.
    pub async fn connect(&self) -> Result<(), AsyncError> {
        let (resp_tx, rx) = oneshot::channel();
        self.cmd_tx
            .send(TaskCommand::Connect { resp_tx })
            .await
            .map_err(|_| AsyncError::WorkerClosed)?;
        rx.await
            .map_err(|_| AsyncError::WorkerClosed)?
            .map_err(AsyncError::Mbus)
    }

    /// Disconnects the underlying transport.
    ///
    /// Drains all in-flight and queued requests with
    /// [`MbusError::ConnectionClosed`] and closes the transport.  After this
    /// call, [`connect`](Self::connect) can be called to reconnect.
    ///
    /// This is an explicit, graceful disconnect.  The background task continues
    /// running so the client can be reconnected later.  Dropping the client
    /// handle entirely also stops the background task.
    pub async fn disconnect(&self) -> Result<(), AsyncError> {
        self.cmd_tx
            .send(TaskCommand::Disconnect)
            .await
            .map_err(|_| AsyncError::WorkerClosed)
    }

    /// Returns `true` when there are requests in-flight awaiting a response.
    ///
    /// This is a **synchronous** check — no `.await` required.
    pub fn has_pending_requests(&self) -> bool {
        *self.pending_count_rx.borrow() > 0
    }
    // ── Request timeout ──────────────────────────────────────────────────────────

    /// Sets a per-request deadline applied to every subsequent request call.
    ///
    /// If a response is not received within `timeout`, the method returns
    /// [`AsyncError::Timeout`].  The in-flight entry remains in the background
    /// task until the transport delivers or errors; calling
    /// [`connect`](Self::connect) resets transport state.
    ///
    /// The timeout can be updated at any time and takes effect on the next
    /// request.  Call [`clear_request_timeout`](Self::clear_request_timeout) to
    /// remove it.
    pub fn set_request_timeout(&self, timeout: Duration) {
        self.request_timeout_ns.store(
            u64::try_from(timeout.as_nanos()).unwrap_or(u64::MAX),
            Ordering::Relaxed,
        );
    }

    /// Removes the per-request timeout set by
    /// [`set_request_timeout`](Self::set_request_timeout), allowing requests to
    /// wait indefinitely for a server response.
    pub fn clear_request_timeout(&self) {
        self.request_timeout_ns.store(0, Ordering::Relaxed);
    }
    // ── Traffic notifier ─────────────────────────────────────────────────

    /// Registers (or replaces) an [`AsyncClientTrafficNotifier`] for traffic events.
    ///
    /// The notifier is invoked from the background task on every transmitted
    /// and received frame.
    #[cfg(feature = "traffic")]
    pub fn set_traffic_notifier<N: AsyncClientTrafficNotifier + Send + 'static>(
        &self,
        notifier: N,
    ) {
        if let Ok(mut g) = self.notifier.try_lock() {
            *g = Some(Box::new(notifier));
        }
    }

    /// Removes any previously registered traffic notifier.
    #[cfg(feature = "traffic")]
    pub fn clear_traffic_notifier(&self) {
        if let Ok(mut g) = self.notifier.try_lock() {
            *g = None;
        }
    }

    // ── Coil methods ─────────────────────────────────────────────────────

    /// Reads multiple coils (FC 01) from `address` with the given `quantity`.
    ///
    /// Returns the coil values packed into a [`Coils`] object.
    #[cfg(feature = "coils")]
    pub async fn read_multiple_coils(
        &self,
        unit_id: u8,
        address: u16,
        quantity: u16,
    ) -> Result<Coils, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        #[allow(unreachable_patterns)]
        match self
            .send_request(ClientRequest::ReadMultipleCoils {
                unit,
                address,
                quantity,
            })
            .await?
        {
            ClientResponse::Coils(coils) => Ok(coils),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Writes a single coil (FC 05) at `address` with the given boolean `value`.
    ///
    /// Returns `(address, value)` echoed back by the server.
    #[cfg(feature = "coils")]
    pub async fn write_single_coil(
        &self,
        unit_id: u8,
        address: u16,
        value: bool,
    ) -> Result<(u16, bool), AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        #[allow(unreachable_patterns)]
        match self
            .send_request(ClientRequest::WriteSingleCoil {
                unit,
                address,
                value,
            })
            .await?
        {
            ClientResponse::Coils(coils) => {
                let v = coils.value(coils.from_address()).unwrap_or(false);
                Ok((coils.from_address(), v))
            }
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Writes multiple coils (FC 15) starting at `address`.
    ///
    /// Returns `(starting_address, quantity)` echoed back by the server.
    #[cfg(feature = "coils")]
    pub async fn write_multiple_coils(
        &self,
        unit_id: u8,
        address: u16,
        coils: &Coils,
    ) -> Result<(u16, u16), AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        #[allow(unreachable_patterns)]
        match self
            .send_request(ClientRequest::WriteMultipleCoils {
                unit,
                address,
                coils: coils.clone(),
            })
            .await?
        {
            ClientResponse::Coils(coils) => Ok((coils.from_address(), coils.quantity())),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    // ── Register methods ──────────────────────────────────────────────────

    /// Reads holding registers (FC 03) from `address` with the given `quantity`.
    ///
    /// Returns the register values as a [`Registers`] object.
    #[cfg(feature = "holding-registers")]
    pub async fn read_holding_registers(
        &self,
        unit_id: u8,
        address: u16,
        quantity: u16,
    ) -> Result<Registers, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::ReadHoldingRegisters {
                unit,
                address,
                quantity,
            })
            .await?
        {
            ClientResponse::Registers(regs) => Ok(regs),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Reads input registers (FC 04) from `address` with the given `quantity`.
    ///
    /// Returns the register values as a [`Registers`] object.
    #[cfg(feature = "input-registers")]
    pub async fn read_input_registers(
        &self,
        unit_id: u8,
        address: u16,
        quantity: u16,
    ) -> Result<Registers, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::ReadInputRegisters {
                unit,
                address,
                quantity,
            })
            .await?
        {
            ClientResponse::Registers(regs) => Ok(regs),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Writes a single holding register (FC 06) at `address` with `value`.
    ///
    /// Returns `(address, value)` echoed back by the server.
    #[cfg(feature = "holding-registers")]
    pub async fn write_single_register(
        &self,
        unit_id: u8,
        address: u16,
        value: u16,
    ) -> Result<(u16, u16), AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::WriteSingleRegister {
                unit,
                address,
                value,
            })
            .await?
        {
            ClientResponse::SingleRegisterWrite { address, value } => Ok((address, value)),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Writes multiple holding registers (FC 16) starting at `address`.
    ///
    /// Returns `(starting_address, quantity)` echoed back by the server.
    #[cfg(feature = "holding-registers")]
    pub async fn write_multiple_registers(
        &self,
        unit_id: u8,
        address: u16,
        values: &[u16],
    ) -> Result<(u16, u16), AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        let hv =
            heapless::Vec::<u16, { mbus_core::data_unit::common::MAX_PDU_DATA_LEN }>::from_slice(
                values,
            )
            .map_err(|_| AsyncError::Mbus(MbusError::BufferTooSmall))?;
        match self
            .send_request(ClientRequest::WriteMultipleRegisters {
                unit,
                address,
                values: hv,
            })
            .await?
        {
            ClientResponse::Registers(regs) => Ok((regs.from_address(), regs.quantity())),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Performs a combined read/write on holding registers (FC 23).
    ///
    /// Reads `read_quantity` registers starting at `read_address` and
    /// simultaneously writes `write_values` starting at `write_address`.
    /// Returns the read registers.
    #[cfg(feature = "holding-registers")]
    pub async fn read_write_multiple_registers(
        &self,
        unit_id: u8,
        read_address: u16,
        read_quantity: u16,
        write_address: u16,
        write_values: &[u16],
    ) -> Result<Registers, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        let hv =
            heapless::Vec::<u16, { mbus_core::data_unit::common::MAX_PDU_DATA_LEN }>::from_slice(
                write_values,
            )
            .map_err(|_| AsyncError::Mbus(MbusError::BufferTooSmall))?;
        match self
            .send_request(ClientRequest::ReadWriteMultipleRegisters {
                unit,
                read_address,
                read_quantity,
                write_address,
                write_values: hv,
            })
            .await?
        {
            ClientResponse::Registers(regs) => Ok(regs),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Applies an AND/OR bitmask to a holding register (FC 22).
    ///
    /// The resulting register value is `(current & and_mask) | (or_mask & !and_mask)`.
    #[cfg(feature = "holding-registers")]
    pub async fn mask_write_register(
        &self,
        unit_id: u8,
        address: u16,
        and_mask: u16,
        or_mask: u16,
    ) -> Result<(), AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::MaskWriteRegister {
                unit,
                address,
                and_mask,
                or_mask,
            })
            .await?
        {
            ClientResponse::MaskWriteRegister => Ok(()),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    // ── Discrete input methods ────────────────────────────────────────────

    /// Reads discrete inputs (FC 02) from `address` with the given `quantity`.
    ///
    /// Returns the input states as a [`DiscreteInputs`] object.
    #[cfg(feature = "discrete-inputs")]
    pub async fn read_discrete_inputs(
        &self,
        unit_id: u8,
        address: u16,
        quantity: u16,
    ) -> Result<DiscreteInputs, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::ReadDiscreteInputs {
                unit,
                address,
                quantity,
            })
            .await?
        {
            ClientResponse::DiscreteInputs(di) => Ok(di),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    // ── FIFO methods ──────────────────────────────────────────────────────

    /// Reads the FIFO queue (FC 24) at `address`.
    ///
    /// Returns up to 31 words from the FIFO queue as a [`FifoQueue`] object.
    #[cfg(feature = "fifo")]
    pub async fn read_fifo_queue(
        &self,
        unit_id: u8,
        address: u16,
    ) -> Result<FifoQueue, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::ReadFifoQueue { unit, address })
            .await?
        {
            ClientResponse::FifoQueue(queue) => Ok(queue),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    // ── File record methods ───────────────────────────────────────────────

    /// Reads a file record (FC 20) described by `sub_request`.
    ///
    /// Returns the sub-request response parameters for each requested record.
    #[cfg(feature = "file-record")]
    pub async fn read_file_record(
        &self,
        unit_id: u8,
        sub_request: &SubRequest,
    ) -> Result<Vec<SubRequestParams>, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::ReadFileRecord {
                unit,
                sub_request: sub_request.clone(),
            })
            .await?
        {
            ClientResponse::FileRecordRead(data) => Ok(data.into_iter().collect()),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Writes a file record (FC 21) described by `sub_request`.
    #[cfg(feature = "file-record")]
    pub async fn write_file_record(
        &self,
        unit_id: u8,
        sub_request: &SubRequest,
    ) -> Result<(), AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::WriteFileRecord {
                unit,
                sub_request: sub_request.clone(),
            })
            .await?
        {
            ClientResponse::FileRecordWrite => Ok(()),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    // ── Diagnostics methods ───────────────────────────────────────────────

    /// Reads device identification objects (FC 43 / MEI 14).
    #[cfg(feature = "diagnostics")]
    pub async fn read_device_identification(
        &self,
        unit_id: u8,
        read_device_id_code: ReadDeviceIdCode,
        object_id: ObjectId,
    ) -> Result<DeviceIdentificationResponse, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::ReadDeviceIdentification {
                unit,
                read_device_id_code,
                object_id,
            })
            .await?
        {
            ClientResponse::DeviceIdentification(resp) => Ok(resp),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Sends an encapsulated interface transport request (FC 43).
    ///
    /// Returns the `(mei_type, data)` pair from the server response.
    #[cfg(feature = "diagnostics")]
    pub async fn encapsulated_interface_transport(
        &self,
        unit_id: u8,
        mei_type: EncapsulatedInterfaceType,
        data: &[u8],
    ) -> Result<(EncapsulatedInterfaceType, Vec<u8>), AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        let hv =
            heapless::Vec::<u8, { mbus_core::data_unit::common::MAX_PDU_DATA_LEN }>::from_slice(
                data,
            )
            .map_err(|_| AsyncError::Mbus(MbusError::BufferTooSmall))?;
        match self
            .send_request(ClientRequest::EncapsulatedInterfaceTransport {
                unit,
                mei_type,
                data: hv,
            })
            .await?
        {
            ClientResponse::EncapsulatedInterfaceTransport { mei_type, data } => {
                Ok((mei_type, data.as_slice().to_vec()))
            }
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Reads the device exception status (FC 07).
    #[cfg(feature = "diagnostics")]
    pub async fn read_exception_status(&self, unit_id: u8) -> Result<u8, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::ReadExceptionStatus { unit })
            .await?
        {
            ClientResponse::ExceptionStatus(status) => Ok(status),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Sends a diagnostics request (FC 08).
    ///
    /// Returns [`DiagnosticsDataResponse`] with echoed `sub_function` and `data`.
    #[cfg(feature = "diagnostics")]
    pub async fn diagnostics(
        &self,
        unit_id: u8,
        sub_function: DiagnosticSubFunction,
        data: &[u16],
    ) -> Result<DiagnosticsDataResponse, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        let hv =
            heapless::Vec::<u16, { mbus_core::data_unit::common::MAX_PDU_DATA_LEN }>::from_slice(
                data,
            )
            .map_err(|_| AsyncError::Mbus(MbusError::BufferTooSmall))?;
        match self
            .send_request(ClientRequest::Diagnostics {
                unit,
                sub_function,
                data: hv,
            })
            .await?
        {
            ClientResponse::DiagnosticsData { sub_function, data } => Ok(DiagnosticsDataResponse {
                sub_function,
                data: data.as_slice().to_vec(),
            }),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Reads the communication event counter (FC 11).
    ///
    /// Returns `(status_word, event_count)`.
    #[cfg(feature = "diagnostics")]
    pub async fn get_comm_event_counter(&self, unit_id: u8) -> Result<(u16, u16), AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::GetCommEventCounter { unit })
            .await?
        {
            ClientResponse::CommEventCounter {
                status,
                event_count,
            } => Ok((status, event_count)),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Reads the communication event log (FC 12).
    ///
    /// Returns `(status, event_count, message_count, events)`.
    #[cfg(feature = "diagnostics")]
    pub async fn get_comm_event_log(
        &self,
        unit_id: u8,
    ) -> Result<CommEventLogResponse, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::GetCommEventLog { unit })
            .await?
        {
            ClientResponse::CommEventLog {
                status,
                event_count,
                message_count,
                events,
            } => Ok((
                status,
                event_count,
                message_count,
                events.as_slice().to_vec(),
            )),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }

    /// Requests the server identifier data (FC 17).
    ///
    /// Returns the raw server ID byte array.
    #[cfg(feature = "diagnostics")]
    pub async fn report_server_id(&self, unit_id: u8) -> Result<Vec<u8>, AsyncError> {
        let unit = UnitIdOrSlaveAddr::new(unit_id).map_err(AsyncError::Mbus)?;
        match self
            .send_request(ClientRequest::ReportServerId { unit })
            .await?
        {
            ClientResponse::ReportServerId(data) => Ok(data.as_slice().to_vec()),
            _ => Err(AsyncError::UnexpectedResponseType),
        }
    }
}