DrDaq Streaming from three Channels problem

Post your C and C++ discussions here
Post Reply
giovanni
Newbie
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

DrDaq Streaming from three Channels problem

Post by giovanni » Tue Oct 07, 2014 8:51 am

Dear,
i am trying to develop a deamon c that reads three channels of the drdaq (light, temperature and sound level) and send them to a java process via socket.
The socket session works but the session relates to the reading the values looks fail.
I have used the code in the SDk to begint he deleloping.
I post the code of the init fucntion and the streaming function.

Code: Select all

//-----------------------------------------------------------------------------------------------------------------------------------

USB_DRDAQ_INPUTS channel[3] = {USB_DRDAQ_CHANNEL_LIGHT, USB_DRDAQ_CHANNEL_TEMP,USB_DRDAQ_CHANNEL_MIC_WAVE};
//------------------------------------------------------------------------------------------------------------------------------------

//init section

	char	info[20];
	short	requiredSize = 0;

	short channel=0;

	printf ("USB DrDAQ driver example program\n");
	printf ("Version 1.0\n\n");

	printf ("\n\nOpening the device...\n");
	status = UsbDrDaqOpenUnit(&g_handle);
	if (status != 0)
	{
		printf ("Unable to open device\nPress any key\n");
		_getch();
		return;
	}
	else
	{
		printf ("Device opened successfully\n\n");
		status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_VARIANT_INFO);
		printf("Model:\t\t\t %s\n", info);
		status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_BATCH_AND_SERIAL);
		printf("Serial Number:\t\t %s\n", info);
		status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_CAL_DATE);
		printf("Calibration Date:\t %s\n\n", info);
		status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_USB_VERSION);
		printf("%s\n", info);
		status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_HARDWARE_VERSION);
		printf("Hardware version %s\n", info);
		status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_DRIVER_VERSION);
		printf("USBDrDAQ.dll version %s\n", info);
		status = UsbDrDaqGetUnitInfo(g_handle, info, 20, &requiredSize, PICO_KERNEL_VERSION);
		printf("%s\n", info);
		

		//Set to scope channel initially
		channel = USB_DRDAQ_CHANNEL_SCOPE;
	}

//-----------------------------------------------------
//init socket

//......

/---------------------------------------------------------------------------------------------------------------------------------------
//Streming function

unsigned int i;
	int aInt=0;
	int result=-1;

	unsigned long nSamples = 2000;		
	short nChannels = 3;
	unsigned long nSamplesPerChannel = 1000;
	unsigned long nSamplesCollected;
	short samples[3000];
	unsigned long usForBlock = 1000000;
	unsigned short overflow;
	unsigned long triggerIndex = 0;
	short nLines = 0;
	FILE *fp;

	char v1[BUFSIZ];
	char v2[BUFSIZ];
	char v3[BUFSIZ];
	char out[BUFMAX];

	printf ("Collect streaming (channel %d)...\n", channel);
	printf ("Data is written to disk file (test.out)\n");
	printf ("Press a key to start\n");
