frmsetan.frm

来自「无线传感器网络节点的定位引擎程序」· FRM 代码 · 共 598 行 · 第 1/2 页

FRM
598
字号
        MsgBox "串口没有被正确打开,请检查配置文件"
        
        Exit Sub
    End If
    Command(0) = COMMAND_QUERY_AD
    Command(1) = Command(0)
    Command(2) = Command(0)
    Command(3) = PC_ADDRESS
    Command(4) = "&H" & strNodeAddress
    seqno = seqno + 1
    
    '修改ini中seqno
    WritePrivateProfileString "SEQNO", "seqno", CStr(seqno), ConfigFilePath
    Int2BYTE seqno, Command(6), Command(5)
    Command(7) = &H0
    Command(8) = &H0
    Command(9) = &H0
    
    MainForm.Text1.Text = MainForm.Text1.Text & Now & " " & "发送" & " "
    
    For i = 0 To UBound(Command)
    MainForm.Text1.Text = MainForm.Text1.Text + " "
    If Len(CStr(Hex(Command(i)))) = 1 Then
        MainForm.Text1.Text = MainForm.Text1.Text + "0"
    
    End If
        MainForm.Text1.Text = MainForm.Text1.Text + CStr(Hex(Command(i)))
    Next
    MainForm.Text1.Text = MainForm.Text1.Text + vbCrLf
    
    
    MainForm.MSComm1.Output = Command
    
    'waitting for response
    
    Dim txtBuff() As Byte
    



    MainForm.MSComm1.InBufferCount = 0 ' clear inBuffer
    Timer1.Enabled = True
    Timer1.Interval = UART_RADIO_RETRY_COUNT * UART_RADIO_HAL_TIME + 2000
    timeout = False
    

    Do
      DoEvents
      If timeout Then
        Exit Do
      End If
    Loop Until ((MainForm.MSComm1.InBufferCount = COMMAND_RESPONSE_LENGTH))
    
    'Timer1.Enabled = False
    If Not timeout Then
        MainForm.Text1.Text = MainForm.Text1.Text & Now & " " & "接收" & " "
        txtBuff = MainForm.MSComm1.Input
        For i = 0 To COMMAND_RESPONSE_LENGTH - 1
        MainForm.Text1.Text = MainForm.Text1.Text + " "
        If Len(CStr(Hex(txtBuff(i)))) = 1 Then
            MainForm.Text1.Text = MainForm.Text1.Text + "0"
        
        End If
            MainForm.Text1.Text = MainForm.Text1.Text + CStr(Hex(txtBuff(i)))
        Next
        MainForm.Text1.Text = MainForm.Text1.Text + vbCrLf
        
        If (CStr(txtBuff(0) = CStr(COMMAND_QUERY_AD_RESPONSE)) And Hex(txtBuff(0)) = Hex(txtBuff(1)) And Hex(txtBuff(1)) = Hex(txtBuff(2))) Then
            '检查设置是否成功
            If txtBuff(6) = SUCCESS_RESPONSE Then
                MsgBox "查询成功", , "success"
                textA.Text = Hex(txtBuff(7))
                textN.Text = Hex(txtBuff(8))
               
            Else
                msgErrorMessage CInt(CStr(txtBuff(7)))
            End If
            
        Else
            MsgBox "错误的响应格式", , "Error"
        End If
    Else
        MsgBox "定时器超时,请检查网关节点和PC串口是否连接完好"
        Timer1.Enabled = False
    End If
    
    
    

End Sub

Private Sub butSetAN_Click()


    Dim strNodeA As String
    Dim strNodeN As String
    
    Dim strNodeAddress As String
    Dim i As Integer
    
strNodeAddress = Trim(textNodeAddress)
strNodeA = Trim(textA.Text)
strNodeN = Trim(textN.Text)


If (strNodeAddress = "") Then
    MsgBox "请输入节点地址", vbOKOnly, "Error"
    Exit Sub
End If
If (Len(strNodeAddress) > 2) Then
    MsgBox "请输入正确的节点地址信息(长度小于2)", vbOKOnly, "Error"
   
    Exit Sub
End If

If Not isHex(Left(strNodeAddress, 1)) Then
    MsgBox "请输入正确的节点地址", vbOKOnly, "Error"
    
    Exit Sub

End If
If Not isHex(Right(strNodeAddress, 1)) Then
    MsgBox "请输入正确的节点地址", vbOKOnly, "Error"
   
    Exit Sub
End If


If (strNodeA = "") Then
    MsgBox "请输入A的值", vbOKOnly, "Error"
    Exit Sub
End If
If (Len(strNodeA) > 2) Then
    MsgBox "请输入正确的A值(长度小于2)", vbOKOnly, "Error"
   
    Exit Sub
End If

If Not isHex(Left(strNodeA, 1)) Then
    MsgBox "请输入正确的A值", vbOKOnly, "Error"
    
    Exit Sub

