How does SCPI Server works?

Applications, development tools, FPGA, C, WEB
Post Reply
grafzahl
Posts: 11
Joined: Mon Mar 02, 2015 2:34 pm

How does SCPI Server works?

Post by grafzahl » Fri Mar 27, 2015 7:42 pm

Hi,

i used following code on RP to send trace data with ~20Hz to Desktop Computer.

Code: Select all

char strges[array_size*9+500];
sprintf(strges, "Some Text information #StartOfTrace");
for(int g=0; g<array_size; ++g){
  sprintf(buffer, "%f ", buff[g]);
  strcat(strges, buffer);
}
strcat(strges, "#EndOfTrace #End\n");

/* send data to socket * /
write(sockfd, strges, strlen(strges));
It works fine, as long as the trace is small (<1000 Samples).
But the loop isn't really nice coded, so for 16000 Samples it takes 5 seconds, only for the loop.

So i switched to use memcpy(), which is really fast, but i got problems by decoding the code on desktop, because the size of the strings vary.

Code: Select all

int anzahlzeichen = 150;
char tosend[array_size*sizeof(float)+200];
sprintf(tosend, "Some Text information #StartOfTrace");
memcpy(tosend+anzahlzeichen, buff, array_size*sizeof(float));
char tempstr[50];
sprintf(tempstr, "#EndOfTrace #End\n");
memcpy(tosend+anzahlzeichen+array_size*sizeof(float), tempstr, strlen(tempstr));
tosend[anzahlzeichen+array_size*sizeof(float)+strlen(tempstr)]='\0';
write(sockfd, tosend, anzahlzeichen+array_size*sizeof(float)+strlen(tempstr));
Now to my question: How does SCPI Server works? Matlab shows how fast the TCP sending of large data could be, so i wanna progam the same for my needs.
Is there a source code from it? i only found the github page ( https://github.com/RedPitaya/RedPitaya/ ... cpi-server ), but there is only the bin file.
OR: Can someone explain me, how to send large data over TCP?

Thanks in advance
GrafZahl

Nils Roos
Posts: 1441
Joined: Sat Jun 07, 2014 12:49 pm
Location: Königswinter

Re: How does SCPI Server works?

Post by Nils Roos » Sun Mar 29, 2015 11:38 pm

Hi
grafzahl wrote: ... but i got problems by decoding the code on desktop, because the size of the strings vary.
...
OR: Can someone explain me, how to send large data over TCP?
The way you are sending out data in the memcpy variant is quite alright.
When decoding your message, you need to account for the fact that only the 150 byte preamble and 50 byte tail are strings. The actual samples are binary data, and can't be handled by string functions (at least in some languages, eg. C). This also means that you may not be able to search the message for the tag '#EndOfTrace' with string functions.
I would suggest to put the size of the sample array into the preamble. Read that first, parse it, then read the remainder of the message, whose length you now know.

grafzahl
Posts: 11
Joined: Mon Mar 02, 2015 2:34 pm

Re: How does SCPI Server works?

Post by grafzahl » Mon Mar 30, 2015 9:29 am

Hi, thanks for the reply!

I realized it similar to your suggestion (see code below).
It works in 90% but sometimes i get wrong values after the first 2000 to 4000 floats. Values should always be in [-20, 20] but i get nan, 7.4*10^-47, 0, ...
Comparing the output of both codes, the RP gives me "#EndOfTrace #End" like he should and the Desktop returns nothing or something like "�h� "

My guess is, that RP and Desktop forget to send or receive one byte. But float has 4 byte, so after forgetting one byte, he uses 3 bytes from float + 1byte from float [i+1].
Perhaps here (memcpy(wholeTrace + bufgrossindex, buf, 1023); bufgrossindex += 1023;) is the problem (1023, not 1024)???

I hope you understand my problem and my guess

RedPitaya:

Code: Select all

uint32_t array_size =1000; //Current buffer size / Anzahl der Werte.
float *buff = (float *)malloc(array_size * sizeof(float));
rp_AcqGetOldestDataV(RP_CH_2, &array_size, buff);

int numberOfCharBeforeValues = 150;
int numberOfCharAfterValues = 50;
char tosend[array_size*sizeof(float)+numberOfCharAfterValues+numberOfCharBeforeValues];

sprintf(tosend, "#Start Checksum: %s Integral: %f NumberOfPoints: %i NullVoltIntA: %i NullVoltIntB: %i IntA: %i IntB %i #StartOfTrace                                          ",
checksum, integral, array_size, n, m, a, b);

memcpy(tosend+numberOfCharBeforeValues, buff, array_size*sizeof(float));

char tempstr[numberOfCharAfterValues];
sprintf(tempstr, "#EndOfTrace #End\n");
memcpy(tosend + numberOfCharBeforeValues + array_size*sizeof(float), tempstr, strlen(tempstr));

tosend[numberOfCharBeforeValues + array_size*sizeof(float) + strlen(tempstr)]='\0';

write(sockfd, tosend, numberOfCharBeforeValues + array_size*sizeof(float) + strlen(tempstr));

char tracetext2[50];
memcpy(tracetext2, tosend + 150 + array_size*sizeof(float), 50-1);
tracetext2[50-1]='\0';
printf("%s", tracetext2);
Desktop:

Code: Select all

while(labSocket->canReadLine())
{
	char buf[1024];
	labSocket->readLine(buf,sizeof(buf));

	memcpy(wholeTrace + bufgrossindex, buf, 1023);
	bufgrossindex += 1023;
}

char tracetext1[numberOfCharBeforeValues];
memcpy(tracetext1, wholeTrace, numberOfCharBeforeValues-1);
tracetext1[numberOfCharBeforeValues-1]='\0';

float *values = (float *)malloc(numberOfSamples * sizeof(float));
memcpy(values, wholeTrace + numberOfCharBeforeValues, numberOfSamples*sizeof(float));

char tracetext2[numberOfCharAfterValues];
memcpy(tracetext2, wholeTrace + numberOfCharBeforeValues + numberOfSamples*sizeof(float), numberOfCharAfterValues-1);
tracetext2[numberOfCharAfterValues-1]='\0';
qDebug() << tracetext2; //(printf of Qt)

grafzahl
Posts: 11
Joined: Mon Mar 02, 2015 2:34 pm

Re: How does SCPI Server works?

Post by grafzahl » Mon Mar 30, 2015 2:49 pm

We found no solution for that problem, so we decided to send strings again.
For 16000 Samples we cant increase the frequency over 1.25 Hz, but it must be ok.

Nils Roos
Posts: 1441
Joined: Sat Jun 07, 2014 12:49 pm
Location: Königswinter

Re: How does SCPI Server works?

Post by Nils Roos » Mon Mar 30, 2015 5:46 pm

We found no solution for that problem, so we decided to send strings again.
I'd check if readLine() converts occurrences of {0x0d, 0x0a} into a single 0x0a - the Qt docs hint at such a behaviour. Generally, it is risky to use string-oriented functions on binary data.
For 16000 Samples we cant increase the frequency over 1.25 Hz, but it must be ok.
You'll probably get a bit better performance, if you sprintf() the floats in place instead of strcat()ing them.

Code: Select all

int pos;
pos = sprintf(strges, "Some Text information #StartOfTrace");
for(int g=0; g<array_size; ++g){
  pos += sprintf(strges + pos, "%f ", buff[g]);
}
pos += sprintf(strges + pos, "#EndOfTrace #End\n");

/* send data to socket * /
write(sockfd, strges, pos);

grafzahl
Posts: 11
Joined: Mon Mar 02, 2015 2:34 pm

Re: How does SCPI Server works?

Post by grafzahl » Tue Mar 31, 2015 9:04 am

Hey Nils Roos,

we now did virtually the same, but your code is a bit better.

Thanks!

Post Reply
jadalnie klasyczne ekskluzywne meble wypoczynkowe do salonu ekskluzywne meble tapicerowane ekskluzywne meble do sypialni ekskluzywne meble włoskie

Who is online

Users browsing this forum: No registered users and 98 guests