Page 181 - The Unofficial Guide to Lego Mindstorms Robots
P. 181

170


                      ' Open the output file.
                      out = FreeFile
                      open filename For Output As #out

                      .InitComm
                      ' First   get item zero, which describes the length of the datalog.
                      data = .UploadDatalog(0, 1)
                      length = data(   2, 0) - 1

                      ' Now upload 50 items at a time.
                      index = 0
                      While (index < length)
                           ' Find the smaller of t    he remaining items or 50.
                           stepSize = min(length - index, 50)
                           ' Get the data.
                           data = .UploadDatalog(index + 1, stepSize)

                           ' Write it out to a file.
                           For i = o To stepSize -1
                               line =  getTypeString(data(0, i)) + "," + _
                                   Str (data(1, i)) + "," +_
                                   Str (dat  a(2, i))
                               Print #out, line
                           Next I

                           index = index + stepSize
                      Wend

                      .CloseComm

                      ' Close the file.
                      Close #out
                  End With
              End Sub
              Function min(n1 As Integer, n2 As Integer) As Integer
                  If n1 < n2 Then
                      min = n1
                  Else
                      min = n2
                  End If
              End Function
              Function getTypeString(ByVal code As Integer) As String
                  getTypeString = Switch( _
                      code = 0, "Variable", _
                      code = 1, "Timer", _
                      code = 2, "Constant", _
                      code = 3, "Motor status", _
                      code = 4, "Random", _
                      code = 8, "Program number", _
                      code = 9, "Sensor value", _
                      code = 10, "Sensor type", _
                      code = 11, "Sensor mode", _
                      code = 12, "Sensor raw", _
   176   177   178   179   180   181   182   183   184   185   186