hook with slider
This commit is contained in:
@@ -3,7 +3,7 @@ extern crate wwserial;
|
||||
use wwserial::WwSerial;
|
||||
|
||||
fn main() {
|
||||
let s = WwSerial::new("COM3".to_string(), 115200, 1000, true);
|
||||
let s = WwSerial::new("COM3".to_string(), 115200, 20, 20, true);
|
||||
|
||||
let x: Vec<u8> = vec![0xff, 0x10, 0x00, 0xf1];
|
||||
println!("Sending {:?}", x);
|
||||
@@ -11,7 +11,7 @@ fn main() {
|
||||
println!("Sent {}/4", bytes);
|
||||
|
||||
let mut r: Vec<u8> = Vec::with_capacity(100);
|
||||
s.read(&mut r, 100);
|
||||
s.read(&mut r);
|
||||
println!("Received {:?}", r);
|
||||
|
||||
let x: Vec<u8> = vec![0xff, 0xf0, 0x00, 0x11];
|
||||
@@ -20,7 +20,7 @@ fn main() {
|
||||
println!("Sent {}/4", bytes);
|
||||
|
||||
let mut r: Vec<u8> = Vec::with_capacity(100);
|
||||
s.read(&mut r, 100);
|
||||
s.read(&mut r);
|
||||
println!("Received {:?}", r);
|
||||
|
||||
let x: Vec<u8> = vec![0xff, 0x03, 0x00, 0xfe];
|
||||
@@ -28,12 +28,28 @@ fn main() {
|
||||
let bytes = s.write(&x);
|
||||
println!("Sent {}/4", bytes);
|
||||
|
||||
let mut r: Vec<u8> = Vec::with_capacity(100);
|
||||
s.read(&mut r);
|
||||
println!("Received {:?}", r);
|
||||
|
||||
let x: Vec<u8> = vec![0xff; 130];
|
||||
println!("Sending {:?}", x);
|
||||
let bytes = s.write(&x);
|
||||
println!("Sent {}/130", bytes);
|
||||
|
||||
println!("Infinite looping, ctrl-c to quit");
|
||||
loop {
|
||||
let mut r: Vec<u8> = Vec::with_capacity(500);
|
||||
let bytes = s.read(&mut r, 500);
|
||||
let bytes = s.read(&mut r);
|
||||
if bytes > 0 {
|
||||
println!("(ctrl-c to quit) Received ({}) {:?}", bytes, r);
|
||||
|
||||
{
|
||||
let x: Vec<u8> = vec![0xff; 130];
|
||||
println!("Sending {:?}", x);
|
||||
let bytes = s.write(&x);
|
||||
println!("Sent {}/130", bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+13
-6
@@ -8,13 +8,14 @@ mod ffi {
|
||||
fn new_cxx_serial(
|
||||
port: String,
|
||||
baud: u32,
|
||||
timeout: u32,
|
||||
read_timeout: u32,
|
||||
write_timeout: u32,
|
||||
hardware: bool,
|
||||
) -> UniquePtr<CxxSerial>;
|
||||
|
||||
fn write(self: &CxxSerial, data: &Vec<u8>) -> u32;
|
||||
|
||||
fn read(self: &CxxSerial, data: &mut Vec<u8>, cap: u32) -> u32;
|
||||
fn read(self: &CxxSerial, data: &mut Vec<u8>) -> u32;
|
||||
|
||||
fn flush(self: &CxxSerial);
|
||||
|
||||
@@ -31,9 +32,15 @@ pub struct WwSerial {
|
||||
unsafe impl Send for WwSerial {}
|
||||
|
||||
impl WwSerial {
|
||||
pub fn new(port: String, baud: u32, timeout: u32, hardware: bool) -> Self {
|
||||
pub fn new(
|
||||
port: String,
|
||||
baud: u32,
|
||||
read_timeout: u32,
|
||||
write_timeout: u32,
|
||||
hardware: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
inner: new_cxx_serial(port, baud, timeout, hardware),
|
||||
inner: new_cxx_serial(port, baud, read_timeout, write_timeout, hardware),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +50,8 @@ impl WwSerial {
|
||||
}
|
||||
|
||||
// #[inline(always)]
|
||||
pub fn read(&self, data: &mut Vec<u8>, cap: u32) -> u32 {
|
||||
self.inner.read(data, cap)
|
||||
pub fn read(&self, data: &mut Vec<u8>) -> u32 {
|
||||
self.inner.read(data)
|
||||
}
|
||||
|
||||
// #[inline(always)]
|
||||
|
||||
@@ -351,8 +351,13 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
||||
}
|
||||
DWORD bytes_written;
|
||||
if (!WriteFile(fd_, data, static_cast<DWORD>(length), &bytes_written, NULL)) {
|
||||
DWORD errcode = GetLastError();
|
||||
if (errcode == 121) {
|
||||
// Timeout
|
||||
return 0;
|
||||
}
|
||||
stringstream ss;
|
||||
ss << "Error while writing to the serial port: " << GetLastError();
|
||||
ss << "Error while writing to the serial port: " << errcode;
|
||||
THROW (IOException, ss.str().c_str());
|
||||
}
|
||||
return (size_t) (bytes_written);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "wwserial/src/lib.rs.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
|
||||
struct CxxSerial::impl
|
||||
{
|
||||
@@ -16,7 +17,7 @@ struct CxxSerial::impl
|
||||
CxxSerial::impl::impl()
|
||||
: serial_port(nullptr){};
|
||||
|
||||
CxxSerial::CxxSerial(rust::String port, uint32_t baud, uint32_t timeout, bool hardware)
|
||||
CxxSerial::CxxSerial(rust::String port, uint32_t baud, uint32_t read_timeout, uint32_t write_timeout, bool hardware)
|
||||
: impl(new struct CxxSerial::impl)
|
||||
{
|
||||
impl->ok = true;
|
||||
@@ -24,7 +25,7 @@ CxxSerial::CxxSerial(rust::String port, uint32_t baud, uint32_t timeout, bool ha
|
||||
try
|
||||
{
|
||||
impl->serial_port = std::shared_ptr<serial::Serial>(
|
||||
new class serial::Serial(port_stdstring, baud, serial::Timeout::simpleTimeout(timeout)));
|
||||
new class serial::Serial(port_stdstring, baud, serial::Timeout(1, read_timeout, 0, write_timeout, 0)));
|
||||
if (hardware)
|
||||
{
|
||||
impl->serial_port->setFlowcontrol(serial::flowcontrol_hardware);
|
||||
@@ -41,18 +42,19 @@ uint32_t CxxSerial::write(const rust::Vec<uint8_t> &data) const
|
||||
if (impl->ok && impl->serial_port->isOpen())
|
||||
{
|
||||
std::vector<uint8_t> buf(data.begin(), data.end());
|
||||
return impl->serial_port->write(buf);
|
||||
size_t bytes_written = impl->serial_port->write(buf);
|
||||
return bytes_written;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
uint32_t CxxSerial::read(rust::Vec<uint8_t> &data, uint32_t cap) const
|
||||
uint32_t CxxSerial::read(rust::Vec<uint8_t> &data) const
|
||||
{
|
||||
if (impl->ok && impl->serial_port->isOpen())
|
||||
{
|
||||
std::vector<uint8_t> buf;
|
||||
buf.reserve(cap);
|
||||
size_t bytes_read = impl->serial_port->read(buf, (size_t)cap);
|
||||
buf.reserve(data.capacity());
|
||||
size_t bytes_read = impl->serial_port->read(buf, (size_t)buf.capacity());
|
||||
std::copy(
|
||||
buf.begin(), buf.end(),
|
||||
std::back_inserter(data));
|
||||
@@ -74,7 +76,7 @@ bool CxxSerial::check() const
|
||||
return impl->ok;
|
||||
}
|
||||
|
||||
std::unique_ptr<CxxSerial> new_cxx_serial(rust::String port, uint32_t baud, uint32_t timeout, bool hardware)
|
||||
std::unique_ptr<CxxSerial> new_cxx_serial(rust::String port, uint32_t baud, uint32_t read_timeout, uint32_t write_timeout, bool hardware)
|
||||
{
|
||||
return std::make_unique<CxxSerial>(port, baud, timeout, hardware);
|
||||
return std::make_unique<CxxSerial>(port, baud, read_timeout, write_timeout, hardware);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user