serial: fix UTF-8 panic on macOS

This commit is contained in:
Will Greenberg
2025-01-21 15:00:13 -08:00
committed by Cooper Quintin
parent 6bd36921d8
commit 565c0f1e67
+4 -1
View File
@@ -78,7 +78,10 @@ fn send_command<T: UsbContext>(handle: &mut DeviceHandle<T>, command: &str) {
.read_bulk(0x82, &mut response, timeout)
.expect("Failed to read response");
let responsestr = str::from_utf8(&response).expect("Failed to parse response");
// For some reason, on macOS the response buffer gets filled with garbage data that's
// rarely valid UTF-8. Luckily we only care about the first couple bytes, so just drop
// the garbage with `from_utf8_lossy` and look for our expected success string.
let responsestr = String::from_utf8_lossy(&response);
if !responsestr.contains("\r\nOK\r\n") {
println!("Received unexpected response{0}", responsestr);
std::process::exit(1);