diff --git a/src/lib.rs b/src/lib.rs
index 5f1c86a859d4880f7fc2a397b76c11d003bcf2b1..1d201eb2ace403c4f66e19d61dd1179061370741 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -149,9 +149,11 @@ fn read_from_serial(mut outputs: Output<'_>, args: Args<'_>) -> SResult<()> {
     let n_doubles = arg as usize;
 
     // Read n_doubles from the serial port
-    let bytes = SERIAL.read().unwrap().read_n_bytes(n_doubles)?;
-    // Here we use unsafe to cast the bytes to a slice of f64 (maybe a safer alternative helps)
-    let doubles = unsafe { std::slice::from_raw_parts(bytes.as_ptr() as *const f64, n_doubles) };
+    let bytes = SERIAL.read().unwrap().read_n_bytes(n_doubles * 8)?;
+    let doubles = bytes
+        .chunks_exact(8)
+        .map(|chunk| f64::from_be_bytes(chunk.try_into().unwrap()))
+        .collect::<Vec<f64>>();
     warn_debug!("Read {} bytes from serial port", n_doubles);
 
     outputs.set(doubles.to_vec())?;