//	_getch();

	//Set the trigger (disabled)
	status = UsbDrDaqSetTrigger(g_handle, FALSE, 0, 0, 0, 0, 0, 0, 0);

	//set sampling rate and channels
	status = UsbDrDaqSetInterval(g_handle, &usForBlock, nSamples, channel, nChannels);

	//Start streaming
	status = UsbDrDaqRun(g_handle, nSamples, BM_STREAM);

	//Wait until unit is ready
	isReady = 0;
	while(isReady == 0)
	{
		status = UsbDrDaqReady(g_handle, &isReady);
	}

	//printf("Press any key to stop\n");
	//fopen_s(&fp, "test.out", "w");

	while((result!=FALSE))
	{
//		nSamplesCollected = nSamplesPerChannel;
		status = UsbDrDaqGetValues(g_handle, samples, &nSamplesCollected, &overflow, &triggerIndex);

//		printf("%d values\n", nSamplesCollected);
		
		i = 0;
		while(	(i<nSamplesCollected-2) && (result!=FALSE) )
		//for (i = 0; i < nSamplesCollected-2; i++) 
		{
			//qui dovrei invia i dati via socket....

				aInt = samples[i];  //is it correct?
				//aInt=1;
				sprintf(v1, "%d", aInt);
				strcat(v1,";");

				aInt = samples[i+1];  //is it correct?
				
				sprintf(v2, "%d", aInt);
				strcat(v2,";");

				aInt = samples[i+2];   //is it correct?
				sprintf(v3, "%d", aInt);
				strcat(v3,"\n");

				strcpy(out,v1);
				strcat(out,v2);
				strcat(out,v3);  //hereby I link the three values to send them via socket

		//		printf("We are going to send");
		//		printf(out);

				
				iResult = send( ConnectSocket,	out	, (int)strlen(out), 0 );
				if (iResult == SOCKET_ERROR) {
					wprintf(L"send failed with error: %d\n", WSAGetLastError());
					closesocket(ConnectSocket);
					WSACleanup();
				}//end send


			//	fprintf (fp, "%d, %d, %d\n", adc_to_mv(samples[i]), adc_to_mv(samples[i+1]), adc_to_mv(samples[i+2]));
				
				//ricevo la risposta del server nel caso si debba chiiedere il flusso
				
				iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
				if (iResult > 0){
					//printf("Bytes received: %d\n", iResult);
					//printf(recvbuf);
					result = strncmp(recvbuf, end_comm, DEFAULT_BUFLEN);
				}
				else if (iResult == 0)
					printf("Connection closed\n");
				else
				printf("recv failed: %d\n", WSAGetLastError());
				
		i++;
		}//end invio blocco dati

		Sleep(200)
	}//
	
	closesocket(ConnectSocket);
    WSACleanup();
	//fclose(fp);
//-----------------------------------------------------------------------------------------------------------
the output of the server side look like that:

Code: Select all

.......
client>2;227;271
client>227;271;3
client>271;3;229
client>3;229;271
client>229;271;3
client>271;3;231
client>3;231;271
client>231;271;2
client>271;2;234
client>2;234;271
client>234;271;0
client>271;0;238
client>0;238;271
client>238;271;2
client>271;2;242
client>2;242;271
client>242;271;0
client>271;0;246
client>0;246;271
client>499;633;-20
client>633;-20;501
client>-20;501;628
client>501;628;-20
client>628;-20;501
client>-20;501;623
client>501;623;-20
client>623;-20;500
client>-20;500;615
client>500;615;-21
client>615;-21;501
client>-21;501;608
client>501;608;-20
client>608;-20;501
client>-20;501;600
client>501;600;-20
client>600;-20;500
client>-20;500;594
client>500;594;-20
client>594;-20;500
client>-20;500;589
client>500;589;-20
client>589;-20;501
client>-20;501;586
client>501;586;-20
client>586;-20;501
client>-20;501;584
client>501;584;-20
client>584;-20;502
client>-20;502;584
client>502;584;-20
client>584;-20;501
client>-20;501;586
client>501;586;-20
client>586;-20;503
client>-20;503;590
client>503;590;-20
client>590;-20;503
------
Please help me!!!

Hitesh
Site Admin
Site Admin
Posts: 2827
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: DrDaq Streaming from three Channels problem

Post by Hitesh » Tue Oct 14, 2014 8:41 am

Hello Giovanni,

When the data values are returned in the array, they will be in the order

Ch 1, Ch 2, Ch 3, Ch 1, Ch 2, Ch 3, ... etc.

I think in your loop you should be incremeting the value of i by 3

e.g.

Code: Select all

while(   (i<nSamplesCollected-2) && (result!=FALSE) )
{
  // Retrieve values from array
  aInt = samples[i]; 

  aInt = samples[i+1];

  aInt = samples[i+2]; 

  // Send data...

  // Increment i by 3
  i = i + 3;
}

That should hopefully extract the data correctly :)

Regards,
Hitesh

Technical Support Engineer

giovanni
Newbie
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

Re: DrDaq Streaming from three Channels problem

Post by giovanni » Fri Oct 17, 2014 2:06 pm

