Go Down

Topic: Taking the Serial Port to Write to File (VBScript) (Read 4 times) previous topic - next topic

HEPNinja

So I made sure to scour the internet before asking this, and I even found useful information on this site.  However, I am new to VBScripting.  I have learned how to take control of the Waverunner I am using it, and use it to set various triggers and such on the oscilloscope, but now I am using it to interface with a piece of Arduino software.  My arduino software works, but for the research I need to do, I need to move away from "Copy and Paste" from the Serial Port.  I need to sync time stamps with a clock on another board and monitor humidity and temperature in my cold box through weekend and nightly runs (and don't feel like living in my lab EVERY night.. some are ok.)  Anyway, here is the VBScript code I have been using... but from what I understand from other forum posts, I would need to save to this EEPROM memory first?  I'm not sure I follow.  

What I have been trying to do is upload my Arduino code, then without opening the Serial Monitor, I double click this VBScript... however, nothing gets written....

Sorry if its stretched out, I program in XCode and Notepade, so anyone who helps me may want to copy and paste the code into another text editor to view comments correctly.  Thanks for those who read and reply quickly!

Code: [Select]
'

'  PreconSensorCode.vbs

'

'  Author: xxxxxxxxxxxx

'  Institution: xxxxxxxxxxxx

'  Contact: xxxxxxxxxxxx

'

'      PreconSensorCode is a VB Script that is meant to be used with the LeCroy WaveRunner Oscilloscope

'      

'      The Precon HS-2000V sensor system (off of Arduino Microntrollers) is interfaced
'      in this script.  It is assumed that whoever attempts to use or modify this code will be able
'      to understand enough of the language with accompanying documentation/comments to figure out
'      what they want and don't want.
'


'**************************************************************


'Call the CreateObject to instate ActiveX Control
'To understand the "com" handle more, go to My Computer->Hardware->Device Manager->Ports->USB Serial Port (COM6)
'      You should see the following port settings: BPS=9600, Data Bits=9, Parity=None, Flow Control=None
'      This Translates to COM6:9600,N,8,1
'      Note, you might be using a different COM port, since there are 3 options on the Waverunner, but the details are most likely the same

set app = CreateObject("Lecroy.XStreamDSO")                        'Establish control over the LeCroy Waverunner

set fso = CreateObject("Scripting.FileSystemObject")      'Establish file system control to manage folders and files
Set com = fso.OpenTextFile("COM6:9600,N,8,1", 1)            'Establish control over the COM port that your arduino is attached too, the option 1 is ForReading
set FSO = CreateObject("Scripting.FileSystemObject")      'Since fso is being used to read the COM port, need to define another to Write



'While setting up the scope, stop any Acquisition Process

app.Acquisition.TriggerMode="Stopped"



'Prepare a text file. TRUE=Overwrittable.  False=ASCII script.  Then set the TTree Branch Definitions

dim filetowrite            'Specify the filename to write time stamp too

dim timeStart



filetowrite = InputBox("Enter the DATE and RUN Number for this formatted as RUN10,RUN11,... (start at RUN10 not RUN01) : ","Setting File Name","RUN10",150,150)

timeStart = "1/1/1970 12:00:00 AM"



if FSO.FolderExists("C:\Documents and Settings\LeCroyUSer\Desktop\PreconSensor") = true then

 set DataFile = FSO.CreateTextFile("C:\Documents and Settings\LeCroyUSer\Desktop\PreconSensor\" & filetowrite & ".txt",True,False)

 DataFile.WriteLine("PreconTrigger/F:Date:Time:Meridiem")

else

 FSO.CreateFolder("C:\Documents and Settings\LeCroyUSer\Desktop\PreconSensor")

 set DataFile = FSO.CreateTextFile("C:\Documents and Settings\LeCroyUSer\Desktop\PreconSensor\" & filetowrite & ".txt",True,False)

 DataFile.WriteLine("PreconTrigger/F:Date:Time:Meridiem")

end if


'Now Prepare a text file to read in the Arduino values.  The option 2 sets it to writeable.  The value True will create the file if it doesn't exist.
Set ArduinoFile = FSO.OpenTextFile("C:\Documents and Settings\LeCroyUSer\Desktop\PreconSensor\" & filetowrite & "_Precon.txt", 2, True)


MsgBox("Start to read data from COM")



Do While com.AtEndOfStream <> True

   s = com.ReadLine

   ArduinoFile.WriteLine(s)

   'Response.Write(datediff("s", timeStart, now())

Loop



'Sanity Check to tell the User What happened

MsgBox("Just finished taking Precon triggers and writing to file.")



'Break away from applications and files

DataFile.Close
ArduinoFile.Close
com.Close()

Set app = Nothing

set fso = Nothing
set FSO = Nothing


drspectro

I havent tried this in VBScript, so I am not sure about the following, but in general if you open a DOS COM port as a text file it is NOT a true two way serial connection.

Try the following:
1) open the file as before.
2) send your data from the Arduino then end data with a Ctrl-Z character (I dont remember the hex - look it up).
3) read to the end of file on the com port.
4) close the com port.