End If
If Not isHex(Right(strNodeA, 1)) Then
    MsgBox "请输入正确的A值", vbOKOnly, "Error"
   
    Exit Sub
End If

If (strNodeN = "") Then
    MsgBox "请输入N的值", vbOKOnly, "Error"
    Exit Sub
End If
If (Len(strNodeN) > 2) Then
    MsgBox "请输入正确的N值(长度小于2)", vbOKOnly, "Error"
   
    Exit Sub
End If

If Not isHex(Left(strNodeN, 1)) Then
    MsgBox "请输入正确的N值", vbOKOnly, "Error"
    
    Exit Sub

End If
If Not isHex(Right(strNodeN, 1)) Then
    MsgBox "请输入正确的N值", vbOKOnly, "Error"
   
    Exit Sub
End If

If Val("&H" & strNodeN) > 31 Then
    MsgBox "N值位于[0X00,0X1F]", vbOKOnly, "Error"
   
    Exit Sub
End If

'通过串口设置AN
If MsgBox("确定写入节点?", vbOKCancel, "confirm") = vbOK Then
    If (isCommOpen = False) Then
        MsgBox "串口没有被正确打开,请检查配置文件"
        
        Exit Sub
    End If
    Command(0) = COMMAND_SET_AD
    Command(1) = Command(0)
    Command(2) = Command(0)
    Command(3) = PC_ADDRESS
    Command(4) = "&H" & strNodeAddress
    seqno = seqno + 1
    
    '修改ini中seqno
    WritePrivateProfileString "SEQNO", "seqno", CStr(seqno), ConfigFilePath
    Int2BYTE seqno, Command(6), Command(5)
    Command(7) = "&H" & strNodeA
    Command(8) = "&H" & strNodeN
    Command(9) = &H0
    
    MainForm.Text1.Text = MainForm.Text1.Text & Now & " " & "发送" & " "
    
    For i = 0 To UBound(Command)
    MainForm.Text1.Text = MainForm.Text1.Text + " "
    If Len(CStr(Hex(Command(i)))) = 1 Then
        MainForm.Text1.Text = MainForm.Text1.Text + "0"
    
    End If
        MainForm.Text1.Text = MainForm.Text1.Text + CStr(Hex(Command(i)))
    Next
    MainForm.Text1.Text = MainForm.Text1.Text + vbCrLf
    
    
    MainForm.MSComm1.Output = Command
    
    'waitting for response
    
    Dim txtBuff() As Byte
    



    MainForm.MSComm1.InBufferCount = 0 ' clear inBuffer
    Timer1.Enabled = True
    Timer1.Interval = UART_RADIO_RETRY_COUNT * UART_RADIO_HAL_TIME + 2000
    timeout = False
    

    Do
      DoEvents
      If timeout Then
        Exit Do
      End If
    Loop Until ((MainForm.MSComm1.InBufferCount = COMMAND_RESPONSE_LENGTH))
    Timer1.Enabled = False
    If Not timeout Then
        
    
        MainForm.Text1.Text = MainForm.Text1.Text & Now & " " & "接收" & " "
        txtBuff = MainForm.MSComm1.Input
        For i = 0 To COMMAND_RESPONSE_LENGTH - 1
        MainForm.Text1.Text = MainForm.Text1.Text + " "
        If Len(CStr(Hex(txtBuff(i)))) = 1 Then
            MainForm.Text1.Text = MainForm.Text1.Text + "0"
    
        End If
            MainForm.Text1.Text = MainForm.Text1.Text + CStr(Hex(txtBuff(i)))
        Next
        MainForm.Text1.Text = MainForm.Text1.Text + vbCrLf
    
        If (CStr(txtBuff(0) = CStr(COMMAND_SET_AD_RESPONSE)) And Hex(txtBuff(0)) = Hex(txtBuff(1)) And Hex(txtBuff(1)) = Hex(txtBuff(2))) Then
            '检查设置是否成功
            If txtBuff(6) = SUCCESS_RESPONSE Then
                MsgBox "设置成功", , "success"
            Else
                msgErrorMessage CInt(CStr(txtBuff(7)))
            End If
    
        Else
            MsgBox "错误的响应格式", , "Error"
        End If
    Else
        MsgBox "定时器超时,请检查网关节点和PC串口是否连接完好"
        Timer1.Enabled = False
    End If
    
    
   
   
End If


End Sub

Private Sub Form_Load()

If (QueryorSetAN = 0) Then
    '查询
    butSetAN.Enabled = False
    butConvert10to16.Enabled = False
    textBeforeConverted.Enabled = False
    textA.Enabled = False
    textN.Enabled = False
    
Else
     '设置
    butQueryAN.Enabled = False
    butConvert16to10.Enabled = False
    textAfterConverted.Enabled = False
    
End If
End Sub

Private Sub Timer1_Timer()
'If Not isreceived Then
    
    timeout = True
'End If
End Sub

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?