PS4262, how to understand the recordings from StreamDataHandler() with trigger

Post your C and C++ discussions here
Post Reply
XavierPico
Newbie
Posts: 0
Joined: Thu Jan 23, 2020 5:16 pm

PS4262, how to understand the recordings from StreamDataHandler() with trigger

Post by XavierPico »

Dear technical support team,
My question may be related to my personal understanding about the mode "CollectStreamingTriggered(UNIT_MODEL * unit)" with PS4262.
I set trigger voltage = 100mV, preTrigger = 0 or 1000;
I have used the C++ examples in github and made some minor modification on recorded data format.
Here are the codes:
void StreamDataHandler(UNIT_MODEL * unit, uint32_t preTrigger)
{
int32_t i, j;
uint32_t sampleCount = 50000; /* Make sure buffer large enough */
FILE * fp = NULL;
int16_t * buffers[PS4000_MAX_CHANNEL_BUFFERS];
int16_t * appBuffers[PS4000_MAX_CHANNEL_BUFFERS];
PICO_STATUS status;
uint32_t sampleInterval = 1;
int32_t index = 0;
int32_t totalSamples;
uint32_t triggeredAt = 0;
int16_t retry = 0;
BUFFER_INFO bufferInfo;
for (i = 0; i < unit->channelCount; i++) // create data buffers
{
buffers[i * 2] = (int16_t*)calloc(sampleCount, sizeof(int16_t));
buffers[i * 2 + 1] = (int16_t*)calloc(sampleCount, sizeof(int16_t));

status = ps4000SetDataBuffers(unit->handle,
(PS4000_CHANNEL)i,
buffers[i * 2],
buffers[i * 2 + 1],
sampleCount);

// Application buffers to copy data into
appBuffers[i * 2] = (int16_t*)malloc(sampleCount * sizeof(int16_t));
appBuffers[i * 2 + 1] = (int16_t*)malloc(sampleCount * sizeof(int16_t));
}

bufferInfo.unit = unit;
bufferInfo.driverBuffers = buffers;
bufferInfo.appBuffers = appBuffers;
printf("Waiting for trigger...Press a key to abort\n");
g_autoStop = FALSE;
status = ps4000RunStreaming(unit->handle,
&sampleInterval,
PS4000_US,
preTrigger,
1000000 - preTrigger,
TRUE, //FALSE,
100,
sampleCount);

printf("Streaming data...Press a key to abort\n");

fopen_s(&fp, "stream.txt", "w");

if (fp != NULL)
{
fprintf(fp, "For each of the %d Channels, results shown are....\n", unit->channelCount);
fprintf(fp, "Maximum Aggregated value ADC Count & mV, Minimum Aggregated value ADC Count & mV\n\n");

for (i = 0; i < unit->channelCount; i++)
{
if (unit->channelSettings.enabled)
{
fprintf(fp, "Ch MaxADC MaxmV MinADC MinmV ");
}
}
fprintf(fp, "\n");
}

totalSamples = 0;

// while (!_kbhit() && !g_autoStop)
while (!g_autoStop)
{
/* Poll until data is received. Until then, GetStreamingLatestValues wont call the callback */
//Sleep(100);
g_ready = FALSE;

status = ps4000GetStreamingLatestValues(unit->handle, CallBackStreaming, &bufferInfo);

index++;

if (g_ready && g_sampleCount > 0) /* can be ready and have no data, if autoStop has fired */
{
if (g_trig)
{
triggeredAt = totalSamples + g_trigAt;
}

totalSamples += g_sampleCount;
printf("\nCollected %li samples, index = %lu, Total: %d samples ", g_sampleCount, g_startIndex, totalSamples);

if (g_trig)
{
printf("Trig. at index %lu", triggeredAt);
}

if (fp != NULL)
{
for (i = g_startIndex; i < (int32_t)(g_startIndex + g_sampleCount); i++)
{
for (j = 0; j < unit->channelCount; j++)
{
if (unit->channelSettings[j].enabled)
{
fprintf(fp,
"Ch%C %d %+d, %d %+d ",
(char)('A' + j),
appBuffers[j * 2],
adc_to_mv(appBuffers[j * 2], unit->channelSettings[PS4000_CHANNEL_A + j].range),
appBuffers[j * 2 + 1],
adc_to_mv(appBuffers[j * 2 + 1], unit->channelSettings[PS4000_CHANNEL_A + j].range));
}
}
fprintf(fp, "\n");
}
}
else
{
printf("Cannot open the file stream.txt for writing.\n");
}
}
}

if (fp != NULL)
{
fclose(fp);
}

ps4000Stop(unit->handle);

if (!g_autoStop)
{
printf("\ndata collection aborted\n");
_getch();
}

for (i = 0; i < unit->channelCount * 2; i++)
{
free(buffers);
free(appBuffers);
}
}


If I don't use trigger, every thing is OK by checking stream.txt, but in trigger mode, there are something out of my understanding. The program works: if I don't connect the signal generator (sine waveform +/-5V) on chan A, it waits otherwise, it does measurements.
Here are my questions about trigger mode and many thanks for your help:
1. sampleCount is set to 50000 but I have got only about 10000 samples for both chans A and B in stream.txt.
2. Trigger works, but I don't know how to find the moment of trigger on the data file. I have checked with preTrigger = 1000, at the point 1000, I didn't get the good value of measured voltage of chan A

Warm regards
Xavier

Post Reply