AGG Software Forums

Online Help Center => Data loggers and plug-ins => Topic started by: mccallum4004 on June 23, 2010, 12:58:05 AM



Title: Getting started with the scripting module
Post by: mccallum4004 on June 23, 2010, 12:58:05 AM
From the parser, I generate a DATA string.  This string is really an array of two byte short integers.  In the expressions module, I can get the integer value correctly sent over DDE if I use the expression DATAi = WORDTOSTR(SUBSTR(DATA,2*i+1,2)).  However, I am not really interested in the individual data points but would like to calculate a result from the whole array and send that.  So I have tried using the scripting module.

I'm getting an error message (very vague - "There is an error in the script") when I try to run a script.  I have some experience with Pascal and C++ but not scripting versions.  There is little detail in the help file for the scripting module.  Is the argument for cos, for example, in degrees or radians?  In particular, how does one get a variable from the parser into the script.  I would like to turn DATA into word dist[141].  From there, I think I can calculate the RESULT value that I want.  Then can I send the result out via DDE just by calling the scripting variable as I did the expression variable?



Title: Re: Getting started with the scripting module
Post by: Arthur Grasin on June 23, 2010, 09:52:22 AM
Please, look aе samples. You can find samples in the "program folder\plugins\scriptexec\Samples" folder. Retrieving a parser variable (Pascal script):

Code:
var sName1:string = 'DATA';
     sName2:string = 'RESULT';
     data: string;
     res: double;
begin
// checks that a variable is stored before
if IsVariableStored(sName1) then
 // retrieves a stored variable
 data := PopVariable(sName1)
else
 data := '';

 // calculate your result here....

 // adds or changes a variable in a data packet
 SetVariable(sName2, res);
 // stores a value between script executions (if necessary)
 PushVariable(sName2, res);

end.


Title: Re: Getting started with the scripting module
Post by: mccallum4004 on June 23, 2010, 06:56:46 PM
As I understand it, when using scripting, the connection between the parser and the data export modules is the stack used by PushVariable and PopVariable.  Is that correct?

I have tried to get my data string into an integer array using

dist := StrToInt(Copy(data,2*i-1,2));

but I think this line is what is generating the error message 'is not a valid integer value' when the script runs.  This is the same error message I got when I was trying to parse directly into integers rather than using the string type in the parser.

I had also tried typecasting directly Copy(data,2*i-1,2):Integer; but got an error with that.


Title: Re: Getting started with the scripting module
Post by: Arthur Grasin on June 23, 2010, 07:14:22 PM
I suppose that "dist" and "i" are not declared in the "var" section.

Code:
var sName1:string = 'DATA';
     sName2:string = 'RESULT';
     data: string;
     dist: string;
     i: integer;
     res: double;
begin
...

Could you post your full script? I'll verify it.


Title: Re: Getting started with the scripting module
Post by: mccallum4004 on June 23, 2010, 08:31:02 PM
They are declared.  They are both integers.  It is tough to get a copy of the script because my operating computer is out in the field and not connected to the internet.  I will be out for a couple of hours soon but basically my script is

var sname1:string = 'DATA'  //the string from the parser
var data:string;
var dist:array[141] of word;
var i: integer;

begin
  // get data as per sample
  data := PopVariable(sname1);  //question - do I need the redundancy of variables here?
  // try to create an integer array
  for i := 1 to 141 do
    dist := StrtoInt(Copy(data,2*i-1,2));

//continue calculating ...




Title: Re: Getting started with the scripting module
Post by: Arthur Grasin on June 23, 2010, 09:05:17 PM
The working script is below. I've modified your conversion routine.

Code:
var

sname1:string = 'DATA_PACKET';  //the name of the parser item
data:string;
dist:array[1..141] of word;
i,j: integer;
begin
  // get data from the parser item
  data := GetVariable(sname1);
  // try to create an integer array
  i := 1; j := 1;
  while (i<length(data))and(j<=141) do
   begin
    dist[j] := ord(data[i])+$100*ord(data[i+1]);
    //SetVariable('VAL'+IntToStr(j), dist[j]); // export value
    j := j + 1;
    i := i + 2;
   end;

//continue calculating ...
end.

I think that this code

Code:
StrtoInt(Copy(data,2*i-1,2));

can't get a WORD value in any case.


Title: Re: Getting started with the scripting module
Post by: mccallum4004 on June 24, 2010, 12:20:58 AM
Thanks.  I'll try that this afternoon.  I wouldn't have thought to use the Ord() function - just like I didn't expect to parse the original binary stream as a "string,"  which is what worked when using the expressions module and I've kept that.


Title: Re: Getting started with the scripting module
Post by: mccallum4004 on June 25, 2010, 03:01:21 AM
Thanks.  That worked great.  Another question: My cosine conversion doesn't seem to give me the right numbers.  Your help file doesn't say whether the argument should be in radians or degrees and the expressions help file says that the cos function is the cosecant, rather than the cosine.  Could this be correct? 

By the time you get this, I may have figured it out by trial and error.  The program has worked flawlessly but the documentation is hardly useable.  If it hadn't been for the support here, I wouldn't be using this program.


Title: Re: Getting started with the scripting module
Post by: Arthur Grasin on June 25, 2010, 09:05:44 AM
Cos(X) - Cos returns the cosine of the angle X. X is a real-type expression that represents an angle in radians.