To write open the com port for write, then end with a Ctrl-Z.

Test this out, but I think Windows is going to buffer the serial characters until it gets an end of file char. (ctrl-Z).  I dont see why this cant work, but it needs to be coded as a sort of "transaction based" system rather than a "terminal based" exchange.

Like I said - not sure about any of this, but generally COM ports opened as files behave like this.  Thanks for the example - I didnt know you could do that in VBScript.


HEPNinja

Quote
drspectro


Thank you for your reply, I will try this when I get to the lab again tomorrow and see how it works out.

Quote
florinc


Also thank you for the reply, actually... this link is where I received the idea of integrating the Arduino monitoring into my VBScript that automates my LeCroy.  So I have exhausted this site.  You will see bits and pieces of that site in my own code... just expounded upon since the code I give here as an example is a small piece of VBScript that I use for an entire automation process on a cosmic telescope with scintillators, etc.

If it helps, I can post the .pde script that I execute first, but really all it does is print a bunch of print statements to the Serial Monitor.  It is these print statements that I want to get into the file, and that link suggested the VBScript method you see above in my own code.

HEPNinja

#4
Aug 06, 2009, 10:44 pm Last Edit: Aug 06, 2009, 11:17 pm by tkelly72 Reason: 1
So....

I was finally able to get the VBScript to work.  But it didn't work very well.  I had to insert a For Loop instead of the While Loop.  By the time I received anything across the port and into a file, it came across as chicken-scratches.  The ASCII characters weren't intact.

I then managed to get a bash scripting to work, but that wasn't for nice for the LeCroy and it meant headaches porting it to a Linux Box having to manage stty information on whichever port I happened to connect the Arduino up too.

In the end, I spent all of today learning Processing, which turns out to be perfect.  By perfect, I mean I can write something in Processing on the Mac, send over the .pde file, and any other operating system that has Processing can just load up the .pde file and get to work... making it cross platform pleasing (my lab runs between Macs, PCs, and a Linux box... hence why I am crossing all these code types together.)

All in all, I packaged something together that I have called PreconSensorCodeBundle.  It includes Arduino Code to operate an Arduino Nano (or any Arduino with at least 4 analog pins) with two Precon Sensors hooked up to it.  The Processing script takes in Temperature and Humidity values and writes them to a file.  I then have an optional bash script which uses regular expressions to reformat the text file's data stamps (since I use Date and Calendar within the Processing Script).  For those who are familiar with ROOT, the bundle then includes a ROOT UnNamed Macro to analyze the file, create graphs/plots, and then write all graphs and canvases to a root file.

The root files is then portable to anyone else who has ROOT.


Thanks for the help both of you, but in the end I have found Processing to be a much more powerful and useable tool.  For anyone else interesting in writing Serial Monitor to File, I include my Processing Code here.  For those interested in the regular expression script and ROOT script, feel free to message me:

Code: [Select]
import processing.serial.*;

Serial port;
String PreconReadings;
Date StartingTimeStamp = new Date();
Date AnyTimeStamp = new Date();
Date EndingTimeStamp = new Date();
PrintWriter PreconReadingsFile;
Calendar Timing = Calendar.getInstance();
float numberofreadings = 0;

void setup() {
 size(100,100);
 println(Serial.list());
 port = new Serial(this, Serial.list()[1], 9600);
 PreconReadingsFile = createWriter("PreconReadingFile.txt");
 PreconReadingsFile.flush();
 StartingTimeStamp = Timing.getTime();
 println("Now writing data to file!  Starting time is: " + StartingTimeStamp);
 
}

void draw() {
}

