Struct visa_device_handler::visa_module::SafeDeviceMap
source · pub struct SafeDeviceMap { /* private fields */ }Expand description
The SafeDeviceMap provides a lock safe way to store The resource manager and all the sessions in one place.
SafeDeviceMap uses Arc and Mutex wrapped around rm and map to provide a safe way to interact with them.
§Params
@lib -> The main visa library, its just to create all kind of calls.
@rm -> u32 session number, will hold the sessions and open new ones.
@map -> HashMap<String,Device> instance, stores all the instruments by thier device name (Human readable string).
#Panic
As the program panics, the rm will be droped so the connections should be freed as well.
But just to be safe allways call SafeDeviceMap::disconnect_device or SafeDeviceMap::clear_map when finished.
I will try too look more into it and update it in the future.
§Examples
let sdm_result:SafeDeviceMap = SafeDeviceMap::init(None);
match sdm_result {
Ok(mapper) => {
mapper.connect_device("address_01".to_string());
let data = mapper.query_from_device("name_01".to_string(),"cool funcation with args").unwrap();
println!("got {} from the device",data);
mapper.disconnect_device("name_01".to_string());
}
Err(e) => {/*print codes or anything */}
}To get a SafeDeviceMap call SafeDeviceMap::init().
Implementations§
source§impl SafeDeviceMap
impl SafeDeviceMap
sourcepub fn init(file_path: Option<&str>) -> Result<SafeDeviceMap, String>
pub fn init(file_path: Option<&str>) -> Result<SafeDeviceMap, String>
Call this to get the SafeDeviceMap.
It will generate a DefaultRM ,create a new HasMap and save the library instance.
This Method MUST be called.
sourcepub fn disconnect_device(&self, name: String) -> Result<Device, String>
pub fn disconnect_device(&self, name: String) -> Result<Device, String>
Call this to remove a device from the map. Note, the removed device data will be returned.
@name -> Device name to get removed.
Return -> The removed Device on success or error string on fail.
sourcepub fn write_to_device(&self, name: String, msg: &str) -> Result<(), String>
pub fn write_to_device(&self, name: String, msg: &str) -> Result<(), String>
Call this to write message to the device.
@name -> Device name send the message to.
@msg -> The message to send, a concated str of function name and its args.
Returns -> Void on success string if error.
sourcepub fn read_from_device(&self, name: String) -> Result<String, String>
pub fn read_from_device(&self, name: String) -> Result<String, String>
Call this to read message from the device buffer.
Note, the buffer will be cleared after a read!
@name -> Device name you wish to read its buffer.
Returns -> The read string if success error string if failed.
sourcepub fn query_from_device(
&self,
name: String,
msg: &str,
) -> Result<String, String>
pub fn query_from_device( &self, name: String, msg: &str, ) -> Result<String, String>
Call this to write and read message from the device buffer.
Note! the buffer will be cleared after a read!
This calls behaves like write and then read.
@name -> Device name to interact with.
@msg -> Message to send to the device (the write message).
Returns -> The read string if success error string if failed.
sourcepub fn get_first_device(
&self,
filter: Option<&str>,
debug: bool,
) -> Result<Device, String>
pub fn get_first_device( &self, filter: Option<&str>, debug: bool, ) -> Result<Device, String>
This function will find the first device the rm can finds.
@filter -> &str, choose the filter keyword for NI-VISA to look for the devices,None defaults to “USB?*”.
@debug -> bool, if you wish to print the device info.
Returns -> The first Device.
sourcepub fn find_all_devices(
&self,
filter: Option<&str>,
debug: bool,
) -> Result<Vec<Device>, String>
pub fn find_all_devices( &self, filter: Option<&str>, debug: bool, ) -> Result<Vec<Device>, String>
sourcepub fn clear_map(&self) -> Result<(), String>
pub fn clear_map(&self) -> Result<(), String>
This function clears the mapping of the devices.
It will close all the connections and drop all the Devices.
sourcepub fn get_all_mapped_devices(&self) -> Result<Vec<Device>, String>
pub fn get_all_mapped_devices(&self) -> Result<Vec<Device>, String>
This function returns to the user all the active Device that the SafeDeviceMap have access to.