Charmed Quark Systems, Ltd. - Support Forums and Community
how would i read a csv file? - Printable Version

+- Charmed Quark Systems, Ltd. - Support Forums and Community (http://www.charmedquark.com/vb_forum)
+-- Forum: General Discussion (http://www.charmedquark.com/vb_forum/forumdisplay.php?fid=3)
+--- Forum: General Automation (http://www.charmedquark.com/vb_forum/forumdisplay.php?fid=11)
+--- Thread: how would i read a csv file? (/showthread.php?tid=10427)



how would i read a csv file? - indygreg - 11-13-2017

i managed to get a macro to write all of my temps to a csv file with the format shown below.  i am really not a macro programmer but i can code a little and someone posted 95% of this one so i got it working.  i am having so much trouble with stunnel which i have been using to pop3 some data from one cqc system to another that i am wondering if i could just use a data file.  i have it writing to a dropbox so within seconds of my remote system writing it i have it on my local system.  would anyone be able to help me figure out a driver to read a csv file into variables?  or i could write this a single value to a file if that made it easier or i could even format it as xml if there were a way to read that.  

any pointers would be appreciated.

greg


Code:
Time,Outside,Inside,FreshWater,BlackWater,GreyWater,Fridge,BatteryVolts
12:07:01 PM,43.20000,47.50000,54.10000,39.00000,42.30000,39.70000



RE: how would i read a csv file? - Dean Roddey - 11-13-2017

Create a FileInStream object for the file you want to read (it'll have to be under CQCData\MacroFileRoot somewhere.) That is a derivative of TextInStream which provides the real functionality so look at that class for the dteails.

You know you have two lines, so do two calls to GetLine(outstring) to fill in two strings. Once you have them, you can use the StringTokenizer class which has methods to break a CSV line into a vector of strings which you can then loop through. If you need to parse both lines, create two vectors of strings and parse each of them separately.

You can create a vector of strings like this:

Code:
Types=
    VectorOf[String]     MyValList;
EndTypes;

Then just create a MyValList for each line and pass it to the string tokenizer's ParseCSVLine() method to get the lines into separate strings.


RE: how would i read a csv file? - indygreg - 11-13-2017

thanks dean. FileInStream was the key. i found another macro very similar to what i need and modified it to work. i am not building an actual driver but i can see the values are in the MyParmStrArray object after running the below statement. How would i address individual elements of that array and move them into global vars that my interfaces can read?

STok.ParseCSVLine(TarLine,MyParmStrArray,IndexErr);

greg


RE: how would i read a csv file? - Dean Roddey - 11-13-2017

You would just do:

GlobalVars:SetVariable(GVar:SomeVar, MyParmStrArray[0]);
GlobalVars:SetVariable(GVar:SomeVar2, MyParmStrArray[1]);

and so on.


RE: how would i read a csv file? - indygreg - 11-14-2017

Dean i really appreciate your patience with us part time cqc guys. for 10 years i have drifted in and out of here when i needed to add something and you have always taken the time to help out with stuff like this. i know how much code you create so i dont know how you have the time for everybody.

g


RE: how would i read a csv file? - indygreg - 11-14-2017

spoke too soon.  i get an error expected a method call, object reference or flow control on that line.  is this a command that should work in a macro?

i really struggle with docs.  i know you have been reworking them and i think you said you were putting it into a single pdf that could be searched.  often i really don't know which webpage to look on so i just want to search all of the documentation for a keyword.  i see reference docs for cml but i don't see anything for how to develop macros.  where would i go to find answers to questions like "how do you pass parameters into and out of a macro"?   


Code:
Class=[NonFinal]
   ClassPath MEng.User.Transit.ReadStatsFile;
   ParentClass MEng.Object;
EndClass;


Imports=
   MEng.System.Runtime.FileInStream;
   MEng.System.Runtime.StringTokenizer;
EndImports;

Types=
EndTypes;

Members=
   //String m_FilePath;
   String m_InStream;

   StringTokenizer.ParmStrList MyParmStrArray;

   //Float8 m_Eto;
EndMembers;


Methods=[Public,Final]

   Constructor()
   Begin
   
   EndConstructor;

   Method Start() Returns Int4
   Begin
Locals=
           FileInStream TransitLogFile;
           Card4 IndexErr;
           String TarLine;
           String CurLine;
           StringTokenizer STok;
       EndLocals;
       
       //Open TransitLogFile
       TransitLogFile.Open("\\TransitLogFile.csv");
       
       //Search for the last in the list before end of stream
       While(!TransitLogFile.EndOfStream())
           TransitLogFile.GetLine(CurLine);
           CurLine.StripWhitespace();
           If (!CurLine.IsEmpty())
               TarLine := CurLine;
           EndIf;
       EndWhile;
       
       //Parse CSV and store individual strings
       STok.ParseCSVLine(TarLine,MyParmStrArray,IndexErr);

       GlobalVars:SetVariable(GVar:TransitVanOutsideTemp, MyParmStrArray[1]);
   
       
       Return 0;

   EndMethod;

EndMethods;



RE: how would i read a csv file? - Dean Roddey - 11-14-2017

You can't set the variable in the macro. That's an action level this. Instead, change Start() to be like this:

Code:
Method Start([Out] String MyValue) Returns Int4
Begin
   ....

    MyValue := MyParmStrArray[1];
EndMethod;

If you pass GVar:TransitVanOutsideTemp as the parameter when you invoke the macro via MacroEng::RunMacro(), it will become the storage behind the MyValue parameter. So, when you set MyValue in the macro, you are actually setting the macro and it so it will have that set value upon return from the macro back to the action level.


In the CML section of the help there is a language reference and a class library reference. The language reference section describes the language itself.

I will continue to try to find the time to have my help compiler generate an overall help index page, but haven't been able to yet. I did it for drivers, but not an overall index.