Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai friends!!i have a problem in coding about how to send and receive data through rs232. i am trying a loop back process as if i send data to rs232 from my laptop again i should receive the data through the port to my laptop. I am using visual basic 6.0 for programming.can u pls help me on coding how to write.I have tried a little,but it's not working.

Your help means alot

Thank You.

What I have tried:

VB
Dim Newdata As String

Private Sub Command35_Click()
    Text21.Text = MSComm1.Input
End Sub

Private Sub Command36_Click()
    Text20.Text = Val(MSComm1.Input)
End Sub

Private Sub Form_Load()
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.CommPort = 8
    MSComm1.InputLen = 1
    MSComm1.PortOpen = True
    MSComm1.RThreshold = 1

    Newdata = ""
End Sub

Private Sub Form_Unload(Cancel As Integer)

    MSComm1.PortOpen = False

End Sub

Private Sub MSComm1_OnComm()
    Dim Dat As String
    Dim I As Integer    'each incoming char
    Dim receive As String   'each received char
    Dim theinfo As String
    Select Case MSComm1.CommEvent
       Case comEventRxOver
       Case comEventTxFull
       Case comEvReceive
       
       Data = MSComm1.Input
       For I = 1 To Len(Dat)
        receive = Mid$(Dat, I, 1)
        If Asc(receive) = 13 Then
            theinfo = Mid$(Newdata, 2, 1)
    If theinfo = "I" Then
        InformationDisplay.SelLength = 0
        InformationDisplay.SelStart = Len(InformationDisplay.Text)
        InformationDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
        InformationDisplay.SelLength = 0
        
    ElseIf theinfo = "P" Then
        OutputDisplay.SelLength = 0
        OutputDisplay.SelStart = Len(OutputDisplay.Text)
        OutputDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
        OutputDisplay.SelLength = 0
    End If
    
    If Asc(theinfo) = 73 Then
        InformationDisplay.SelStart = Len(InformationDisplay.Text)
        InformationDisplay.SelText = Newdata + vbCr + vbLf
        Newdata = "" 'clear NewData so it can assemble the next packet
                    
            Data.SelLength = 0
            Data.SelStart = 2
            Data.SelText = vbCrLf
            Data.SelLength = 0
            
    ElseIf Asc(theChar) <> 10 Then  'ignore linefeeds

            Newdata = Newdata + theChar 'received a character -- append it to NewData

            Data.SelLength = 0
            Data.SelStart = Len(Data.Text)
            Data.SelText = theChar
            Data.SelLength = 0

    End If
    End If
    Next I
    
       Case comEvSend
       Case comEvEOF
    End Select
End Sub
Posted
Updated 7-Sep-17 22:38pm
v2

1 solution

VB
Data = MSComm1.Input
For I = 1 To Len(Dat)

Data or Dat?

You know, in order to make loopback working you have to physically connect RX with TX.
 
Share this answer
 
v2
Comments
Member 13396059 8-Sep-17 5:00am    
physically connecting means?? can u pls explain
CPallini 8-Sep-17 5:37am    
Well, loopback means you are going to receive the character you have just sent. This is usually achieved connecting the TX wire directly to the RX one.
See, for instance:
http://www.ni.com/tutorial/3450/en/
Member 13396059 11-Sep-17 2:23am    
should we write code to tx and rx or any inbuild events are present to call?

Thank You

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900