Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how would i read a csv file?
#1
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
Reply
#2
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.
Dean Roddey
Software Geek Extraordinaire
Reply
#3
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
Reply
#4
You would just do:

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

and so on.
Dean Roddey
Software Geek Extraordinaire
Reply
#5
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
Reply
#6
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;
Reply
#7
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.
Dean Roddey
Software Geek Extraordinaire
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  CrystalDiskInfo/read SMART hdd info IVB 2 516 09-30-2016, 10:00 AM
Last Post: IVB
  Anyone with Foscam - read this. jkmonroe 6 907 03-01-2016, 11:55 AM
Last Post: wuench
  Windows 8.1 and file sharing Mark Stega 3 899 10-09-2014, 10:51 AM
Last Post: Dean Roddey
  File location in cab cookie? rtarver 21 2,028 04-07-2014, 11:07 AM
Last Post: Dean Roddey
  CQSL Media Repo & File Tags timaelabu 1 979 04-23-2012, 06:45 PM
Last Post: Dean Roddey
  MKV file and Zoomplayer jpants 0 590 03-21-2010, 02:16 PM
Last Post: jpants
  Automated cross-server file backups IVB 4 746 08-23-2008, 02:11 PM
Last Post: Dean Roddey
  Anyone got a copy of Premiere/etc to edit a WMV file? IVB 17 2,269 07-21-2007, 12:17 PM
Last Post: znelbok
  Just read the CQC Driver Dev Docs! Squintz 27 2,384 10-26-2006, 07:32 PM
Last Post: johnnynine

Forum Jump:


Users browsing this thread: 1 Guest(s)