PicoScope 7 Software
Available on Windows, Mac and Linux
Code: Select all
#include
#include
#include
#define FILEPSD2 "test.psd" // name of file
// used to check that the .psd file has not been saved commpressed
#define COMPRESSED_FLAG 0x80000000UL
// PSD EOF marker
#define PS_EOF 10
// PSD header
typedef struct
{
short owner;
short section_type;
short section_no;
short no_of_parts;
} PSDHEADER;
void main(void)
{
FILE *fp;
PSDHEADER header;
BOOL ok;
HANDLE hFile;
DWORD nb;
DWORD length;
short *channel1_adc, *channel2_adc;
long *times;
int part = 0;
long no_of_samples, i;
char single[200];
int lth;
short number_of_channels;
no_of_samples = 0;
hFile = CreateFile(FILEPSD2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
if (hFile == INVALID_HANDLE_VALUE)
exit (0);
number_of_channels = 1;
ok = ReadFile(hFile, &header, sizeof(PSDHEADER), &nb, NULL);
ReadFile(hFile, &length, sizeof(DWORD), &nb, NULL);
SetFilePointer (hFile, length, NULL, FILE_CURRENT);
//enter a loop and keep going
//until either PS_EOF is reached or some DATA is found.
while (header.section_type != PS_EOF)
{
// read the header
ok = ReadFile(hFile, &header, sizeof(PSDHEADER), &nb, NULL);
// if the header is of type 1 or 2 get the data and times
if(header.section_type == 1 || header.section_type == 2)
{
// Always Part1 read settings, and discard
ReadFile(hFile, &length, sizeof(DWORD), &nb, NULL);
ReadFile(hFile, (BYTE *) &single, length, &nb, NULL);
// Number of bytes making up part 2
ReadFile(hFile, &length, sizeof(DWORD), &nb, NULL);
// check the file is not compressed.
if(length & COMPRESSED_FLAG)
{
printf("The data is compressed");
exit (0);
}
else
{
channel1_adc = (short *) malloc (length);
ok = ReadFile(hFile, channel1_adc, length, &nb, NULL);
}
// if you are using 2 channels on the ADC-2xx and ADC-100 units
if(header.section_type == 2)
{
// number of channels used set to 2
number_of_channels = 2;
// read the number of bytes in part 3
ReadFile(hFile, &length, sizeof(DWORD), &nb, NULL);
// check that the file is not compressed
if(length & COMPRESSED_FLAG)
{
printf("The data is compressed");
exit (0);
}
else
{
channel2_adc = (short *) malloc (length);
ok = ReadFile(hFile, channel2_adc, length, &nb, NULL);
}
}
// if only one channels is used read the number of bytes in part 3
// if 2 channels have been used then this will be part 4
ReadFile(hFile, &length, sizeof(DWORD), &nb, NULL);
// check that the file is not compressed
if(length & COMPRESSED_FLAG)
{
printf("The data is compressed");
exit (0);
}
else
{
times = (long *) malloc (length);
ok = ReadFile(hFile, times, length, &nb, NULL);
no_of_samples = length / 4;
}
}
else
{
// move the file pointer on the number of bytes, this is not needed.
part = 0;
if (times)
{
while (part < header.no_of_parts)
{
ReadFile(hFile, &length, sizeof(DWORD), &nb, NULL);
ok = SetFilePointer (hFile, length, NULL, FILE_CURRENT);
part++;
}
}
}
}
// write data in to the .txt file.
if(no_of_samples > 0)
{
fp = fopen (FILETXT, "w");
if (fp)
{
fprintf(fp, "Channel A\nTimes, ADC\n");
for (i = 0; i < no_of_samples; i++)
{
fprintf(fp, "%ld, %d\n", times[i], channel1_adc[i]);
}
if(number_of_channels == 2)
{
fprintf(fp, "\nChannel B\nTimes, ADC\n");
for (i = 0; i < no_of_samples; i++)
{
fprintf(fp, "%ld, %d\n", times[i], channel2_adc[i]);
}
}
fclose(fp);
}
}
free (times);
free (channel1_adc);
if (number_of_channels == 2)
{
free (channel2_adc);
}
CloseHandle(hFile);
}