none
Codice per microcontrollore in VB2010 RRS feed

  • Domanda

  • Ho un problemino. Devo far funzionare il codice di un programma di un microcontrollore scritto in VB6  in VB2010.  Non conoscendo VB6 ho difficoltà ad adattare il codice al 2010 soprattutto per la connessione con le porte seriali.

    Questo è il codice in VB6 potete darmi qualche aiuto?  Grazie

    Dim Connected As Integer
    
    Private Sub Check1_Click()
    Timer1.Enabled = Check1.Value
    End Sub
    
    Private Sub Close_Click()
    Call Form_Unload(0)
    End
    End Sub
    
    Private Sub Connect_Click()
    ' Make sure it's not already open
    If Connected = 1 Then MSComm1.PortOpen = False
    ' Open the device
    MSComm1.CommPort = Val(PortNumber)
    On Error GoTo notopen
    MSComm1.PortOpen = True
    On Error GoTo 0
    Connected = 1
    StatusLabel = "Connected"
    Call ResetIO24_Click
    Exit Sub
    
    notopen:
    StatusLabel = "Could Not Open That Port"
    End Sub
    
    
    Private Sub Form_Load()
    Connected = 0
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    If Connected = 1 Then MSComm1.PortOpen = False
    End Sub
    
    Private Sub PollNow_Click()
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    MSComm1.Output = "abc"
    InputBuffer$ = ""
    ' Setup a half second timeout for the USBIO24 to respond (Ttakes less than 1ms in reality)
    T = Timer
    While Timer < T + 0.4 And MSComm1.InBufferCount <> 3
    Wend
    If MSComm1.InBufferCount <> 3 Then
        StatusLabel = "Read Timeout"
        Exit Sub
        End If
    Dim TempBuffer As Variant
    Dim ByteBuffer() As Byte
    TempBuffer = MSComm1.Input
    ByteBuffer = TempBuffer
    PORTAVAL = ByteBuffer(0)
    PORTBVAL = ByteBuffer(1)
    PORTCVAL = ByteBuffer(2)
    PortARHex = Hex$(PORTAVAL)
    PortBRHex = Hex$(PORTBVAL)
    PortCRHex = Hex$(PORTCVAL)
    PortARDec = Str$(PORTAVAL)
    PortBRDec = Str$(PORTBVAL)
    PortCRDec = Str$(PORTCVAL)
    End Sub
    
    Private Sub PortAIO_Click(Index As Integer)
    If PortAIO(Index).Caption = "1" Then
        PortAIO(Index).Caption = "0"
    Else
        PortAIO(Index).Caption = "1"
    End If
    IOVal = 0
    For n = 0 To 7
    If PortAIO(n).Caption = "1" Then IOVal = IOVal + 2 ^ n
    Next
    PortAWHex = Hex$(IOVal)
    PortAWDec = Str$(IOVal)
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    
    ' Write to Port A Data Register
    
    MSComm1.Output = "A" + Chr$(IOVal)
    
    
    End Sub
    
    Private Sub PortAIOBit_Click(Index As Integer)
    
    If PortAIOBit(Index).Caption = "I" Then
        PortAIOBit(Index).Caption = "O"
        PortAIO(Index).FontStrikethru = False
    Else
        PortAIOBit(Index).Caption = "I"
        PortAIO(Index).FontStrikethru = True
    End If
    IOVal = 0
    For n = 0 To 7
    If PortAIOBit(n).Caption = "I" Then IOVal = IOVal + 2 ^ n
    Next
    PortAIOHex = Hex$(IOVal)
    PortAIODec = Str$(IOVal)
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    
    ' Write to Port A Direction Register
    
    MSComm1.Output = "!A" + Chr$(IOVal)
    
    End Sub
    
    Private Sub PortAIODec_Change()
    EPos = PortAIODec.SelStart
    
    If Val(Right$(PortAIODec.Text, 3)) > 255 Then
        IOVal = Val(Right$(PortAIODec.Text, 2))
        Else
        IOVal = Val(Right$(PortAIODec.Text, 3))
        End If
        
    
    PortAIOHex = Hex$(IOVal)
    PortAIODec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortAIOBit(n).Caption = "O"
        PortAIO(n).FontStrikethru = False
    Else
        PortAIOBit(n).Caption = "I"
        PortAIO(n).FontStrikethru = True
    End If
    Next
    If IOVal = 0 Then EPos = 2
    PortAIODec.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port A Direction Register
    
    MSComm1.Output = "!A" + Chr$(IOVal)
    End Sub
    
    Private Sub PortAIOHex_Change()
    EPos = PortAIOHex.SelStart
    IOVal = Val("&H" + Right$(PortAIOHex.Text, 2)) And 255
    PortAIOHex = Hex$(IOVal)
    PortAIODec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortAIOBit(n).Caption = "O"
        PortAIO(n).FontStrikethru = False
    Else
        PortAIOBit(n).Caption = "I"
        PortAIO(n).FontStrikethru = True
    End If
    Next
    PortAIOHex.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port A Direction Register
    
    MSComm1.Output = "!A" + Chr$(IOVal)
    
    
    End Sub
    
    Private Sub PortAWDec_Change()
    
    EPos = PortAWDec.SelStart
    If Val(Right$(PortAWDec.Text, 3)) > 255 Then
        IOVal = Val(Right$(PortAWDec.Text, 2))
        Else
        IOVal = Val(Right$(PortAWDec.Text, 3))
        End If
    PortAWHex = Hex$(IOVal)
    PortAWDec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortAIO(n).Caption = "0"
    Else
        PortAIO(n).Caption = "1"
    End If
    Next
    If IOVal = 0 Then EPos = 2
    PortAWDec.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port A Data Register
    
    MSComm1.Output = "A" + Chr$(IOVal)
    
    
    End Sub
    
    Private Sub PortAWHex_Change()
    
    EPos = PortAWHex.SelStart
    IOVal = Val("&H" + Right$(PortAWHex.Text, 2)) And 255
    PortAWHex = Hex$(IOVal)
    PortAWDec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortAIO(n).Caption = "0"
    Else
        PortAIO(n).Caption = "1"
    End If
    Next
    PortAWHex.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port A Data Register
    
    MSComm1.Output = "A" + Chr$(IOVal)
    
    End Sub
    
    Private Sub PortBIO_Click(Index As Integer)
    
    If PortBIO(Index).Caption = "1" Then
        PortBIO(Index).Caption = "0"
    Else
        PortBIO(Index).Caption = "1"
    End If
    IOVal = 0
    For n = 0 To 7
    If PortBIO(n).Caption = "1" Then IOVal = IOVal + 2 ^ n
    Next
    PortBWHex = Hex$(IOVal)
    PortBWDec = Str$(IOVal)
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    
    ' Write to Port B Data Register
    
    MSComm1.Output = "B" + Chr$(IOVal)
    
    End Sub
    
    Private Sub PortBIOBit_Click(Index As Integer)
    
    If PortBIOBit(Index).Caption = "I" Then
        PortBIOBit(Index).Caption = "O"
        PortBIO(Index).FontStrikethru = False
    Else
        PortBIOBit(Index).Caption = "I"
        PortBIO(Index).FontStrikethru = True
    End If
    IOVal = 0
    For n = 0 To 7
    If PortBIOBit(n).Caption = "I" Then IOVal = IOVal + 2 ^ n
    Next
    PortBIOHex = Hex$(IOVal)
    PortBIODec = Str$(IOVal)
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    
    ' Write to Port B Direction Register
    
    MSComm1.Output = "!B" + Chr$(IOVal)
    
    
    
    End Sub
    
    Private Sub PortBIODec_Change()
    
    EPos = PortBIODec.SelStart
    
    If Val(Right$(PortBIODec.Text, 3)) > 255 Then
        IOVal = Val(Right$(PortBIODec.Text, 2))
        Else
        IOVal = Val(Right$(PortBIODec.Text, 3))
        End If
    
    
    PortBIOHex = Hex$(IOVal)
    PortBIODec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortBIOBit(n).Caption = "O"
        PortBIO(n).FontStrikethru = False
    Else
        PortBIOBit(n).Caption = "I"
        PortBIO(n).FontStrikethru = True
    End If
    Next
    If IOVal = 0 Then EPos = 2
    PortBIODec.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port B Direction Register
    
    MSComm1.Output = "!B" + Chr$(IOVal)
    
    End Sub
    
    Private Sub PortBIOHex_Change()
    EPos = PortBIOHex.SelStart
    IOVal = Val("&H" + Right$(PortBIOHex.Text, 2)) And 255
    PortBIOHex = Hex$(IOVal)
    PortBIODec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortBIOBit(n).Caption = "O"
        PortBIO(n).FontStrikethru = False
    Else
        PortBIOBit(n).Caption = "I"
        PortBIO(n).FontStrikethru = True
    End If
    Next
    PortBIOHex.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port B Direction Register
    
    MSComm1.Output = "!B" + Chr$(IOVal)
    
    End Sub
    
    Private Sub PortBWDec_Change()
    
    EPos = PortBWDec.SelStart
    If Val(Right$(PortBWDec.Text, 3)) > 255 Then
        IOVal = Val(Right$(PortBWDec.Text, 2))
        Else
        IOVal = Val(Right$(PortBWDec.Text, 3))
        End If
    PortBWHex = Hex$(IOVal)
    PortBWDec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortBIO(n).Caption = "0"
    Else
        PortBIO(n).Caption = "1"
    End If
    Next
    If IOVal = 0 Then EPos = 2
    PortBWDec.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port B Data Register
    
    MSComm1.Output = "B" + Chr$(IOVal)
    End Sub
    
    Private Sub PortBWHex_Change()
    
    EPos = PortBWHex.SelStart
    IOVal = Val("&H" + Right$(PortBWHex.Text, 2)) And 255
    PortBWHex = Hex$(IOVal)
    PortBWDec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortBIO(n).Caption = "0"
    Else
        PortBIO(n).Caption = "1"
    End If
    Next
    PortBWHex.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port B Data Register
    
    MSComm1.Output = "B" + Chr$(IOVal)
    End Sub
    
    Private Sub PortCIO_Click(Index As Integer)
    
    
    
    If PortCIO(Index).Caption = "1" Then
        PortCIO(Index).Caption = "0"
    Else
        PortCIO(Index).Caption = "1"
    End If
    IOVal = 0
    For n = 0 To 7
    If PortCIO(n).Caption = "1" Then IOVal = IOVal + 2 ^ n
    Next
    PortCWHex = Hex$(IOVal)
    PortCWDec = Str$(IOVal)
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    
    ' Write to Port C Data Register
    
    MSComm1.Output = "C" + Chr$(IOVal)
    
    End Sub
    
    Private Sub PortCIOBit_Click(Index As Integer)
    
    
    If PortCIOBit(Index).Caption = "I" Then
        PortCIOBit(Index).Caption = "O"
        PortCIO(Index).FontStrikethru = False
    Else
        PortCIOBit(Index).Caption = "I"
        PortCIO(Index).FontStrikethru = True
    End If
    IOVal = 0
    For n = 0 To 7
    If PortCIOBit(n).Caption = "I" Then IOVal = IOVal + 2 ^ n
    Next
    PortCIOHex = Hex$(IOVal)
    PortCIODec = Str$(IOVal)
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    
    ' Write to Port C Direction Register
    
    MSComm1.Output = "!C" + Chr$(IOVal)
    
    
    End Sub
    
    Private Sub PortCIODec_Change()
    
    EPos = PortCIODec.SelStart
    
    
    If Val(Right$(PortCIODec.Text, 3)) > 255 Then
        IOVal = Val(Right$(PortCIODec.Text, 2))
        Else
        IOVal = Val(Right$(PortCIODec.Text, 3))
        End If
    
    
    PortCIOHex = Hex$(IOVal)
    PortCIODec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortCIOBit(n).Caption = "O"
        PortCIO(n).FontStrikethru = False
    Else
        PortCIOBit(n).Caption = "I"
        PortCIO(n).FontStrikethru = True
    End If
    Next
    PortCIODec.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port C Direction Register
    
    MSComm1.Output = "!C" + Chr$(IOVal)
    
    End Sub
    
    Private Sub PortCIOHex_Change()
    
    EPos = PortCIOHex.SelStart
    IOVal = Val("&H" + Right$(PortCIOHex.Text, 2)) And 255
    PortCIOHex = Hex$(IOVal)
    PortCIODec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortCIOBit(n).Caption = "O"
        PortCIO(n).FontStrikethru = False
    Else
        PortCIOBit(n).Caption = "I"
        PortCIO(n).FontStrikethru = True
    End If
    Next
    PortCIOHex.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port C Direction Register
    
    MSComm1.Output = "!C" + Chr$(IOVal)
    End Sub
    
    Private Sub PortCWDec_Change()
    
    EPos = PortCWDec.SelStart
    If Val(Right$(PortCWDec.Text, 3)) > 255 Then
        IOVal = Val(Right$(PortCWDec.Text, 2))
        Else
        IOVal = Val(Right$(PortCWDec.Text, 3))
        End If
    PortCWHex = Hex$(IOVal)
    PortCWDec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortCIO(n).Caption = "0"
    Else
        PortCIO(n).Caption = "1"
    End If
    Next
    If IOVal = 0 Then EPos = 2
    PortCWDec.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port C Data Register
    
    MSComm1.Output = "C" + Chr$(IOVal)
    
    End Sub
    
    Private Sub PortCWHex_Change()
    
    EPos = PortCWHex.SelStart
    IOVal = Val("&H" + Right$(PortCWHex.Text, 2)) And 255
    PortCWHex = Hex$(IOVal)
    PortCWDec = Str$(IOVal)
    For n = 0 To 7
    If (IOVal And 2 ^ n) = 0 Then
        PortCIO(n).Caption = "0"
    Else
        PortCIO(n).Caption = "1"
    End If
    Next
    PortCWHex.SelStart = EPos
    
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Write to Port C Data Register
    
    MSComm1.Output = "C" + Chr$(IOVal)
    
    End Sub
    
    Private Sub ResetIO24_Click()
    
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    
    MSComm1.Output = "!A" + Chr$(255) 'PORT A All Inputs
    MSComm1.Output = "!B" + Chr$(255) 'PORT B All Inputs
    MSComm1.Output = "!C" + Chr$(255) 'PORT C All Inputs
    MSComm1.Output = "A" + Chr$(0) 'PORT A All Low
    MSComm1.Output = "B" + Chr$(0) 'PORT B All Low
    MSComm1.Output = "C" + Chr$(0) 'PORT C All Low
    
    
    PortAWHex = Hex$(0)
    PortBWHex = Hex$(0)
    PortCWHex = Hex$(0)
    PortAIOHex = Hex$(255)
    PortBIOHex = Hex$(255)
    PortCIOHex = Hex$(255)
    End Sub
    
    Private Sub Timer1_Timer()
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    ' Read all three ports
    If Connected = 0 Then
        StatusLabel = "Please Click Connect First"
        Exit Sub
        End If
    
    MSComm1.Output = "abc"
    InputBuffer$ = ""
    ' Setup a half second timeout for the USBIO24 to respond (Ttakes less than 1ms in reality)
    T = Timer
    While Timer < T + 0.4 And MSComm1.InBufferCount <> 3
    Wend
    If MSComm1.InBufferCount <> 3 Then
        StatusLabel = "Read Timeout"
        Exit Sub
        End If
    Dim TempBuffer As Variant
    Dim ByteBuffer() As Byte
    TempBuffer = MSComm1.Input
    ByteBuffer = TempBuffer
    PORTAVAL = ByteBuffer(0)
    PORTBVAL = ByteBuffer(1)
    PORTCVAL = ByteBuffer(2)
    PortARHex = Hex$(PORTAVAL)
    PortBRHex = Hex$(PORTBVAL)
    PortCRHex = Hex$(PORTCVAL)
    PortARDec = Str$(PORTAVAL)
    PortBRDec = Str$(PORTBVAL)
    PortCRDec = Str$(PORTCVAL)
    
    End Sub
    

    • Spostato Irina Turcu venerdì 9 maggio 2014 15:31 Spostato il thread sul forum dedicato alla migrazione da VB6 a VB.NET
    lunedì 5 maggio 2014 15:12

Tutte le risposte