Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends!!!

I want to write a program using VB6.0 with RS232 and I have shorted 2 and 3 pins (i.e., receive and transmit) of the female connector and i have connected that rs232 through USB to my laptop and now using vb6 programming i want to send data(as String) to transmit pin of rs232 and want to receive the data(as String) back to my laptop through receive pin..

Please help me in coding..I'm to new to VB and started learning..

Pls help me..Thank alot.

Thank you.

What I have tried:

VB
Option Explicit

Dim Newdata As String   'each imcoming packet is assembled here

Private Sub Command35_Click()
    Text21.Text = Text21.Text & ""
    Text20.Text = Text21.Text & ""
End Sub

Private Sub Form_Load()
         
         Form1.Caption = ""

         MSComm1.CommPort = 8

            '.Handshaking = 2 - comRTS

         MSComm1.RThreshold = 1

         MSComm1.RTSEnable = True

         MSComm1.Settings = "9600,n,8,1"

         MSComm1.SThreshold = 1

         MSComm1.PortOpen = True
    
         Newdata = ""   'initialize to empty

      End Sub

      Private Sub Form_Unload(Cancel As Integer)

         MSComm1.PortOpen = False

      End Sub
      
      Private Sub MSComm1_OnComm()

         Dim InBuff As String

         Dim I As Integer      'used to inspect each incoming character

         Dim theChar As String  'each received character

         Dim theInfo As String

          

         Select Case MSComm1.CommEvent
 

         ' Errors

            Case comEventBreak   ' A Break was received.

            Case comEventCDTO   ' CD (RLSD) Timeout.

            Case comEventCTSTO   ' CTS Timeout.

            Case comEventDSRTO   ' DSR Timeout.

            Case comEventFrame   ' Framing Error.

            Case comEventOverrun ' Data Lost.

            Case comEventRxOver  ' Receive buffer overflow.

            Case comEventRxParity   ' Parity Error.

            Case comEventTxFull  ' Transmit buffer full.

            Case comEventDCB     ' Unexpected error retrieving DCB]
 

         ' Events

            Case comEvCD   ' Change in the CD line.

            Case comEvCTS  ' Change in the CTS line.

            Case comEvDSR  ' Change in the DSR line.

            Case comEvRing ' Change in the Ring Indicator.

            Case comEvReceive ' Received RThreshold # of chars.
             

               InBuff = MSComm1.Input  'received 1 or more characters

                

            For I = 1 To Len(InBuff) 'examine each received character in sequence

             

                theChar = Mid$(InBuff, I, 1) 'extract the next character

                    

                If Asc(theChar) = 13 Then 'Look for CR

                     

                theInfo = Mid$(Newdata, 2, 1) 'Loads the third letter in the String "Newdata" in "theInfo" is a (I) or (P)

                 
                 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 'Look for (I)
                     

                    InformationDisplay.SelStart = Len(InformationDisplay.Text)

                    InformationDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay

                   

                    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

            Next I


            Case comEvSend ' There are SThreshold number of

                           ' characters in the transmit buffer.

            Case comEvEOF  ' An EOF character was found in the

                           ' input stream.

         End Select

      End Sub
Posted
Updated 12-Sep-17 20:44pm
v2
Comments
OriginalGriff 13-Sep-17 2:37am    
And?
What does it do that you didn't expect, or not do that you did?
Member 13396059 13-Sep-17 3:52am    
no..the code was jus copying and pasting the data from one text box to another..if i remove the rs232 female connector also the process is running it shouldn't do so na..
Member 13396059 13-Sep-17 3:40am    
no..the code was jus copying and pasting the data from one text box to another..if i remove the rs232 female connector also the process is running it shouldn't do so na..
Graeme_Grant 13-Sep-17 3:43am    
You should click "reply" button if you want us to know that you have responded.
Member 13396059 13-Sep-17 3:45am    
ohh sry i dnt knw..i'll do so frm now

1 solution

It's been quite a while since VB6 & RS232c comms however I do remember that RS232 Loopback cable wiring is a little bit more tricky... Not only Rx & Tx, but also CTS (clear to send) & RTS (request to send), plus more to cater for the hardware handshaking... Have a look at this link: Pinout For Serial Ports - National Instruments[^]
 
Share this answer
 

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