This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to communicate between RS232 VB6.0 & 8051 keil C

I want to have some leds in P2.0~2.3 and show off the "ON" lamp in VB6.0 program though RS232 during the sensors are +5V output to P1.0~1.3 89S52 MCU. But it appears many errors and doesn't work in PC. Please advise me the comments for the following code. Thanks a lot!!

Parents
  • By the VB6.0 for the following codes:
    
    Private Sub Command5_Click()
    tex1.Text = "connectiong"
    MSComm1.Output = "0"
    delayms 100
    buf = MSComm1.Input
    If buf = "2" Then
       LOAD1.FillColor = RGB(0, 255, 0)
       LOAD2.FillColor = RGB(0, 255, 0)
       LOAD3.FillColor = RGB(0, 255, 0)
       LOAD4.FillColor = RGB(0, 255, 0)
    Else
       tex1.Text = "fail"
    End If
    End Sub
    Private Sub Command6_Click()
    On Error Resume Next
       msg = "are u close?"
       code = 65
       Title = "closed"
       return_value = MsgBox(msg, code, Title)
    If return_value = 1 Then
       Command5_Click
       MSComm1.PortOpen = False
       End
    End If
    End Sub
    Private Sub Command7_Click()
    
    On Error GoTo OUTPUTERR
    Dim i As Integer, iFile As String
    
    iFile = "d:\test.txt"
    Do While Dir(iFile) <> ""
        i = i + 1
        iFile = "d:\test-" & i & ".txt"
    Loop
    Open iFile For Output As #1
    Print #1, Text0(0).Text
    Directory.Text = iFile
    
    Close #1
    Exit Sub
    OUTPUTERR:
    MsgBox Err.Description, vbCritical, "file failed"
    
    End Sub
    
    Private Sub Command8_Click()
    Text0(0).Text = Empty
    End Sub
    Private Sub Form_Load()
    On Error Resume Next
    LOAD1.FillColor = RGB(0, 255, 0)
    LOAD2.FillColor = RGB(0, 255, 0)
    LOAD3.FillColor = RGB(0, 255, 0)
    LOAD4.FillColor = RGB(0, 255, 0)
    MSComm1.PortOpen = False
    MSComm1.CommPort = 1
    MSComm1.PortOpen = True
    If MSComm1.PortOpen = True Then
       MSComm1.PortOpen = False
       MSComm1.CommPort = 1
       MSComm1.Settings = "1200,n,8,1"
       MSComm1.PortOpen = True
       tex1.Text = "set COM1"
    Else
        MSComm1.PortOpen = False
        MSComm1.CommPort = 2
        MSComm1.PortOpen = True
      If MSComm1.PortOpen = True Then
        MSComm1.PortOpen = False
        MSComm1.CommPort = 2
        MSComm1.Settings = "1200,n,8,1"
        MSComm1.PortOpen = True
        tex1.Text = "set COM2"
      Else
        MSComm1.PortOpen = False
        tex1.Text = "no COM"
      End If
    End If
    End Sub
    
    Private Sub MSComm1_OnComm(Index As Integer)
    tex1.Text = "connecting"
    MSComm1.Output = "P1.0"
    delayms 100
    buf = MSComm1.Input
    If buf = "0" Then
       LOAD1.FillColor = RGB(0, 255, 0)
    
    ElseIf buf = "1" Then
       LOAD1.FillColor = RGB(255, 0, 0)
       Text0(0).Text = Text0(0).Text + "GasSensor-" + Format(Now, "DD.MMM.YYYY.HH:MM:SS") + vbNewLine
    Else
       tex1.Text = "fail"
    End If
    End Sub
    

Reply
  • By the VB6.0 for the following codes:
    
    Private Sub Command5_Click()
    tex1.Text = "connectiong"
    MSComm1.Output = "0"
    delayms 100
    buf = MSComm1.Input
    If buf = "2" Then
       LOAD1.FillColor = RGB(0, 255, 0)
       LOAD2.FillColor = RGB(0, 255, 0)
       LOAD3.FillColor = RGB(0, 255, 0)
       LOAD4.FillColor = RGB(0, 255, 0)
    Else
       tex1.Text = "fail"
    End If
    End Sub
    Private Sub Command6_Click()
    On Error Resume Next
       msg = "are u close?"
       code = 65
       Title = "closed"
       return_value = MsgBox(msg, code, Title)
    If return_value = 1 Then
       Command5_Click
       MSComm1.PortOpen = False
       End
    End If
    End Sub
    Private Sub Command7_Click()
    
    On Error GoTo OUTPUTERR
    Dim i As Integer, iFile As String
    
    iFile = "d:\test.txt"
    Do While Dir(iFile) <> ""
        i = i + 1
        iFile = "d:\test-" & i & ".txt"
    Loop
    Open iFile For Output As #1
    Print #1, Text0(0).Text
    Directory.Text = iFile
    
    Close #1
    Exit Sub
    OUTPUTERR:
    MsgBox Err.Description, vbCritical, "file failed"
    
    End Sub
    
    Private Sub Command8_Click()
    Text0(0).Text = Empty
    End Sub
    Private Sub Form_Load()
    On Error Resume Next
    LOAD1.FillColor = RGB(0, 255, 0)
    LOAD2.FillColor = RGB(0, 255, 0)
    LOAD3.FillColor = RGB(0, 255, 0)
    LOAD4.FillColor = RGB(0, 255, 0)
    MSComm1.PortOpen = False
    MSComm1.CommPort = 1
    MSComm1.PortOpen = True
    If MSComm1.PortOpen = True Then
       MSComm1.PortOpen = False
       MSComm1.CommPort = 1
       MSComm1.Settings = "1200,n,8,1"
       MSComm1.PortOpen = True
       tex1.Text = "set COM1"
    Else
        MSComm1.PortOpen = False
        MSComm1.CommPort = 2
        MSComm1.PortOpen = True
      If MSComm1.PortOpen = True Then
        MSComm1.PortOpen = False
        MSComm1.CommPort = 2
        MSComm1.Settings = "1200,n,8,1"
        MSComm1.PortOpen = True
        tex1.Text = "set COM2"
      Else
        MSComm1.PortOpen = False
        tex1.Text = "no COM"
      End If
    End If
    End Sub
    
    Private Sub MSComm1_OnComm(Index As Integer)
    tex1.Text = "connecting"
    MSComm1.Output = "P1.0"
    delayms 100
    buf = MSComm1.Input
    If buf = "0" Then
       LOAD1.FillColor = RGB(0, 255, 0)
    
    ElseIf buf = "1" Then
       LOAD1.FillColor = RGB(255, 0, 0)
       Text0(0).Text = Text0(0).Text + "GasSensor-" + Format(Now, "DD.MMM.YYYY.HH:MM:SS") + vbNewLine
    Else
       tex1.Text = "fail"
    End If
    End Sub
    

Children
No data