The purpose of the read callback is to read data from the instrument. Typically, if you are inside the read callback, you've already determined that you want to read from the instrument.
The read callback gets called when the attribute's cache is marked as invalid. So one solution is for you to modify your application to call Ivi_InvalidAttribute on the attribute when you are in the state where you want to read from the instrument (instead of implementing this logic *in* the read callback).
If this doesn't work, here's something else you can try:
- Add an attribute to your driver that is marked as IVI_VAL_ALWAYS_CACHE and doesn;t have any read/write callbacks. Essentially, its a session-based global variable. e.g. if your attribute is called IVI_ATTR_
VOLTAGE, call this attribute IVI_ATTR_VOLTAGE_CACHE.
- In the attirbute's write callback, make sure you set IVI_ATTR_VOLTAGE_CACHE to the same value as that of IVI_ATTR_VOLTAGE.
- In the attribute's read callback, return the value of IVI_ATTR_VOLTAGE_CACHE when you want to return the cache value.
Basically, you're using IVI_ATTR_VOLTAGE_CACHE as a session based variable to store the cache value and return it when needed. Keep in mind that this requires you to make sure the values of both attirbutes are always synchronized.