hexagon logo

Writing/Reading a .txt file

This is very much a VB question not related to PC-DMIS. There is so much information out there for VB that I find it very hard to locate a good example of what I want to do.

I have a script at the beginning and end of my part program. I would like to get the total runtime of the program. I have a way to get systemtime in each and put it into seconds and such, thanks to this message board.

I do not want to write the beginning time to a variable and have it go through the part program. What I wanted was a temporary .txt file or something similar that I could write the begintime value (in seconds) to. The second script would grab this value and use it. The .txt file either needs to be cleared or overwritten by the new begintime number every execution. How do I code this?

My goal is to take these values and use them to calculate CMM run time for the day in Excel. Can I export the Total Runtime number I get in the second script to Excel? It will probably be easier to google information for this; I will update this post if I find what I am looking for.

Thanks!
  • I'm using VB express and I recently wrote a script to dump data to a text file. I used the WriteAllText Method of the My.Computer.FileSystem object. There is also a method for reading and deleting the files using the My.Computer.FileSystem object. Give that a shot and see if it gets you anywhere.
  • I would create a running log of start and end times with part program name. then you can use excel to evaluate the run time per day. Save the start time in a variable(or write it out to a file) then at the end of the program combine it with the end time, program name and whatever else you want to track and save it(using pcdmis commands) to a log file. Separate the fields with a comma.
  • A quickie I wrote in VB for capturing the time. You will need to write this into two seperate files.

    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub Main()[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteNow As DateTime = DateAndTime.Now[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteHour As Integer = dteNow.Hour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteMinute As Integer = dteNow.Minute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteSecond As Integer = dteNow.Second[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim strTime As String[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strTime = Str(dteHour) + ":" + Str(dteMinute) + ":" + Str(dteSecond)[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       My.Computer.FileSystem.WriteAllText("C:\temp\xtime.txt", strTime, False)[/COLOR][/SIZE]
     
    [SIZE=2][COLOR=#0000ff]       MsgBox("Wait for it...", MsgBoxStyle.OkOnly)   [COLOR=red]<----Seperate here[/COLOR][/COLOR][/SIZE]
    [COLOR=#ff0000][/COLOR][COLOR=#0000ff] 
    [SIZE=2]       dteNow = DateAndTime.Now[/SIZE]
    [/COLOR][/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim strStartTime As String[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartHour As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartMinute As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartSecond As Integer[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strStartTime = My.Computer.FileSystem.ReadAllText("C:\temp\xtime.txt")[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff] 
    [SIZE=2][COLOR=#0000ff]       dteStartHour = Int(Left(strStartTime, 3))[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       dteStartMinute = Int(Mid(strStartTime, 5, 3))[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       dteStartSecond = Int(Mid(strStartTime, 9, 3))[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim dteEndHour As Integer = dteNow.Hour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteEndMinute As Integer = dteNow.Minute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteEndSecond As Integer = dteNow.Second[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim intElapsedHour As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim intElapsedMinute As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim intElapsedSecond As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim strElapsed As String[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff]
    [SIZE=2][COLOR=#0000ff]       If dteStartSecond > dteEndSecond Then[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndMinute = dteEndMinute - 1[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndSecond = dteEndSecond + 60[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       End If[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       If dteStartMinute > dteEndMinute Then[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndHour = dteEndHour - 1[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndMinute = dteEndMinute + 60[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       End If[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       intElapsedHour = dteEndHour - dteStartHour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       intElapsedMinute = dteEndMinute - dteStartMinute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       intElapsedSecond = dteEndSecond - dteStartSecond[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strElapsed = Str(intElapsedHour) + ":" + Str(intElapsedMinute) + ":" + Str(intElapsedSecond)[/COLOR][/SIZE][/COLOR][/SIZE]
    [COLOR=#0000ff][/COLOR] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       MsgBox("Elapsed time: " + strElapsed, MsgBoxStyle.OkOnly)[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       My.Computer.FileSystem.DeleteFile("C:\temp\xtime.txt")[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]
    [SIZE=2][COLOR=#0000ff]   End Sub[/COLOR][/SIZE][/COLOR][/SIZE]


  • I use this script to track how many time the CMM is used

    Sub Main()
    Dim AssetNumber As String
    Dim AssetLocation As String
    Dim PCDiLocation As String

    Dim StartDate As String
    Dim StartTime As String

    StartDate = Format(Date, "Short Date")
    StartTime = Format(Time, "Short Time")

    Open "c:\temp\CMMInit.txt" For Input As #1
    ' Where PC-DMIS The CMM Plant floor
    ' is installed Asset # CMM Location
    Input #1, PCDiLocation, AssetNumber, AssetLocation
    Close #1

    Open "c:\CMMAvail.txt" For Append As #1
    Print #1, AssetNumber & "," & AssetLocation & ",1," & StartDate & "," & StartTime
    Close #1

    End Sub

    I keep data about the CMM in a text file located in the "c:\temp" folder, the file name is CMMInit.txt. The data is comma separated and looks like this.

    C:\Program Files\WAI\PC-DMIS V42 MR1,OFFLINE,CM

    The first column is the installation location of PC-DMIS. The next column is the asset or serial number of the CMM. The last column is the location of the CMM on the shop floor.

    Hope this gets you started.
  • I was having trouble getting any of these to work for me, so I wrote this up yesterday:


    Sub Main()
    
    Dim Beforetime As String
    Beforetime = Timer
    Beforetime = CInt(Beforetime)
    
    Open "I:\CMM_MASTER_PROGRAMS\Scripts\TIMEVARIABLE.TXT" For Output As #1
    Print #1, Beforetime
    Close #1
    
    End Sub


    The Timer object grabs the number of seconds since midnight. Simpler than grabbing system time in 3 different ways and converting it. CInt() rounds the time, because it goes to 2 decimal places in the value it puts out. Opening and writing to the .txt file in this method overwrites the file contents every time, so second problem solved.

    So now I have a simple way of getting program run time. I can grab date, and program name easy enough. Somehow I now have to get all these values into an excel file and make some pretty charts and graphs out of it. I have the Excel help files open now.....
  • I would think that the easiest way to get it into excel would be to go the log file route that cmmguy suggests. If you set it up correctly a comma seperated log file imports very cleanly and easily into Excel. Then you don't have to muck around with opening the excel page reading the worksheet to find your next avaliable line placeing the data and then updating the graph all from within your script.

    Are you using Cypress or some other program for creating your scripts?
  • I am actually writing these scripts in the VB editor in Excel, and then testing them to make sure they run ok through PC-DMIS. I am aware some syntax is different, but I am not going too complex just yet, so crossing my fingers that I won't run into problems.

    Thanks for the suggestions, much appreciated, all.

    So, going off of what cmmguy was saying, I could export my information to a log file so that it looked something like: 200,9242009,350-51,. 200=runtime in seconds, 9242009=date, 350-51=part number, etc. etc. for however many fields I want. Should I end each bit of information with a common character or something to make it easier to sort when bringing it into excel? I don't know what will be easiest when I start to bring stuff into excel, I am going to attempt to do that today for the first time.
  • if you place a comma in between each piece of data Excel will import and sort this data almost automaticlly into seperate cells. As long as each line in your log file contains the data formated in the same way, which it should if you place it in there through scripting, it will format the data very nicely. If you plan on evenutally putting this data into charts you will not want to mix data types in beyween commas, ie 200=runtime in seconds, would be a no-no. If you know the order of the fields you can create a worksheet for the charting that you plan to do, open your log file into excel and then paste the data from that log file directly into your worksheet and be done.
  • Dont end the record of info with a comma.

    The CSV(comma separated values) format is a native format to excel and when you open the file with excel, it will automatically parse the fields into columns for you and the records into rows. It is very clean and will only require a simple macro to compute the rest of the stuff.
  • I would though suggest that you start with the charts that you want to create. Once you know how those will look and how you need to setup the data that feeds those charts the rest is pretty simple and you already have the basic idea on how to send the data to the file. The only differance is that you want to open your log file for Append so you can continously add data to it. At least that is the way I would envision achieving what you are after.