#Multithreaded serial port problems

1 messages · Page 1 of 1 (latest)

wheat current
#

Hi, complete Rust newbie here.

The basic intention here is to manage a serial port configuration in a struct. Then when connect() gets called on that struct, the serial port connection object gets created in the struct and then separate threads get created: one to handle serial reads and the other to handle serial writes. I am using the rust serial port crate to manage the serial port. I am trying to use Arc to manage everything with my serial port struct so it can be shared across threads.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=57d7059114099ee036a2d186470bee3e

This is a simplified version of what I'm trying to do. This won't run on the rust playground or on your machine because this creates a real serial port connection to a device I have. I've not sure how to meaningfully stub this part out of my code, but suggestions for increasing snippet share-ability would be appreciated.

The issues I get when compiling this code are:

error[E0277]: the size for values of type dyn SerialPort cannot be known at compilation time
--> src/main.rs:44:49

error[E0277]: the size for values of type dyn SerialPort cannot be known at compilation time
--> src/main.rs:44:34

error[E0614]: type usize cannot be dereferenced
--> src/main.rs:63:21

I have mostly been struggling with those BufReader errors. I thought that the Box<dyn serialport::SerialPort> bit I was doing in the struct initialization was doing something to help Rust cope with the dynamics of the serialport object, but that appears to be insufficient for BufReader in this case. Any ideas what I'd need to do to make BufReader happy when reading through the dyn Serialport? Does anything else in this code stand out as egregious or wrong (especially related to how I'm sharing the Arc)?

Thanks in advance - I've been struggling to work out this Arc sharing pattern in Rust for a long time.

icy meteor
#

at line 44 you have

BufReader::new(*...

which is dereferencing the box, i.e. trying to move the dyn SerialPort out of the Box<dyn SerialPort, which you cannot do

#

you can accomplish what you're trying with BufReader::new(&mut *locked_local.serial_connection.as_mut().unwrap())
— but you probably shouldn't, because repeatedly recreating a BufReader will mean that the data in the buffer from last time is discarded