Hardware: AD-PQMON-SL
Problem Description:
- While waiting for waveform acquisition, some quick power quality events, like interrupts, may be missed.
- If two while loops communicate with AD-PQMON-SL at the same time, will it cause a conflict?

Temporary Solution:
Now we put waveform acquisition and monitor power quality events in the same while loop in a single thread.
while (running) {
// 1. Monitor Event Attributes
iio_channel_attr_read_double(chn_PQ, "countEvent", &chnAttrValue);
// 2. Fetch Waveform Buffer
iio_buffer_refill(buffer);
// 3. Process Waveform
process_waveform(buffer);
}
Troubleshooting steps that I have taken:
- From AI, yes, that would be conflict.
- So AI suggest me create another context; here’s the example that was generated by AI and hasn’t been tested
// Thread 1: Dedicated to monitoring attributes
void* thread_monitor(void *arg) {
// 1. Create a dedicated context instance for Thread 1
// Pass NULL for local sysfs or use a explicit URI like "ip:192.168.2.1" or "local:"
struct iio_context *ctx1 = iio_create_context(NULL, "local:");
if (!ctx1) return NULL;
struct iio_device *dev1 = iio_context_find_device(ctx1, "ad-pqmon-sl");
struct iio_channel *chn_PQ = iio_device_find_channel(dev1, "power_channel", false);
double chnAttrValue;
while (1) {
// Safe to read attributes concurrently without mutexes
iio_channel_attr_read_double(chn_PQ, "countEvent", &chnAttrValue);
usleep(100000);
}
// Clean up dedicated context on exit
iio_context_destroy(ctx1);
return NULL;
}
// Thread 2: Dedicated to continuous buffer streaming
void* thread_waveform(void *arg) {
// 2. Create a separate context instance for Thread 2
struct iio_context *ctx2 = iio_create_context(NULL, "local:");
if (!ctx2) return NULL;
struct iio_device *dev2 = iio_context_find_device(ctx2, "ad-pqmon-sl");
struct iio_channel *chn_wave = iio_device_find_channel(dev2, "voltage0", false);
iio_channel_enable(chn_wave);
struct iio_buffer *buffer = iio_device_create_buffer(dev2, 1024, false);
while (1) {
// Refill buffer independently
iio_buffer_refill(buffer);
// Process waveform data...
}
iio_buffer_destroy(buffer);
iio_context_destroy(ctx2);
return NULL;
}
int main() {
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_monitor, NULL);
pthread_create(&t2, NULL, thread_waveform, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}