void serialEvent(Serial port) {
  PreconReadings = port.readString();
  if(PreconReadings != null) {
    PreconReadingsFile.print(PreconReadings);
    PreconReadingsFile.flush();
  }
  if(PreconReadings.equals("*")) {
    Timing = Calendar.getInstance();
    AnyTimeStamp = Timing.getTime();
    PreconReadingsFile.print(" " + AnyTimeStamp);
    PreconReadingsFile.flush();
    println("Number of readings: " + numberofreadings);
    ++numberofreadings;
  }
}


Edit:
And for those who are wanting to see how to monitor four analog signals and print out results based on a pvss sort of system (i.e. don't print a reading within some small range of temperature fluctuation to ignore thermal noise:

Code: [Select]
/*
*  Author: Tony Kelly
*  Email: xxxxxxxxxxxxxxxxx
*
*  The PreConSensor code is meant to be used with the Arduino Nano microcontroller, as well as, the HS-2000V Precon sensor.
*    This sensor is able to measure both relative humidity from 0 to 100% and the Temperature from -30 to 100 Celsius.  It
*    arrives from the manufacturer already calibrated.  This code attempts to take input into analog pins then record and
*    graph that information over time.
*
*  Special Note 1: The analogRead function takes a parameter that specifies the pin number to be read.  For example,
*    analogRead(0) reads out the signal on A0 pin of Nano board.  Since it takes a 5V input, it will spread the 0 to
*    5 volt range over a read out value of 0 to 1023.  This means that the number returned is a unit of .0049 volts.
*    If the analogRead returns 200 (for example), then it must have read 200*.0049 = 0.98 Volts on the analog pin.
*    This might need to be modified by whoever uses this code.  For example, your own powersupply might actually give
*    4.95 volts which would actually give 4.95/1024 = .00483 volts.
*/

int MODTemp = 0;
int MODHumid = 1;
int FPGATemp = 2;
int FPGAHumid = 3;
int Holder = 0;

int ledPin = 13;

float previous[4] = {0,0,0,0};
float present[4]  = {0,0,0,0};

int readswitch = 0;


void setup()
{
     pinMode(ledPin, OUTPUT);
     pinMode(MODTemp, INPUT);
     pinMode(MODHumid, INPUT);
     pinMode(FPGATemp, INPUT);
     pinMode(FPGAHumid, INPUT);
     Serial.begin(9600);

     Holder = analogRead(MODTemp);
     present[0] = 0.12695313*Holder-30;
     Holder = analogRead(MODHumid);
     present[1] = 0.09765625*Holder;
     Holder = analogRead(FPGATemp);
     present[2] = 0.12695313*Holder-30;
     Holder = analogRead(FPGAHumid);
     present[3] = 0.09765625*Holder;
           Serial.print(present[0]);
           Serial.print(" ");
           Serial.print(present[1]);
           Serial.print(" ");
           Serial.print(present[2]);
           Serial.print(" ");
           Serial.print(present[3]);
           Serial.println(" *");
}

void loop()
{
   readswitch = 0;
   analogReference(EXTERNAL);
   Holder = analogRead(MODTemp);
   Holder = analogRead(MODTemp);
   present[0] = 0.12695313*Holder-30;
   Holder = analogRead(MODHumid);
   present[1] = 0.09765625*Holder;
   Holder = analogRead(FPGATemp);
   present[2] = 0.12695313*Holder-30;
   Holder = analogRead(FPGAHumid);
   present[3] = 0.09765625*Holder;

   if (present[0]-previous[0] > 0.30 || previous[0]-present[0] > 0.30) {
           Serial.print(present[0]);
           Serial.print(" ");
           Serial.print(present[1]);
           Serial.print(" ");
           Serial.print(present[2]);
           Serial.print(" ");
           Serial.print(present[3]);
           Serial.println(" *");
           readswitch=1;
               for (int i=0; i<4 ; ++i){
           previous[i] = present[i];
               }  
     }
     if(readswitch != 1) {
           if (present[1]-previous[1] > 0.30 || previous[1]-present[1] > 0.30) {
                 Serial.print(present[0]);
                 Serial.print(" ");
                 Serial.print(present[1]);
                 Serial.print(" ");
                 Serial.print(present[2]);
                 Serial.print(" ");
                 Serial.print(present[3]);
                   Serial.println(" *");
                       for (int i=0; i<4 ; ++i){
                   previous[i] = present[i];
                       }  
             }
     }
             delay(100);
}

Go Up