#[cfg(unix)]
fn main() {
use std::io::{Read, Write};
use std::os::unix::prelude::*;
use std::str;
use std::thread;
use std::time;
use serialport::{SerialPort, TTYPort};
let (mut master, mut slave) = TTYPort::pair().expect("Unable to create pseudo-terminal pair");
println!(
"Master ptty fd: {}, path: {:?}",
master.as_raw_fd(),
master.name()
);
println!(
"Slave ptty fd: {}, path: {:?}",
slave.as_raw_fd(),
slave.name()
);
let mut buf = [0u8; 512];
println!("Sending 5 messages from master to slave.");
for x in 1..6 {
let msg = format!("Message #{}", x);
assert_eq!(master.write(msg.as_bytes()).unwrap(), msg.len());
let bytes_recvd = slave.read(&mut buf).unwrap();
assert_eq!(bytes_recvd, msg.len());
let msg_recvd = str::from_utf8(&buf[..bytes_recvd]).unwrap();
assert_eq!(msg_recvd, msg);
println!("Slave Rx: {}", msg_recvd);
thread::sleep(time::Duration::from_secs(1));
}
}
#[cfg(not(unix))]
fn main() {}