I'm very new to Rust, I started learning it just a few days ago, and I really enjoy it so far. I'm planning to write a program in it that will control an experiment. The experiment will involve one main computer (Raspberry Pi running the Rust program) and two Arduinos. One Arduino will be equipped with a couple of sensors and a mechanized experiment unit and the other one will serve as data transmission unit. I want to fetch sensor data and save the data to a log every couple of seconds, and every couple of minutes I want to send latest available data to the transmission unit. I tried to use
std::sync::mpsc, somewhat similar to example here, but with everything in
loops and with
thread::sleeps, but when I tried to get data in one thread from another thread with
rx.recv(), I wasn't getting the latest data, but only the "next" data (let's say that second thread got "1" last time, the first thread sent "2", "3", "4" and "5" in the meantime, but the second thread would get "2" instead of "5"). Also, communication between these two threads quickly got convoluted, so I decided to write and read latest data to a file, while implementing file locking thanks to fs2 crate. It works, but I don't think that it's the best solution for this problem. I also didn't like using
thread::sleeps, is there a better option to schedule a task in Rust to occur periodically? I'm sorry if this question is too broad, but I just don't know what is the best way in this scenario.