Dear,

thank you for your response, that was a really stupid problem but with you r help i fixed it.
Now i am facing with something more worse.

The code i same but there are some periods in which the values of parameter as totally wrong.

I post the results:

client>(lignt, temp, sound)
client>156;296;5
client>161;296;5
client>167;296;3
client>170;296;3
client>173;296;3
client>175;296;0
client>175;296;0
client>173;296;3
client>170;296;4
client>164;296;5
client>157;296;7
client>149;296;5
client>142;296;4
client>138;296;1
client>136;296;1
client>135;296;0
client>137;296;3
client>140;296;1
client>144;296;0
client>150;296;1 <- What is happen after this point?!?!?!
client>500;805;-67
client>501;827;-66
client>500;848;-66
client>500;861;-67
client>501;32767;-66
client>500;32767;-66
client>500;862;-67
client>499;852;-67
client>500;835;-66
client>501;820;-66
client>501;803;-66
client>501;788;-66
client>501;774;-66
client>501;764;-67
client>500;756;-66
client>500;752;-66
client>501;752;-66
client>501;758;-66
client>502;766;-66
client>499;781;-66
client>501;802;-66
client>501;825;-66
client>501;845;-66
client>500;860;-66
client>499;32767;-67
client>500;32767;-66
client>500;862;-66
client>500;852;-66
client>501;837;-66
client>501;820;-67
client>501;805;-66
client>501;789;-67
client>501;776;-66
client>501;765;-67
client>500;758;-66
client>500;754;-67
client>500;755;-66


Please help me..again!!

Hitesh
Site Admin
Site Admin
Posts: 2827
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: DrDaq Streaming from three Channels problem

Post by Hitesh » Tue Oct 21, 2014 4:46 pm

Hi,

Are you getting any overflow indications?

Is this consistently happening and does the same thing occur if you use PicoScope 6 or PicoLog to capture data using the same settings?

Regards,
Hitesh

Technical Support Engineer

giovanni
Newbie
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

Re: DrDaq Streaming from three Channels problem

Post by giovanni » Mon Oct 27, 2014 4:08 pm

hi,

no i did not have any indication about overflow.

When i use picoscope 6 every thing works fine but i do not to know how to look if the settings are the same....

Hitesh
Site Admin
Site Admin
Posts: 2827
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: DrDaq Streaming from three Channels problem

Post by Hitesh » Mon Oct 27, 2014 5:59 pm

Hi giovanni,

In PicoScope 6 click Views -> View Properties in order to see the sampling rate and number of samples being collected.

You can then adjust your settings (samples and collection time) accordingly.

Regards,
Hitesh

Technical Support Engineer

giovanni
Newbie
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

Re: DrDaq Streaming from three Channels problem

Post by giovanni » Fri Oct 31, 2014 5:41 pm

The parameters are

sampling period: 19 uS

sampling rate: 52,63 KS/Sec

Number of samples: 5236

How can i set my C program with such as parameters?

Regards!!

Hitesh
Site Admin
Site Admin
Posts: 2827
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: DrDaq Streaming from three Channels problem

Post by Hitesh » Mon Nov 03, 2014 3:05 pm

Hi Giovanni,

Could you please send me a PicoScope 6 data file showing the readings so I can check the settings.

Regards,
Hitesh

Technical Support Engineer

giovanni
Newbie
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

Re: DrDaq Streaming from three Channels problem

Post by giovanni » Tue Nov 04, 2014 8:40 am

really sorry but how can i get a PicoScope 6 data file? i think i have to connect the device and then start the reading.
and then?

Hitesh
Site Admin
Site Admin
Posts: 2827
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: DrDaq Streaming from three Channels problem

Post by Hitesh » Tue Nov 04, 2014 9:34 am

Hi Giovanni,

Once you have connected the device and captured some data, click File -> Save As..., then enter a name and click Save.

This will save a data file in the form <file_name>.psdata

Regards,
Hitesh

Technical Support Engineer

giovanni
Newbie
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

Re: DrDaq Streaming from three Channels problem

