Need to know arduino like command in rust

println in arduino - prints data out in serial monitor
but in rust, the data conversion errors are happening.
since this is more embedded problem, here no std libs are allowed.
hence provide an example for rp-hal - serial monitor for all data types.

examples are available as only for bytes
serial.write(b"Helloworld\r\n");

when i try to pass on u8 or i8 data type - getting error
expected &[u8], found i8

If you want to write an i8 (does it come from a C signed char, by the way?), then you can just cast it to a u8 to preserve the bit pattern, and then create a single-element array from it: serial.write(&[the_i8 as u8])

mismatched types

expected &[u8], found u8rustc(E0308)

pico_usb_serial.rs(175, 30): expected &[u8], found u8

casting &i8 as u8 is invalid

cannot cast &i8 as u8

It looks like your the_i8 is actually an &i8 reference; does serial.write(&[*the_i8 as u8]) work?

type i8 cannot be dereferenced

How are you creating the_i8? Do you have a full function with the issue that you could post?

1 Like

you could use the Heapless (https://crates.io/crates/heapless) crate with the write! macro to write the value to a "String" and later print that "String"

use core::fmt::Write;

let mut out: String<10> = String::new();
let my_var = Sensor::read();
write!(&mut out, "{}", my_var).unwrap();
serial.write(&out)

the context of error is eliminated - but the issue is now pico serial is down when ever serial is written.

will check a good lib for pico - for serial with multi data type

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.