Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast receive missing beginning of packet #161

Open
usbalbin opened this issue Feb 23, 2024 · 3 comments
Open

Fast receive missing beginning of packet #161

usbalbin opened this issue Feb 23, 2024 · 3 comments

Comments

@usbalbin
Copy link

Hi

I am building an embedded device which needs to log a lot of data quite fast to a PC. Right now I have hardcoded the device to send the same 52bytes of data every 2ms, this works fine as seen on logic analyzer.

Now on the PC end, I am using this library to try and receive that data. Since the data is COBS i thought I'd use BufReader::split(SENTINEL_BYTE). However for some reason every about 9-10th received packet is missing its first x bytes, where x varies.

fn main() {
    use std::io::{BufReader, BufRead};

    // This is what the device prints(including the 0 byte) which BufReader::split removes as expected
    const EXPECTED_DATA: [u8; 52 - 1] = [
        1, 10, 5, 29, 1, 25, 2, 248, 85, 51, 9, 180, 16, 176, 9, 182, 2, 51, 3, 6, 180,
        36, 248, 172, 1, 128, 128, 192, 247, 13, 180, 36, 224, 93, 52, 10, 152, 17,
        148, 10, 192, 2, 52, 40, 134, 3, 194, 222, 152, 241, 167,// 0 <- End byte
    ];
    let baud = 460800;
    let port = serialport::new("/dev/ttyUSB0", baud).open_native().unwrap();
    let mut reader = BufReader::with_capacity(u16::MAX as _, port);
    let mut successes_in_a_row: u64 = 0;
    println!("Prints successed received bytes in a row on failure:");
    for data in reader.split(0) {
        let Ok(data) = data else {
            continue
        };
        if data == EXPECTED_DATA {
            successes_in_a_row += 1;
        } else {
            println!("{successes_in_a_row}, {}B", data.len());
            successes_in_a_row = 0;
        }
    }
}

output from cargo r --release

Prints successed received bytes in a row on failure:
0, 65B
9, 33B
8, 5B
9, 29B
8, 1B
9, 25B
9, 49B
8, 21B
9, 45B
8, 17B
9, 41B
8, 13B
9, 37B
8, 9B
9, 33B
8, 5B
9, 29B
8, 1B
9, 25B
9, 49B
8, 21B
9, 45B
8, 17B
9, 41B
8, 13B
9, 37B
8, 9B
9, 33B
8, 5B
9, 29B
8, 1B
9, 25B
9, 49B
8, 21B
9, 45B
8, 17B
9, 41B
8, 13B
9, 37B
8, 9B
9, 33B
8, 5B
9, 29B
...

Any ideas as to what could be the cause? Some sort of buffer overrun? I have yet to see any wrong bytes in the stream, it is just that data sometimes is missing its first set of bytes

@torkleyy
Copy link

torkleyy commented Mar 4, 2024

I've been experiencing the same issue, we couldn't figure out what's causing it. However, some tools we use don't lose any of the UART data...

@DanielJoyce
Copy link

Instead of swallowing the error, can you print it out? It might be something obvious.

  let Ok(data) = data else {
            continue
        };

This continues the next iter of the loop, and doesn't print anything out nor reset the success counter.

@henm
Copy link

henm commented May 15, 2024

I'm experiencing a similar issue. I've noticed that in my case version 4.0.1 works, so I'm using that one for the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants