tinkerforge_async/bindings/
rs232_v2_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-16.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.20                              *
5 *                                                           *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link  *
8 * to the generators git repository on tinkerforge.com       *
9 *************************************************************/
10
11//! Communicates with RS232 devices.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/RS232V2_Bricklet_Rust.html).
14#[allow(unused_imports)]
15use crate::{
16    base58::Uid, byte_converter::*, converting_high_level_callback_receiver::ConvertingHighLevelCallbackReceiver,
17    converting_receiver::BrickletRecvTimeoutError, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
18    low_level_traits::LowLevelRead,
19};
20#[allow(unused_imports)]
21use futures_core::Stream;
22#[allow(unused_imports)]
23use tokio_stream::StreamExt;
24pub enum Rs232V2BrickletFunction {
25    WriteLowLevel,
26    ReadLowLevel,
27    EnableReadCallback,
28    DisableReadCallback,
29    IsReadCallbackEnabled,
30    SetConfiguration,
31    GetConfiguration,
32    SetBufferConfig,
33    GetBufferConfig,
34    GetBufferStatus,
35    GetErrorCount,
36    SetFrameReadableCallbackConfiguration,
37    GetFrameReadableCallbackConfiguration,
38    GetSpitfpErrorCount,
39    SetBootloaderMode,
40    GetBootloaderMode,
41    SetWriteFirmwarePointer,
42    WriteFirmware,
43    SetStatusLedConfig,
44    GetStatusLedConfig,
45    GetChipTemperature,
46    Reset,
47    WriteUid,
48    ReadUid,
49    GetIdentity,
50    CallbackReadLowLevel,
51    CallbackErrorCount,
52    CallbackFrameReadable,
53}
54impl From<Rs232V2BrickletFunction> for u8 {
55    fn from(fun: Rs232V2BrickletFunction) -> Self {
56        match fun {
57            Rs232V2BrickletFunction::WriteLowLevel => 1,
58            Rs232V2BrickletFunction::ReadLowLevel => 2,
59            Rs232V2BrickletFunction::EnableReadCallback => 3,
60            Rs232V2BrickletFunction::DisableReadCallback => 4,
61            Rs232V2BrickletFunction::IsReadCallbackEnabled => 5,
62            Rs232V2BrickletFunction::SetConfiguration => 6,
63            Rs232V2BrickletFunction::GetConfiguration => 7,
64            Rs232V2BrickletFunction::SetBufferConfig => 8,
65            Rs232V2BrickletFunction::GetBufferConfig => 9,
66            Rs232V2BrickletFunction::GetBufferStatus => 10,
67            Rs232V2BrickletFunction::GetErrorCount => 11,
68            Rs232V2BrickletFunction::SetFrameReadableCallbackConfiguration => 14,
69            Rs232V2BrickletFunction::GetFrameReadableCallbackConfiguration => 15,
70            Rs232V2BrickletFunction::GetSpitfpErrorCount => 234,
71            Rs232V2BrickletFunction::SetBootloaderMode => 235,
72            Rs232V2BrickletFunction::GetBootloaderMode => 236,
73            Rs232V2BrickletFunction::SetWriteFirmwarePointer => 237,
74            Rs232V2BrickletFunction::WriteFirmware => 238,
75            Rs232V2BrickletFunction::SetStatusLedConfig => 239,
76            Rs232V2BrickletFunction::GetStatusLedConfig => 240,
77            Rs232V2BrickletFunction::GetChipTemperature => 242,
78            Rs232V2BrickletFunction::Reset => 243,
79            Rs232V2BrickletFunction::WriteUid => 248,
80            Rs232V2BrickletFunction::ReadUid => 249,
81            Rs232V2BrickletFunction::GetIdentity => 255,
82            Rs232V2BrickletFunction::CallbackReadLowLevel => 12,
83            Rs232V2BrickletFunction::CallbackErrorCount => 13,
84            Rs232V2BrickletFunction::CallbackFrameReadable => 16,
85        }
86    }
87}
88pub const RS232_V2_BRICKLET_PARITY_NONE: u8 = 0;
89pub const RS232_V2_BRICKLET_PARITY_ODD: u8 = 1;
90pub const RS232_V2_BRICKLET_PARITY_EVEN: u8 = 2;
91pub const RS232_V2_BRICKLET_STOPBITS_1: u8 = 1;
92pub const RS232_V2_BRICKLET_STOPBITS_2: u8 = 2;
93pub const RS232_V2_BRICKLET_WORDLENGTH_5: u8 = 5;
94pub const RS232_V2_BRICKLET_WORDLENGTH_6: u8 = 6;
95pub const RS232_V2_BRICKLET_WORDLENGTH_7: u8 = 7;
96pub const RS232_V2_BRICKLET_WORDLENGTH_8: u8 = 8;
97pub const RS232_V2_BRICKLET_FLOWCONTROL_OFF: u8 = 0;
98pub const RS232_V2_BRICKLET_FLOWCONTROL_SOFTWARE: u8 = 1;
99pub const RS232_V2_BRICKLET_FLOWCONTROL_HARDWARE: u8 = 2;
100pub const RS232_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
101pub const RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
102pub const RS232_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
103pub const RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
104pub const RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
105pub const RS232_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
106pub const RS232_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
107pub const RS232_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
108pub const RS232_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
109pub const RS232_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
110pub const RS232_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
111pub const RS232_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
112pub const RS232_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
113pub const RS232_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
114pub const RS232_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
115
116#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
117pub struct WriteLowLevel {
118    pub message_chunk_written: u8,
119}
120impl FromByteSlice for WriteLowLevel {
121    fn bytes_expected() -> usize {
122        1
123    }
124    fn from_le_byte_slice(bytes: &[u8]) -> WriteLowLevel {
125        WriteLowLevel { message_chunk_written: <u8>::from_le_byte_slice(&bytes[0..1]) }
126    }
127}
128
129#[derive(Clone, Copy)]
130pub struct ReadLowLevel {
131    pub message_length: u16,
132    pub message_chunk_offset: u16,
133    pub message_chunk_data: [char; 60],
134}
135impl FromByteSlice for ReadLowLevel {
136    fn bytes_expected() -> usize {
137        64
138    }
139    fn from_le_byte_slice(bytes: &[u8]) -> ReadLowLevel {
140        ReadLowLevel {
141            message_length: <u16>::from_le_byte_slice(&bytes[0..2]),
142            message_chunk_offset: <u16>::from_le_byte_slice(&bytes[2..4]),
143            message_chunk_data: <[char; 60]>::from_le_byte_slice(&bytes[4..64]),
144        }
145    }
146}
147impl LowLevelRead<char, ReadResult> for ReadLowLevel {
148    fn ll_message_length(&self) -> usize {
149        self.message_length as usize
150    }
151
152    fn ll_message_chunk_offset(&self) -> usize {
153        self.message_chunk_offset as usize
154    }
155
156    fn ll_message_chunk_data(&self) -> &[char] {
157        &self.message_chunk_data
158    }
159
160    fn get_result(&self) -> ReadResult {
161        ReadResult {}
162    }
163}
164
165#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
166pub struct Configuration {
167    pub baudrate: u32,
168    pub parity: u8,
169    pub stopbits: u8,
170    pub wordlength: u8,
171    pub flowcontrol: u8,
172}
173impl FromByteSlice for Configuration {
174    fn bytes_expected() -> usize {
175        8
176    }
177    fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
178        Configuration {
179            baudrate: <u32>::from_le_byte_slice(&bytes[0..4]),
180            parity: <u8>::from_le_byte_slice(&bytes[4..5]),
181            stopbits: <u8>::from_le_byte_slice(&bytes[5..6]),
182            wordlength: <u8>::from_le_byte_slice(&bytes[6..7]),
183            flowcontrol: <u8>::from_le_byte_slice(&bytes[7..8]),
184        }
185    }
186}
187
188#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
189pub struct BufferConfig {
190    pub send_buffer_size: u16,
191    pub receive_buffer_size: u16,
192}
193impl FromByteSlice for BufferConfig {
194    fn bytes_expected() -> usize {
195        4
196    }
197    fn from_le_byte_slice(bytes: &[u8]) -> BufferConfig {
198        BufferConfig {
199            send_buffer_size: <u16>::from_le_byte_slice(&bytes[0..2]),
200            receive_buffer_size: <u16>::from_le_byte_slice(&bytes[2..4]),
201        }
202    }
203}
204
205#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
206pub struct BufferStatus {
207    pub send_buffer_used: u16,
208    pub receive_buffer_used: u16,
209}
210impl FromByteSlice for BufferStatus {
211    fn bytes_expected() -> usize {
212        4
213    }
214    fn from_le_byte_slice(bytes: &[u8]) -> BufferStatus {
215        BufferStatus {
216            send_buffer_used: <u16>::from_le_byte_slice(&bytes[0..2]),
217            receive_buffer_used: <u16>::from_le_byte_slice(&bytes[2..4]),
218        }
219    }
220}
221
222#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
223pub struct ErrorCount {
224    pub error_count_overrun: u32,
225    pub error_count_parity: u32,
226}
227impl FromByteSlice for ErrorCount {
228    fn bytes_expected() -> usize {
229        8
230    }
231    fn from_le_byte_slice(bytes: &[u8]) -> ErrorCount {
232        ErrorCount {
233            error_count_overrun: <u32>::from_le_byte_slice(&bytes[0..4]),
234            error_count_parity: <u32>::from_le_byte_slice(&bytes[4..8]),
235        }
236    }
237}
238
239#[derive(Clone, Copy)]
240pub struct ReadLowLevelEvent {
241    pub message_length: u16,
242    pub message_chunk_offset: u16,
243    pub message_chunk_data: [char; 60],
244}
245impl FromByteSlice for ReadLowLevelEvent {
246    fn bytes_expected() -> usize {
247        64
248    }
249    fn from_le_byte_slice(bytes: &[u8]) -> ReadLowLevelEvent {
250        ReadLowLevelEvent {
251            message_length: <u16>::from_le_byte_slice(&bytes[0..2]),
252            message_chunk_offset: <u16>::from_le_byte_slice(&bytes[2..4]),
253            message_chunk_data: <[char; 60]>::from_le_byte_slice(&bytes[4..64]),
254        }
255    }
256}
257impl LowLevelRead<char, ReadResult> for ReadLowLevelEvent {
258    fn ll_message_length(&self) -> usize {
259        self.message_length as usize
260    }
261
262    fn ll_message_chunk_offset(&self) -> usize {
263        self.message_chunk_offset as usize
264    }
265
266    fn ll_message_chunk_data(&self) -> &[char] {
267        &self.message_chunk_data
268    }
269
270    fn get_result(&self) -> ReadResult {
271        ReadResult {}
272    }
273}
274
275#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
276pub struct ErrorCountEvent {
277    pub error_count_overrun: u32,
278    pub error_count_parity: u32,
279}
280impl FromByteSlice for ErrorCountEvent {
281    fn bytes_expected() -> usize {
282        8
283    }
284    fn from_le_byte_slice(bytes: &[u8]) -> ErrorCountEvent {
285        ErrorCountEvent {
286            error_count_overrun: <u32>::from_le_byte_slice(&bytes[0..4]),
287            error_count_parity: <u32>::from_le_byte_slice(&bytes[4..8]),
288        }
289    }
290}
291
292#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
293pub struct SpitfpErrorCount {
294    pub error_count_ack_checksum: u32,
295    pub error_count_message_checksum: u32,
296    pub error_count_frame: u32,
297    pub error_count_overflow: u32,
298}
299impl FromByteSlice for SpitfpErrorCount {
300    fn bytes_expected() -> usize {
301        16
302    }
303    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
304        SpitfpErrorCount {
305            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
306            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
307            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
308            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
309        }
310    }
311}
312
313#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
314pub struct Identity {
315    pub uid: String,
316    pub connected_uid: String,
317    pub position: char,
318    pub hardware_version: [u8; 3],
319    pub firmware_version: [u8; 3],
320    pub device_identifier: u16,
321}
322impl FromByteSlice for Identity {
323    fn bytes_expected() -> usize {
324        25
325    }
326    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
327        Identity {
328            uid: <String>::from_le_byte_slice(&bytes[0..8]),
329            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
330            position: <char>::from_le_byte_slice(&bytes[16..17]),
331            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
332            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
333            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
334        }
335    }
336}
337
338#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
339pub struct WriteResult {}
340
341#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
342pub struct ReadResult {}
343
344/// Communicates with RS232 devices
345#[derive(Clone)]
346pub struct Rs232V2Bricklet {
347    device: Device,
348}
349impl Rs232V2Bricklet {
350    pub const DEVICE_IDENTIFIER: u16 = 2108;
351    pub const DEVICE_DISPLAY_NAME: &'static str = "RS232 Bricklet 2.0";
352    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
353    pub fn new(uid: Uid, connection: AsyncIpConnection) -> Rs232V2Bricklet {
354        let mut result = Rs232V2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
355        result.device.response_expected[u8::from(Rs232V2BrickletFunction::WriteLowLevel) as usize] = ResponseExpectedFlag::AlwaysTrue;
356        result.device.response_expected[u8::from(Rs232V2BrickletFunction::ReadLowLevel) as usize] = ResponseExpectedFlag::AlwaysTrue;
357        result.device.response_expected[u8::from(Rs232V2BrickletFunction::EnableReadCallback) as usize] = ResponseExpectedFlag::True;
358        result.device.response_expected[u8::from(Rs232V2BrickletFunction::DisableReadCallback) as usize] = ResponseExpectedFlag::True;
359        result.device.response_expected[u8::from(Rs232V2BrickletFunction::IsReadCallbackEnabled) as usize] =
360            ResponseExpectedFlag::AlwaysTrue;
361        result.device.response_expected[u8::from(Rs232V2BrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
362        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetConfiguration) as usize] = ResponseExpectedFlag::AlwaysTrue;
363        result.device.response_expected[u8::from(Rs232V2BrickletFunction::SetBufferConfig) as usize] = ResponseExpectedFlag::False;
364        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetBufferConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
365        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetBufferStatus) as usize] = ResponseExpectedFlag::AlwaysTrue;
366        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
367        result.device.response_expected[u8::from(Rs232V2BrickletFunction::SetFrameReadableCallbackConfiguration) as usize] =
368            ResponseExpectedFlag::True;
369        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetFrameReadableCallbackConfiguration) as usize] =
370            ResponseExpectedFlag::AlwaysTrue;
371        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetSpitfpErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
372        result.device.response_expected[u8::from(Rs232V2BrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
373        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
374        result.device.response_expected[u8::from(Rs232V2BrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
375        result.device.response_expected[u8::from(Rs232V2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
376        result.device.response_expected[u8::from(Rs232V2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
377        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
378        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
379        result.device.response_expected[u8::from(Rs232V2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
380        result.device.response_expected[u8::from(Rs232V2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
381        result.device.response_expected[u8::from(Rs232V2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
382        result.device.response_expected[u8::from(Rs232V2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
383        result
384    }
385
386    /// Returns the response expected flag for the function specified by the function ID parameter.
387    /// It is true if the function is expected to send a response, false otherwise.
388    ///
389    /// For getter functions this is enabled by default and cannot be disabled, because those
390    /// functions will always send a response. For callback configuration functions it is enabled
391    /// by default too, but can be disabled by [`set_response_expected`](crate::rs232_v2_bricklet::Rs232V2Bricklet::set_response_expected).
392    /// For setter functions it is disabled by default and can be enabled.
393    ///
394    /// Enabling the response expected flag for a setter function allows to detect timeouts
395    /// and other error conditions calls of this setter as well. The device will then send a response
396    /// for this purpose. If this flag is disabled for a setter function then no response is sent
397    /// and errors are silently ignored, because they cannot be detected.
398    ///
399    /// See [`set_response_expected`](crate::rs232_v2_bricklet::Rs232V2Bricklet::set_response_expected) for the list of function ID constants available for this function.
400    pub fn get_response_expected(&mut self, fun: Rs232V2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
401        self.device.get_response_expected(u8::from(fun))
402    }
403
404    /// Changes the response expected flag of the function specified by the function ID parameter.
405    /// This flag can only be changed for setter (default value: false) and callback configuration
406    /// functions (default value: true). For getter functions it is always enabled.
407    ///
408    /// Enabling the response expected flag for a setter function allows to detect timeouts and
409    /// other error conditions calls of this setter as well. The device will then send a response
410    /// for this purpose. If this flag is disabled for a setter function then no response is sent
411    /// and errors are silently ignored, because they cannot be detected.
412    pub fn set_response_expected(&mut self, fun: Rs232V2BrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
413        self.device.set_response_expected(u8::from(fun), response_expected)
414    }
415
416    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
417    pub fn set_response_expected_all(&mut self, response_expected: bool) {
418        self.device.set_response_expected_all(response_expected)
419    }
420
421    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
422    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
423    pub fn get_api_version(&self) -> [u8; 3] {
424        self.device.api_version
425    }
426
427    /// See [`get_read_callback_receiver`](crate::rs232_v2::RS232V2::get_read_callback_receiver)
428    pub async fn get_read_low_level_callback_receiver(&mut self) -> impl Stream<Item = ReadLowLevelEvent> {
429        self.device
430            .get_callback_receiver(u8::from(Rs232V2BrickletFunction::CallbackReadLowLevel))
431            .await
432            .map(|p| ReadLowLevelEvent::from_le_byte_slice(p.body()))
433    }
434
435    /// This receiver is called if a new error occurs. It returns
436    /// the current overrun and parity error count.
437    pub async fn get_error_count_callback_receiver(&mut self) -> impl Stream<Item = ErrorCountEvent> {
438        self.device
439            .get_callback_receiver(u8::from(Rs232V2BrickletFunction::CallbackErrorCount))
440            .await
441            .map(|p| ErrorCountEvent::from_le_byte_slice(p.body()))
442    }
443
444    /// This receiver is called if at least one frame of data is readable. The frame size is configured with [`set_frame_readable_callback_configuration`].
445    /// The frame count parameter is the number of frames that can be read.
446    /// This receiver is triggered only once until [`read`] is called. This means, that if you have configured a frame size of X bytes,
447    /// you can read exactly X bytes using the [`read`] function, every time the receiver triggers without checking the frame count parameter.
448    ///
449    ///
450    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
451    pub async fn get_frame_readable_callback_receiver(&mut self) -> impl Stream<Item = u16> {
452        self.device
453            .get_callback_receiver(u8::from(Rs232V2BrickletFunction::CallbackFrameReadable))
454            .await
455            .map(|p| u16::from_le_byte_slice(p.body()))
456    }
457
458    /// Writes characters to the RS232 interface. The characters can be binary data,
459    /// ASCII or similar is not necessary.
460    ///
461    /// The return value is the number of characters that were written.
462    ///
463    /// See [`set_configuration`] for configuration possibilities
464    /// regarding baud rate, parity and so on.
465    pub async fn write_low_level(
466        &mut self,
467        message_length: u16,
468        message_chunk_offset: u16,
469        message_chunk_data: &[char; 60],
470    ) -> Result<WriteLowLevel, TinkerforgeError> {
471        let mut payload = [0; 64];
472        message_length.write_to_slice(&mut payload[0..2]);
473        message_chunk_offset.write_to_slice(&mut payload[2..4]);
474        message_chunk_data.write_to_slice(&mut payload[4..64]);
475
476        #[allow(unused_variables)]
477        let result = self.device.get(u8::from(Rs232V2BrickletFunction::WriteLowLevel), &payload).await?;
478        Ok(WriteLowLevel::from_le_byte_slice(result.body()))
479    }
480
481    /// Returns up to *length* characters from receive buffer.
482    ///
483    /// Instead of polling with this function, you can also use
484    /// callbacks. But note that this function will return available
485    /// data only when the read receiver is disabled.
486    /// See [`enable_read_callback`] and [`get_read_callback_receiver`] receiver.
487    pub async fn read_low_level(&mut self, length: u16) -> Result<ReadLowLevel, TinkerforgeError> {
488        let mut payload = [0; 2];
489        length.write_to_slice(&mut payload[0..2]);
490
491        #[allow(unused_variables)]
492        let result = self.device.get(u8::from(Rs232V2BrickletFunction::ReadLowLevel), &payload).await?;
493        Ok(ReadLowLevel::from_le_byte_slice(result.body()))
494    }
495
496    /// Enables the [`get_read_callback_receiver`] receiver. This will disable the [`get_frame_readable_callback_receiver`] receiver.
497    ///
498    /// By default the receiver is disabled.
499    pub async fn enable_read_callback(&mut self) -> Result<(), TinkerforgeError> {
500        let payload = [0; 0];
501
502        #[allow(unused_variables)]
503        let result = self.device.set(u8::from(Rs232V2BrickletFunction::EnableReadCallback), &payload).await?;
504        Ok(())
505    }
506
507    /// Disables the [`get_read_callback_receiver`] receiver.
508    ///
509    /// By default the receiver is disabled.
510    pub async fn disable_read_callback(&mut self) -> Result<(), TinkerforgeError> {
511        let payload = [0; 0];
512
513        #[allow(unused_variables)]
514        let result = self.device.set(u8::from(Rs232V2BrickletFunction::DisableReadCallback), &payload).await?;
515        Ok(())
516    }
517
518    /// Returns *true* if the [`get_read_callback_receiver`] receiver is enabled,
519    /// *false* otherwise.
520    pub async fn is_read_callback_enabled(&mut self) -> Result<bool, TinkerforgeError> {
521        let payload = [0; 0];
522
523        #[allow(unused_variables)]
524        let result = self.device.get(u8::from(Rs232V2BrickletFunction::IsReadCallbackEnabled), &payload).await?;
525        Ok(bool::from_le_byte_slice(result.body()))
526    }
527
528    /// Sets the configuration for the RS232 communication.
529    ///
530    /// Associated constants:
531    /// * RS232_V2_BRICKLET_PARITY_NONE
532    ///	* RS232_V2_BRICKLET_PARITY_ODD
533    ///	* RS232_V2_BRICKLET_PARITY_EVEN
534    ///	* RS232_V2_BRICKLET_STOPBITS_1
535    ///	* RS232_V2_BRICKLET_STOPBITS_2
536    ///	* RS232_V2_BRICKLET_WORDLENGTH_5
537    ///	* RS232_V2_BRICKLET_WORDLENGTH_6
538    ///	* RS232_V2_BRICKLET_WORDLENGTH_7
539    ///	* RS232_V2_BRICKLET_WORDLENGTH_8
540    ///	* RS232_V2_BRICKLET_FLOWCONTROL_OFF
541    ///	* RS232_V2_BRICKLET_FLOWCONTROL_SOFTWARE
542    ///	* RS232_V2_BRICKLET_FLOWCONTROL_HARDWARE
543    pub async fn set_configuration(
544        &mut self,
545        baudrate: u32,
546        parity: u8,
547        stopbits: u8,
548        wordlength: u8,
549        flowcontrol: u8,
550    ) -> Result<(), TinkerforgeError> {
551        let mut payload = [0; 8];
552        baudrate.write_to_slice(&mut payload[0..4]);
553        parity.write_to_slice(&mut payload[4..5]);
554        stopbits.write_to_slice(&mut payload[5..6]);
555        wordlength.write_to_slice(&mut payload[6..7]);
556        flowcontrol.write_to_slice(&mut payload[7..8]);
557
558        #[allow(unused_variables)]
559        let result = self.device.set(u8::from(Rs232V2BrickletFunction::SetConfiguration), &payload).await?;
560        Ok(())
561    }
562
563    /// Returns the configuration as set by [`set_configuration`].
564    ///
565    /// Associated constants:
566    /// * RS232_V2_BRICKLET_PARITY_NONE
567    ///	* RS232_V2_BRICKLET_PARITY_ODD
568    ///	* RS232_V2_BRICKLET_PARITY_EVEN
569    ///	* RS232_V2_BRICKLET_STOPBITS_1
570    ///	* RS232_V2_BRICKLET_STOPBITS_2
571    ///	* RS232_V2_BRICKLET_WORDLENGTH_5
572    ///	* RS232_V2_BRICKLET_WORDLENGTH_6
573    ///	* RS232_V2_BRICKLET_WORDLENGTH_7
574    ///	* RS232_V2_BRICKLET_WORDLENGTH_8
575    ///	* RS232_V2_BRICKLET_FLOWCONTROL_OFF
576    ///	* RS232_V2_BRICKLET_FLOWCONTROL_SOFTWARE
577    ///	* RS232_V2_BRICKLET_FLOWCONTROL_HARDWARE
578    pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
579        let payload = [0; 0];
580
581        #[allow(unused_variables)]
582        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetConfiguration), &payload).await?;
583        Ok(Configuration::from_le_byte_slice(result.body()))
584    }
585
586    /// Sets the send and receive buffer size in byte. In total the buffers have to be
587    /// 10240 byte (10KiB) in size, the minimum buffer size is 1024 byte (1KiB) for each.
588    ///
589    /// The current buffer content is lost if this function is called.
590    ///
591    /// The send buffer holds data that is given by [`write`] and
592    /// can not be written yet. The receive buffer holds data that is
593    /// received through RS232 but could not yet be send to the
594    /// user, either by [`read`] or through [`get_read_callback_receiver`] receiver.
595    pub async fn set_buffer_config(&mut self, send_buffer_size: u16, receive_buffer_size: u16) -> Result<(), TinkerforgeError> {
596        let mut payload = [0; 4];
597        send_buffer_size.write_to_slice(&mut payload[0..2]);
598        receive_buffer_size.write_to_slice(&mut payload[2..4]);
599
600        #[allow(unused_variables)]
601        let result = self.device.set(u8::from(Rs232V2BrickletFunction::SetBufferConfig), &payload).await?;
602        Ok(())
603    }
604
605    /// Returns the buffer configuration as set by [`set_buffer_config`].
606    pub async fn get_buffer_config(&mut self) -> Result<BufferConfig, TinkerforgeError> {
607        let payload = [0; 0];
608
609        #[allow(unused_variables)]
610        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetBufferConfig), &payload).await?;
611        Ok(BufferConfig::from_le_byte_slice(result.body()))
612    }
613
614    /// Returns the currently used bytes for the send and received buffer.
615    ///
616    /// See [`set_buffer_config`] for buffer size configuration.
617    pub async fn get_buffer_status(&mut self) -> Result<BufferStatus, TinkerforgeError> {
618        let payload = [0; 0];
619
620        #[allow(unused_variables)]
621        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetBufferStatus), &payload).await?;
622        Ok(BufferStatus::from_le_byte_slice(result.body()))
623    }
624
625    /// Returns the current number of overrun and parity errors.
626    pub async fn get_error_count(&mut self) -> Result<ErrorCount, TinkerforgeError> {
627        let payload = [0; 0];
628
629        #[allow(unused_variables)]
630        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetErrorCount), &payload).await?;
631        Ok(ErrorCount::from_le_byte_slice(result.body()))
632    }
633
634    /// Configures the [`get_frame_readable_callback_receiver`] receiver. The frame size is the number of bytes, that have to be readable to trigger the receiver.
635    /// A frame size of 0 disables the receiver. A frame size greater than 0 enables the receiver and disables the [`get_read_callback_receiver`] receiver.
636    ///
637    /// By default the receiver is disabled.
638    ///
639    ///
640    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
641    pub async fn set_frame_readable_callback_configuration(&mut self, frame_size: u16) -> Result<(), TinkerforgeError> {
642        let mut payload = [0; 2];
643        frame_size.write_to_slice(&mut payload[0..2]);
644
645        #[allow(unused_variables)]
646        let result = self.device.set(u8::from(Rs232V2BrickletFunction::SetFrameReadableCallbackConfiguration), &payload).await?;
647        Ok(())
648    }
649
650    /// Returns the receiver configuration as set by [`set_frame_readable_callback_configuration`].
651    ///
652    ///
653    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
654    pub async fn get_frame_readable_callback_configuration(&mut self) -> Result<u16, TinkerforgeError> {
655        let payload = [0; 0];
656
657        #[allow(unused_variables)]
658        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetFrameReadableCallbackConfiguration), &payload).await?;
659        Ok(u16::from_le_byte_slice(result.body()))
660    }
661
662    /// Returns the error count for the communication between Brick and Bricklet.
663    ///
664    /// The errors are divided into
665    ///
666    /// * ACK checksum errors,
667    /// * message checksum errors,
668    /// * framing errors and
669    /// * overflow errors.
670    ///
671    /// The errors counts are for errors that occur on the Bricklet side. All
672    /// Bricks have a similar function that returns the errors on the Brick side.
673    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
674        let payload = [0; 0];
675
676        #[allow(unused_variables)]
677        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
678        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
679    }
680
681    /// Sets the bootloader mode and returns the status after the requested
682    /// mode change was instigated.
683    ///
684    /// You can change from bootloader mode to firmware mode and vice versa. A change
685    /// from bootloader mode to firmware mode will only take place if the entry function,
686    /// device identifier and CRC are present and correct.
687    ///
688    /// This function is used by Brick Viewer during flashing. It should not be
689    /// necessary to call it in a normal user program.
690    ///
691    /// Associated constants:
692    /// * RS232_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
693    ///	* RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
694    ///	* RS232_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
695    ///	* RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
696    ///	* RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
697    ///	* RS232_V2_BRICKLET_BOOTLOADER_STATUS_OK
698    ///	* RS232_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
699    ///	* RS232_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
700    ///	* RS232_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
701    ///	* RS232_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
702    ///	* RS232_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
703    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
704        let mut payload = [0; 1];
705        mode.write_to_slice(&mut payload[0..1]);
706
707        #[allow(unused_variables)]
708        let result = self.device.get(u8::from(Rs232V2BrickletFunction::SetBootloaderMode), &payload).await?;
709        Ok(u8::from_le_byte_slice(result.body()))
710    }
711
712    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
713    ///
714    /// Associated constants:
715    /// * RS232_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
716    ///	* RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
717    ///	* RS232_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
718    ///	* RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
719    ///	* RS232_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
720    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
721        let payload = [0; 0];
722
723        #[allow(unused_variables)]
724        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetBootloaderMode), &payload).await?;
725        Ok(u8::from_le_byte_slice(result.body()))
726    }
727
728    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
729    /// to be increased by chunks of size 64. The data is written to flash
730    /// every 4 chunks (which equals to one page of size 256).
731    ///
732    /// This function is used by Brick Viewer during flashing. It should not be
733    /// necessary to call it in a normal user program.
734    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
735        let mut payload = [0; 4];
736        pointer.write_to_slice(&mut payload[0..4]);
737
738        #[allow(unused_variables)]
739        let result = self.device.set(u8::from(Rs232V2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
740        Ok(())
741    }
742
743    /// Writes 64 Bytes of firmware at the position as written by
744    /// [`set_write_firmware_pointer`] before. The firmware is written
745    /// to flash every 4 chunks.
746    ///
747    /// You can only write firmware in bootloader mode.
748    ///
749    /// This function is used by Brick Viewer during flashing. It should not be
750    /// necessary to call it in a normal user program.
751    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
752        let mut payload = [0; 64];
753        data.write_to_slice(&mut payload[0..64]);
754
755        #[allow(unused_variables)]
756        let result = self.device.get(u8::from(Rs232V2BrickletFunction::WriteFirmware), &payload).await?;
757        Ok(u8::from_le_byte_slice(result.body()))
758    }
759
760    /// Sets the status LED configuration. By default the LED shows
761    /// communication traffic between Brick and Bricklet, it flickers once
762    /// for every 10 received data packets.
763    ///
764    /// You can also turn the LED permanently on/off or show a heartbeat.
765    ///
766    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
767    ///
768    /// Associated constants:
769    /// * RS232_V2_BRICKLET_STATUS_LED_CONFIG_OFF
770    ///	* RS232_V2_BRICKLET_STATUS_LED_CONFIG_ON
771    ///	* RS232_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
772    ///	* RS232_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
773    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
774        let mut payload = [0; 1];
775        config.write_to_slice(&mut payload[0..1]);
776
777        #[allow(unused_variables)]
778        let result = self.device.set(u8::from(Rs232V2BrickletFunction::SetStatusLedConfig), &payload).await?;
779        Ok(())
780    }
781
782    /// Returns the configuration as set by [`set_status_led_config`]
783    ///
784    /// Associated constants:
785    /// * RS232_V2_BRICKLET_STATUS_LED_CONFIG_OFF
786    ///	* RS232_V2_BRICKLET_STATUS_LED_CONFIG_ON
787    ///	* RS232_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
788    ///	* RS232_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
789    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
790        let payload = [0; 0];
791
792        #[allow(unused_variables)]
793        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetStatusLedConfig), &payload).await?;
794        Ok(u8::from_le_byte_slice(result.body()))
795    }
796
797    /// Returns the temperature as measured inside the microcontroller. The
798    /// value returned is not the ambient temperature!
799    ///
800    /// The temperature is only proportional to the real temperature and it has bad
801    /// accuracy. Practically it is only useful as an indicator for
802    /// temperature changes.
803    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
804        let payload = [0; 0];
805
806        #[allow(unused_variables)]
807        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetChipTemperature), &payload).await?;
808        Ok(i16::from_le_byte_slice(result.body()))
809    }
810
811    /// Calling this function will reset the Bricklet. All configurations
812    /// will be lost.
813    ///
814    /// After a reset you have to create new device objects,
815    /// calling functions on the existing ones will result in
816    /// undefined behavior!
817    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
818        let payload = [0; 0];
819
820        #[allow(unused_variables)]
821        let result = self.device.set(u8::from(Rs232V2BrickletFunction::Reset), &payload).await?;
822        Ok(())
823    }
824
825    /// Writes a new UID into flash. If you want to set a new UID
826    /// you have to decode the Base58 encoded UID string into an
827    /// integer first.
828    ///
829    /// We recommend that you use Brick Viewer to change the UID.
830    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
831        let mut payload = [0; 4];
832        uid.write_to_slice(&mut payload[0..4]);
833
834        #[allow(unused_variables)]
835        let result = self.device.set(u8::from(Rs232V2BrickletFunction::WriteUid), &payload).await?;
836        Ok(())
837    }
838
839    /// Returns the current UID as an integer. Encode as
840    /// Base58 to get the usual string version.
841    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
842        let payload = [0; 0];
843
844        #[allow(unused_variables)]
845        let result = self.device.get(u8::from(Rs232V2BrickletFunction::ReadUid), &payload).await?;
846        Ok(u32::from_le_byte_slice(result.body()))
847    }
848
849    /// Returns the UID, the UID where the Bricklet is connected to,
850    /// the position, the hardware and firmware version as well as the
851    /// device identifier.
852    ///
853    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
854    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
855    /// position 'z'.
856    ///
857    /// The device identifier numbers can be found [here](device_identifier).
858    /// |device_identifier_constant|
859    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
860        let payload = [0; 0];
861
862        #[allow(unused_variables)]
863        let result = self.device.get(u8::from(Rs232V2BrickletFunction::GetIdentity), &payload).await?;
864        Ok(Identity::from_le_byte_slice(result.body()))
865    }
866}