Post by giovanni » Tue Nov 04, 2014 2:37 pm

Dears,

hereby the psdata file.

Thank you!!!
Attachments
20141104-0001_data.psdata
(150.15 KiB) Downloaded 218 times

giovanni
Newbie
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

Re: DrDaq Streaming from three Channels problem

Post by giovanni » Tue Nov 04, 2014 2:39 pm

this is another one.
thank you!
Attachments
20141104-0001_data_2.psdata
(165.08 KiB) Downloaded 188 times

Hitesh
Site Admin
Site Admin
Posts: 2827
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: DrDaq Streaming from three Channels problem

Post by Hitesh » Fri Nov 07, 2014 2:14 pm

Hi Giovanni,

Thank you for the files.

In the call to UsbDrDaqSetInterval try setting the following:

Code: Select all

usForBlock = 100000; //100ms
nSamples = 5236;
along with your selected channels.

When you call UsbDrDaqRun, ensure that nSamples is used as above.

I hope this helps.

Regards,
Hitesh

Technical Support Engineer

giovanni
Newbie
Posts: 0
Joined: Tue Oct 07, 2014 8:34 am

Re: DrDaq Streaming from three Channels problem

Post by giovanni » Tue Nov 25, 2014 4:48 pm

it does not work... can you post me the code for the setting of the device?

Hitesh
Site Admin
Site Admin
Posts: 2827
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: DrDaq Streaming from three Channels problem

Post by Hitesh » Fri Nov 28, 2014 12:36 pm

Hi Giovanni,

Please find below the modified code:

Code: Select all

void collect_block_immediate (void)
{
	uint32_t	i;
	uint32_t	nSamples = 5236;
	int16_t	nChannels = 3;
	uint32_t	nSamplesPerChannel = 5236;
	uint32_t	nSamplesCollected;
	int16_t	samples[18000] = {0}; 
	uint32_t	usForBlock = 100000;
	uint16_t	overflow;
	uint32_t	triggerIndex = 0;
	FILE *	fp;
	USB_DRDAQ_INPUTS testChannel[3] = {USB_DRDAQ_CHANNEL_LIGHT, USB_DRDAQ_CHANNEL_TEMP, USB_DRDAQ_CHANNEL_MIC_WAVE};

	printf ("Collect block immediate (channel %d)...\n", channel);
	printf ("Press a key to start\n");
	_getch();

	//Set the trigger (disabled)
	status = UsbDrDaqSetTrigger(g_handle, FALSE, 0, 0, 0, 0, 0, 0, 0);

	//set sampling rate and channels
	status = UsbDrDaqSetInterval(g_handle, &usForBlock, nSamples, testChannel, nChannels);

	printf("Press any key to stop\n");
	fopen_s(&fp, "usb_dr_daq_block_immediate.txt", "w");
	
	while(!_kbhit())
	{
		//Run
		status = UsbDrDaqRun(g_handle, nSamples, BM_SINGLE);

		//Wait until unit is ready
		isReady = 0;
		
		while(isReady == 0)
		{
			status = UsbDrDaqReady(g_handle, &isReady);
		}

		nSamplesCollected = nSamplesPerChannel;
		status = UsbDrDaqGetValues(g_handle, samples, &nSamplesCollected, &overflow, &triggerIndex);

		//Print out the first 10 readings, converting the readings to mV if required
		printf ("First 10 readings of %i (press any key to stop)\n\n", nSamplesCollected);
		
		for (i = 0; i < 10; i++)
		{
			printf ("%d\n", adc_to_mv(samples[i]));
		}
		
		for (i = 0; i < nSamplesCollected; i++)
		{
			fprintf (fp, "%d\n", adc_to_mv(samples[i]));
		}
		Sleep(100);
	}
	fclose(fp);
	status = UsbDrDaqStop(g_handle);
	_getch();
}

Please note that the sound waveform can have negative values. What are you capturing the sound waveform from?

Have you tried plotting the data?

I hope this helps. If you are still having issues, please post your code file here or send it to support@picotech.com
Hitesh

Technical Support Engineer

Post Reply