tinkerforge_async/bindings/
rs232_bricklet.rs1#[allow(unused_imports)]
15use crate::{
16 base58::Uid, byte_converter::*, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
17 low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum Rs232BrickletFunction {
24 Write,
25 Read,
26 EnableReadCallback,
27 DisableReadCallback,
28 IsReadCallbackEnabled,
29 SetConfiguration,
30 GetConfiguration,
31 SetBreakCondition,
32 SetFrameReadableCallbackConfiguration,
33 GetFrameReadableCallbackConfiguration,
34 ReadFrame,
35 GetIdentity,
36 CallbackRead,
37 CallbackError,
38 CallbackFrameReadable,
39}
40impl From<Rs232BrickletFunction> for u8 {
41 fn from(fun: Rs232BrickletFunction) -> Self {
42 match fun {
43 Rs232BrickletFunction::Write => 1,
44 Rs232BrickletFunction::Read => 2,
45 Rs232BrickletFunction::EnableReadCallback => 3,
46 Rs232BrickletFunction::DisableReadCallback => 4,
47 Rs232BrickletFunction::IsReadCallbackEnabled => 5,
48 Rs232BrickletFunction::SetConfiguration => 6,
49 Rs232BrickletFunction::GetConfiguration => 7,
50 Rs232BrickletFunction::SetBreakCondition => 10,
51 Rs232BrickletFunction::SetFrameReadableCallbackConfiguration => 11,
52 Rs232BrickletFunction::GetFrameReadableCallbackConfiguration => 12,
53 Rs232BrickletFunction::ReadFrame => 14,
54 Rs232BrickletFunction::GetIdentity => 255,
55 Rs232BrickletFunction::CallbackRead => 8,
56 Rs232BrickletFunction::CallbackError => 9,
57 Rs232BrickletFunction::CallbackFrameReadable => 13,
58 }
59 }
60}
61pub const RS232_BRICKLET_BAUDRATE_300: u8 = 0;
62pub const RS232_BRICKLET_BAUDRATE_600: u8 = 1;
63pub const RS232_BRICKLET_BAUDRATE_1200: u8 = 2;
64pub const RS232_BRICKLET_BAUDRATE_2400: u8 = 3;
65pub const RS232_BRICKLET_BAUDRATE_4800: u8 = 4;
66pub const RS232_BRICKLET_BAUDRATE_9600: u8 = 5;
67pub const RS232_BRICKLET_BAUDRATE_14400: u8 = 6;
68pub const RS232_BRICKLET_BAUDRATE_19200: u8 = 7;
69pub const RS232_BRICKLET_BAUDRATE_28800: u8 = 8;
70pub const RS232_BRICKLET_BAUDRATE_38400: u8 = 9;
71pub const RS232_BRICKLET_BAUDRATE_57600: u8 = 10;
72pub const RS232_BRICKLET_BAUDRATE_115200: u8 = 11;
73pub const RS232_BRICKLET_BAUDRATE_230400: u8 = 12;
74pub const RS232_BRICKLET_PARITY_NONE: u8 = 0;
75pub const RS232_BRICKLET_PARITY_ODD: u8 = 1;
76pub const RS232_BRICKLET_PARITY_EVEN: u8 = 2;
77pub const RS232_BRICKLET_PARITY_FORCED_PARITY_1: u8 = 3;
78pub const RS232_BRICKLET_PARITY_FORCED_PARITY_0: u8 = 4;
79pub const RS232_BRICKLET_STOPBITS_1: u8 = 1;
80pub const RS232_BRICKLET_STOPBITS_2: u8 = 2;
81pub const RS232_BRICKLET_WORDLENGTH_5: u8 = 5;
82pub const RS232_BRICKLET_WORDLENGTH_6: u8 = 6;
83pub const RS232_BRICKLET_WORDLENGTH_7: u8 = 7;
84pub const RS232_BRICKLET_WORDLENGTH_8: u8 = 8;
85pub const RS232_BRICKLET_HARDWARE_FLOWCONTROL_OFF: u8 = 0;
86pub const RS232_BRICKLET_HARDWARE_FLOWCONTROL_ON: u8 = 1;
87pub const RS232_BRICKLET_SOFTWARE_FLOWCONTROL_OFF: u8 = 0;
88pub const RS232_BRICKLET_SOFTWARE_FLOWCONTROL_ON: u8 = 1;
89pub const RS232_BRICKLET_ERROR_OVERRUN: u8 = 1;
90pub const RS232_BRICKLET_ERROR_PARITY: u8 = 2;
91pub const RS232_BRICKLET_ERROR_FRAMING: u8 = 4;
92
93#[derive(Clone, Copy)]
94pub struct Read {
95 pub message: [char; 60],
96 pub length: u8,
97}
98impl FromByteSlice for Read {
99 fn bytes_expected() -> usize {
100 61
101 }
102 fn from_le_byte_slice(bytes: &[u8]) -> Read {
103 Read { message: <[char; 60]>::from_le_byte_slice(&bytes[0..60]), length: <u8>::from_le_byte_slice(&bytes[60..61]) }
104 }
105}
106
107#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
108pub struct Configuration {
109 pub baudrate: u8,
110 pub parity: u8,
111 pub stopbits: u8,
112 pub wordlength: u8,
113 pub hardware_flowcontrol: u8,
114 pub software_flowcontrol: u8,
115}
116impl FromByteSlice for Configuration {
117 fn bytes_expected() -> usize {
118 6
119 }
120 fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
121 Configuration {
122 baudrate: <u8>::from_le_byte_slice(&bytes[0..1]),
123 parity: <u8>::from_le_byte_slice(&bytes[1..2]),
124 stopbits: <u8>::from_le_byte_slice(&bytes[2..3]),
125 wordlength: <u8>::from_le_byte_slice(&bytes[3..4]),
126 hardware_flowcontrol: <u8>::from_le_byte_slice(&bytes[4..5]),
127 software_flowcontrol: <u8>::from_le_byte_slice(&bytes[5..6]),
128 }
129 }
130}
131
132#[derive(Clone, Copy)]
133pub struct ReadEvent {
134 pub message: [char; 60],
135 pub length: u8,
136}
137impl FromByteSlice for ReadEvent {
138 fn bytes_expected() -> usize {
139 61
140 }
141 fn from_le_byte_slice(bytes: &[u8]) -> ReadEvent {
142 ReadEvent { message: <[char; 60]>::from_le_byte_slice(&bytes[0..60]), length: <u8>::from_le_byte_slice(&bytes[60..61]) }
143 }
144}
145
146#[derive(Clone, Copy)]
147pub struct ReadFrame {
148 pub message: [char; 60],
149 pub length: u8,
150}
151impl FromByteSlice for ReadFrame {
152 fn bytes_expected() -> usize {
153 61
154 }
155 fn from_le_byte_slice(bytes: &[u8]) -> ReadFrame {
156 ReadFrame { message: <[char; 60]>::from_le_byte_slice(&bytes[0..60]), length: <u8>::from_le_byte_slice(&bytes[60..61]) }
157 }
158}
159
160#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
161pub struct Identity {
162 pub uid: String,
163 pub connected_uid: String,
164 pub position: char,
165 pub hardware_version: [u8; 3],
166 pub firmware_version: [u8; 3],
167 pub device_identifier: u16,
168}
169impl FromByteSlice for Identity {
170 fn bytes_expected() -> usize {
171 25
172 }
173 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
174 Identity {
175 uid: <String>::from_le_byte_slice(&bytes[0..8]),
176 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
177 position: <char>::from_le_byte_slice(&bytes[16..17]),
178 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
179 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
180 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
181 }
182 }
183}
184
185#[derive(Clone)]
187pub struct Rs232Bricklet {
188 device: Device,
189}
190impl Rs232Bricklet {
191 pub const DEVICE_IDENTIFIER: u16 = 254;
192 pub const DEVICE_DISPLAY_NAME: &'static str = "RS232 Bricklet";
193 pub fn new(uid: Uid, connection: AsyncIpConnection) -> Rs232Bricklet {
195 let mut result = Rs232Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
196 result.device.response_expected[u8::from(Rs232BrickletFunction::Write) as usize] = ResponseExpectedFlag::AlwaysTrue;
197 result.device.response_expected[u8::from(Rs232BrickletFunction::Read) as usize] = ResponseExpectedFlag::AlwaysTrue;
198 result.device.response_expected[u8::from(Rs232BrickletFunction::EnableReadCallback) as usize] = ResponseExpectedFlag::True;
199 result.device.response_expected[u8::from(Rs232BrickletFunction::DisableReadCallback) as usize] = ResponseExpectedFlag::True;
200 result.device.response_expected[u8::from(Rs232BrickletFunction::IsReadCallbackEnabled) as usize] = ResponseExpectedFlag::AlwaysTrue;
201 result.device.response_expected[u8::from(Rs232BrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
202 result.device.response_expected[u8::from(Rs232BrickletFunction::GetConfiguration) as usize] = ResponseExpectedFlag::AlwaysTrue;
203 result.device.response_expected[u8::from(Rs232BrickletFunction::SetBreakCondition) as usize] = ResponseExpectedFlag::False;
204 result.device.response_expected[u8::from(Rs232BrickletFunction::SetFrameReadableCallbackConfiguration) as usize] =
205 ResponseExpectedFlag::True;
206 result.device.response_expected[u8::from(Rs232BrickletFunction::GetFrameReadableCallbackConfiguration) as usize] =
207 ResponseExpectedFlag::AlwaysTrue;
208 result.device.response_expected[u8::from(Rs232BrickletFunction::ReadFrame) as usize] = ResponseExpectedFlag::AlwaysTrue;
209 result.device.response_expected[u8::from(Rs232BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
210 result
211 }
212
213 pub fn get_response_expected(&mut self, fun: Rs232BrickletFunction) -> Result<bool, GetResponseExpectedError> {
228 self.device.get_response_expected(u8::from(fun))
229 }
230
231 pub fn set_response_expected(&mut self, fun: Rs232BrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
240 self.device.set_response_expected(u8::from(fun), response_expected)
241 }
242
243 pub fn set_response_expected_all(&mut self, response_expected: bool) {
245 self.device.set_response_expected_all(response_expected)
246 }
247
248 pub fn get_api_version(&self) -> [u8; 3] {
251 self.device.api_version
252 }
253
254 pub async fn get_read_callback_receiver(&mut self) -> impl Stream<Item = ReadEvent> {
262 self.device
263 .get_callback_receiver(u8::from(Rs232BrickletFunction::CallbackRead))
264 .await
265 .map(|p| ReadEvent::from_le_byte_slice(p.body()))
266 }
267
268 pub async fn get_error_callback_receiver(&mut self) -> impl Stream<Item = u8> {
274 self.device.get_callback_receiver(u8::from(Rs232BrickletFunction::CallbackError)).await.map(|p| u8::from_le_byte_slice(p.body()))
275 }
276
277 pub async fn get_frame_readable_callback_receiver(&mut self) -> impl Stream<Item = u8> {
285 self.device
286 .get_callback_receiver(u8::from(Rs232BrickletFunction::CallbackFrameReadable))
287 .await
288 .map(|p| u8::from_le_byte_slice(p.body()))
289 }
290
291 pub async fn write(&mut self, message: &[char; 60], length: u8) -> Result<u8, TinkerforgeError> {
301 let mut payload = [0; 61];
302 message.write_to_slice(&mut payload[0..60]);
303 length.write_to_slice(&mut payload[60..61]);
304
305 #[allow(unused_variables)]
306 let result = self.device.get(u8::from(Rs232BrickletFunction::Write), &payload).await?;
307 Ok(u8::from_le_byte_slice(result.body()))
308 }
309
310 pub async fn read(&mut self) -> Result<Read, TinkerforgeError> {
316 let payload = [0; 0];
317
318 #[allow(unused_variables)]
319 let result = self.device.get(u8::from(Rs232BrickletFunction::Read), &payload).await?;
320 Ok(Read::from_le_byte_slice(result.body()))
321 }
322
323 pub async fn enable_read_callback(&mut self) -> Result<(), TinkerforgeError> {
327 let payload = [0; 0];
328
329 #[allow(unused_variables)]
330 let result = self.device.set(u8::from(Rs232BrickletFunction::EnableReadCallback), &payload).await?;
331 Ok(())
332 }
333
334 pub async fn disable_read_callback(&mut self) -> Result<(), TinkerforgeError> {
338 let payload = [0; 0];
339
340 #[allow(unused_variables)]
341 let result = self.device.set(u8::from(Rs232BrickletFunction::DisableReadCallback), &payload).await?;
342 Ok(())
343 }
344
345 pub async fn is_read_callback_enabled(&mut self) -> Result<bool, TinkerforgeError> {
348 let payload = [0; 0];
349
350 #[allow(unused_variables)]
351 let result = self.device.get(u8::from(Rs232BrickletFunction::IsReadCallbackEnabled), &payload).await?;
352 Ok(bool::from_le_byte_slice(result.body()))
353 }
354
355 pub async fn set_configuration(
389 &mut self,
390 baudrate: u8,
391 parity: u8,
392 stopbits: u8,
393 wordlength: u8,
394 hardware_flowcontrol: u8,
395 software_flowcontrol: u8,
396 ) -> Result<(), TinkerforgeError> {
397 let mut payload = [0; 6];
398 baudrate.write_to_slice(&mut payload[0..1]);
399 parity.write_to_slice(&mut payload[1..2]);
400 stopbits.write_to_slice(&mut payload[2..3]);
401 wordlength.write_to_slice(&mut payload[3..4]);
402 hardware_flowcontrol.write_to_slice(&mut payload[4..5]);
403 software_flowcontrol.write_to_slice(&mut payload[5..6]);
404
405 #[allow(unused_variables)]
406 let result = self.device.set(u8::from(Rs232BrickletFunction::SetConfiguration), &payload).await?;
407 Ok(())
408 }
409
410 pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
442 let payload = [0; 0];
443
444 #[allow(unused_variables)]
445 let result = self.device.get(u8::from(Rs232BrickletFunction::GetConfiguration), &payload).await?;
446 Ok(Configuration::from_le_byte_slice(result.body()))
447 }
448
449 pub async fn set_break_condition(&mut self, break_time: u16) -> Result<(), TinkerforgeError> {
455 let mut payload = [0; 2];
456 break_time.write_to_slice(&mut payload[0..2]);
457
458 #[allow(unused_variables)]
459 let result = self.device.set(u8::from(Rs232BrickletFunction::SetBreakCondition), &payload).await?;
460 Ok(())
461 }
462
463 pub async fn set_frame_readable_callback_configuration(&mut self, frame_size: u8) -> Result<(), TinkerforgeError> {
471 let mut payload = [0; 1];
472 frame_size.write_to_slice(&mut payload[0..1]);
473
474 #[allow(unused_variables)]
475 let result = self.device.set(u8::from(Rs232BrickletFunction::SetFrameReadableCallbackConfiguration), &payload).await?;
476 Ok(())
477 }
478
479 pub async fn get_frame_readable_callback_configuration(&mut self) -> Result<u8, TinkerforgeError> {
484 let payload = [0; 0];
485
486 #[allow(unused_variables)]
487 let result = self.device.get(u8::from(Rs232BrickletFunction::GetFrameReadableCallbackConfiguration), &payload).await?;
488 Ok(u8::from_le_byte_slice(result.body()))
489 }
490
491 pub async fn read_frame(&mut self) -> Result<ReadFrame, TinkerforgeError> {
498 let payload = [0; 0];
499
500 #[allow(unused_variables)]
501 let result = self.device.get(u8::from(Rs232BrickletFunction::ReadFrame), &payload).await?;
502 Ok(ReadFrame::from_le_byte_slice(result.body()))
503 }
504
505 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
516 let payload = [0; 0];
517
518 #[allow(unused_variables)]
519 let result = self.device.get(u8::from(Rs232BrickletFunction::GetIdentity), &payload).await?;
520 Ok(Identity::from_le_byte_slice(result.body()))
521 }
522}