Convert.ChangeType()返回不正确的值

时间:2015-06-17 19:27:46

标签: arrays vb.net cnc

我有一个解析CNC文件的课程,但我在跟踪"单词"在文件的每一行。

我的代码解析所有领先的"单词"直到它到达最后一个字。在解析" Z"时,它最引人注目。值或其他Double类型值。我已经对它进行了足够的调试,注意到它成功地解析了数值,就像它使用" X"和" Y"值,但它似乎没有成功地将其转换为double。我缺少某个角色是否有问题?

这是我的代码:



    If IO.File.Exists("Some GCode File.eia") Then
        Dim sr As New IO.StreamReader("Some GCode File.eia")
        Dim i As Integer = 0
        'Read text file
        Do While Not sr.EndOfStream
            'Get the words in the line
            Dim words() As String = sr.ReadLine.Split(" ")
            'iterate through each word
            For i = 0 To words.Length - 1 Step 1
                'iterate through each "registered" keyword. Handled earlier in program
                For Each cmd As String In _registeredCmds.Keys
                    'if current word resembles keyword then process
                    If words(i) Like cmd & "*" Then
                        _commands.Add(i, _registeredCmds(cmd))
                        'Double check availability of a Type to convert to
                        If Not IsNothing(_commands(i).DataType) Then
                            'Verify enum ScopeType exists
                            If Not IsNothing(_commands(i).Scope) Then
                                'If ScopeType is modal then just set it to True. I'll fix later
                                If _commands(i).Scope = ScopeType.Modal Then
                                    _commands(i).DataValue = True
                                Else
                                    'Catch errors in conversion
                                    Try
                                        'Get the value of the gcode command by removing the "registered" keyword from the string
                                        Dim strTemp As String = words(i).Remove(0, words(i).IndexOf(_commands(i).Key) + _commands(i).Key.Length)
                                        'Save the parsed value into an Object type in another class
                                        _commands(i).DataValue = Convert.ChangeType(strTemp, _commands(i).DataType)
                                    Catch ex As Exception
                                        'Log(vbTab & "Error:" & ex.Message)
                                    End Try
                                End If
                            Else
                                'Log(vbTab & "Command scope is null")
                            End If
                        Else
                            'Log(vbTab & "Command datatype is null")
                        End If
                        Continue For
                    End If
                Next
            Next
            i += 1
        Loop
    Else
        Throw New ApplicationException("FilePath provided does not exist! FilePath Provided:'Some GCode File.eia'")
    End If

以下是GCode的一个示例:



    N2930 X-.2187 Y-1.2378 Z-.0135
    N2940 X-.2195 Y-1.2434 Z-.0121
    N2950 X-.2187 Y-1.249 Z-.0108
    N2960 X-.2164 Y-1.2542 Z-.0096
    N2970 X-.2125 Y-1.2585 Z-.0086
    N2980 X-.207 Y-1.2613 Z-.0079
    N2990 X-.2 Y-1.2624 Z-.0076
    N3000 X0.
    N3010 X12.
    N3020 X24.
    N3030 X24.2
    N3040 X24.2072 Y-1.2635 Z-.0075
    N3050 X24.2127 Y-1.2665 Z-.0071
    N3060 X24.2167 Y-1.2709 Z-.0064
    N3070 X24.2191 Y-1.2763 Z-.0057
    N3080 X24.2199 Y-1.2821 Z-.0048
    N3090 X24.2191 Y-1.2879 Z-.004
    N3100 X24.2167 Y-1.2933 Z-.0032
    N3110 X24.2127 Y-1.2977 Z-.0026
    N3120 X24.2072 Y-1.3007 Z-.0021
    N3130 X24.2 Y-1.3018 Z-.002
    N3140 X24.
    N3150 X12.
    N3160 X0.
    N3170 X-.2
    N3180 X-.2074 Y-1.3029 Z-.0019
    N3190 X-.2131 Y-1.306 Z-.0018
    N3200 X-.2172 Y-1.3106 Z-.0016
    N3210 X-.2196 Y-1.3161 Z-.0013
    N3220 X-.2204 Y-1.3222 Z-.001
    N3230 X-.2196 Y-1.3282 Z-.0007
    N3240 X-.2172 Y-1.3338 Z-.0004
    N3250 X-.2131 Y-1.3384 Z-.0002
    N3260 X-.2074 Y-1.3415 Z-.0001
    N3270 X-.2 Y-1.3426 Z0.
    N3280 X0.
    N3290 X12.
    N3300 X24.
    N3310 X24.2
    N3320 G0 Z.1
    N3330 Z1.0
    N3340 G91 G28 Z0.0
    N3350 G90

关于上面的示例CNC代码,您会注意到带有尾随Z命令的X和Y命令正确解析。

修改的 每条评论,这里是_commands()的细分 _commands = SortedList(Of Integer, Command) Command是一个具有以下属性的类:

  • 范围 Enum ScopeType
  • 名称 String
  • String
  • DataType Type
  • DataValue as Object

编辑:解决方案! 弄清楚出了什么问题。构成类的构造的数组基本上被传递给"注册的"来自Command类的对象数组。因此,每次我从" word"中解析出值。每一行,我都覆盖了DataValue对象中的Command

解决方案是宣布一个新的命令'每个解析对象并将其附加到正确的数组。

这是我的捷径:


...
For I = 0 To words.Length - 1 Step 1
  'iterate through each "registered" keyword. Handled earlier in program
  For Each cmd as String in _registeredCmds.Keys
    'if current word resembles keyword then process
    If words(I) Like cmd & "*" Then
      'NEW!!! Declare unassigned Command object
      Dim com As Command
      ' ****** New elongated logic double checking existence of values.....
      If _registeredCmds.Keys.Scope = ScopeType.Modal Then
        'assign Command object to previously declared variable com
        com = New Command()'There's technically passing arguments now to ensure items are transferred
      Else
        'Parse and pass DataValue from this word
        com = New Command()'There's technically passing arguments now to ensure items are transferred
      End If
      'New sub to add Command object to local array
      Add(com)
      Continue For
    End If
  Next
Next
...

0 个答案:

没有答案
相关问题