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
//
// Copyright (c) 2025, Astute Systems PTY LTD
//
// This file is part of the VivoeX SDK project developed by Astute Systems.
//
// See the commercial LICENSE file in the project root for full license details.
//
//! Send and receive serial data using a serial connection
use serialport::{DataBits, FlowControl, Parity, SerialPort, SerialPortBuilder, StopBits};
use std::io::Result;
/// Serial port connection
/// * `port` - Serial port name
/// * `baud` - Baud rate
///
pub struct Serial {
// Serial port settings
serial_port: SerialPortBuilder,
open_port: Option<Box<dyn SerialPort>>,
}
impl Serial {
pub fn new(
port: String,
baud: u32,
data_bits: u8,
parity: u8,
stop_bits: u8,
flow_control: u8,
) -> Result<Self> {
let serial_port = serialport::new(&port, baud)
.data_bits(match data_bits {
5 => DataBits::Five,
6 => DataBits::Six,
7 => DataBits::Seven,
8 => DataBits::Eight,
_ => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid data bits",
))
}
})
.parity(match parity {
0 => Parity::None,
1 => Parity::Odd,
2 => Parity::Even,
_ => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid parity",
))
}
})
.stop_bits(match stop_bits {
1 => StopBits::One,
2 => StopBits::Two,
_ => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid stop bits",
))
}
})
.flow_control(match flow_control {
0 => FlowControl::None,
1 => FlowControl::Software,
2 => FlowControl::Hardware,
_ => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid flow control",
))
}
});
Ok(Serial {
serial_port,
open_port: None,
})
}
/// Open the serial port
/// # Arguments
/// * `self` - The serial port
/// # Returns
/// * `Result<()>` - Result of opening the serial port
/// # Example
/// ```
/// let mut serial = Serial::new("/dev/ttyUSB0".to_string(), 9600, 8, 0, 1, 0).unwrap();
/// serial.open().unwrap();
/// ```
///
pub fn open(&mut self) -> Result<()> {
self.open_port = Some(self.serial_port.clone().open()?);
Ok(())
}
/// Send data over the serial port
/// # Arguments
/// * `self` - The serial port
/// * `message` - The message to send
/// # Returns
/// * `Result<usize>` - Result of sending the message
/// # Example
/// ```
/// let mut serial = Serial::new("/dev/ttyUSB0".to_string(), 9600, 8, 0, 1, 0).unwrap();
/// serial.open().unwrap();
/// serial.send(b"Hello World").unwrap();
/// ```
///
pub fn send(&mut self, message: &[u8]) -> Result<usize> {
if self.open_port.is_none() {
self.open()?;
}
if let Some(ref mut port) = self.open_port {
port.write(message)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to open port",
))
}
}
/// Receive data over the serial port
/// # Arguments
/// * `self` - The serial port
/// * `message` - The message to receive
/// # Returns
/// * `Result<usize>` - Result of receiving the message
/// # Example
/// ```
/// let mut serial = Serial::new("/dev/ttyUSB0".to_string(), 9600, 8, 0, 1, 0).unwrap();
/// serial.open().unwrap();
/// let mut message = [0; 1024];
/// serial.receive(&mut message).unwrap();
/// ```
///
pub fn receive(&mut self, message: &mut [u8]) -> Result<usize> {
if self.open_port.is_none() {
self.open()?;
}
if let Some(ref mut port) = self.open_port {
port.read(message)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to open port",
))
}
}
}