VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB Expert!

Join 300,355 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,945 people online right now. Registration is fast and FREE... Join Now!




Reading Data from the RS232

 

Reading Data from the RS232, Problem with seperating data

oe8pck

1 Jul, 2009 - 01:37 AM
Post #1

New D.I.C Head
*

Joined: 27 Jun, 2009
Posts: 17


My Contributions
Hi, Since I last posted this problem I have managed to narrow it down.
This program collects information from the RS232 and places it in a Textbox. Now each data string that is placed in the box has a start which is as follows:
-I-: Retrieving Flash Memory Settings
-I-: etc......
When this data has been received in on one textbox then I want the next data to go into another box. The following data looks like this:
-P-: -71.4 dBm
-P-: etc.................
I have managed to get the data running into two Textboxes but when I try to split the data looking at the string for the (I) or the (P) then I get a failure message. I am attaching the program ina running condition with the split code blanked out with ' .
I just hope someone can help, I have been trying to get this solved for a couple of weeks now.
PLEASE HELP or suggest how I could rewrite the program.

CODE


Option Explicit

Dim Newdata As String   'each imcoming packet is assembled here

Private Sub Form_Load()
         Form1.Caption = "OE8PCK Digital Wattmeter"
         With MSComm1
            .CommPort = 4
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .PortOpen = True
            ' Leave all other settings as default values.
         End With
        
         OutputDisplay.Text = "Infobox"
         InformationDisplay.Text = "Databox"
         Help.Text = "Helpbox"
         Data.Text = "Databox"
         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
         ' Handle each event or error by placing
         ' code below each case statement.

         ' This template is found in the Example
         ' section of the OnComm event Help topic
         ' in VB Help.

         ' 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)
                
                '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
                    'theInfo = ""
     'End If
                
                
                        
    'If Asc(theInfo) = 80 Then 'Look for (P)
                        
                  OutputDisplay.SelStart = Len(OutputDisplay.Text)
                  OutputDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
                                                    
                    
                  
    'End If
                
                
                
                
                
                  
                Newdata = "" 'clear NewData so it can assemble the next packet
                
                        
               ElseIf Asc(theChar) = 10 Then   'received a linefeed - ignore it
                        
                Else
                        
                        Newdata = Newdata + theChar 'received a character -- append it to NewData
                        'Print (NewData)
                        
                        
                      Data.SelStart = Len(Data.Text)
                      Data.SelText = Newdata + vbCr + vbLf
                      
                      
                End If
                              
                      Help.SelStart = Len(Help.Text)
                      Help.SelText = theInfo + vbCr + vbLf
                  
            'theInfo = ""
            
            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









User is offlineProfile CardPM
+Quote Post


NoBrain

RE: Reading Data From The RS232

1 Jul, 2009 - 02:48 AM
Post #2

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
i dont see where you fill something in newdata befor mid$ it in this line

CODE

theInfo = mID$(Newdata, 2, 1)


where you fill the mscominput in inbuff then you look for each char in the string inbuff then you look if there is the special chars you look and then you mid$ newData when there is nothing there as i see from code you send

look

you do this
CODE

         Newdata = ""   'initialize to empty


then

CODE

               InBuff = MSComm1.Input  'received 1 or more characters


then

CODE

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)


now smile.gif probaly you want to load the 3 leather in the string newData if that is so you got mid$ functio wrong

it should be like this

CODE

Newdata=mid$(theInfo,2,1)


still i dont think you want that where you fill newdata with something

what is the error btw

This post has been edited by NoBrain: 1 Jul, 2009 - 02:41 AM
User is offlineProfile CardPM
+Quote Post

oe8pck

RE: Reading Data From The RS232

1 Jul, 2009 - 03:56 AM
Post #3

New D.I.C Head
*

Joined: 27 Jun, 2009
Posts: 17


My Contributions
Hi thanks for the answer. This program runs ok as it is but does not do that what I want. To try it out you would need a data stream via the rs232. If you can do this then let the following data be streamed.

CODE


-I-:  Retrieving Flash Memory Settings
-I-:  PIC  Power Meter
-I-:  Kanga US  V2.0.0
-I-:  Calibration Settings:
-I-:    Voltage Display:      OFF
-I-:    VSWR Display:         OFF
-I-:    Power Curve Point A:  -70.0 dBm ::  0.51 V
-I-:    Power Curve Point B:  +0.0 dBm ::  3.15 V
-I-:    Power Curve Point C:  +7.0 dBm ::  3.56 V
-I-:    Differential Mode Calibration:  -10.0 dB ::  2.22 V
-I-:    Tap Offset:  +40.0 dB
-P-:   -71.3 dBm
-P-:   -71.3 dBm
-P-:   -71.3 dBm
-P-:   -71.6 dBm
-P-:   -71.3 dBm
-P-:   -71.4 dBm
-P-:   -71.4 dBm
-P-:   -71.1 dBm




I do realize this is not code but I presume that the data will not be corrupted. I want the data starting with -I-: in one textbox and all the other data starting with -P-: in another. When I try to run the code the program stops at the line 'If Asc(theInfo) = 73 Then 'Look for (I) as you can see I have disabled this line at the moment if you run the code with the lines enabled you will see the error.

If you want to give me an email address I could send you the files etc.

I do hope you can help.

Regards, Paul OE8PCK

User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Reading Data From The RS232

1 Jul, 2009 - 09:55 AM
Post #4

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
mostly becose you got to get a char by char and then look for asc. for I or P

but you can do instr(info,"p")


try that


or you get the diferent info with that kind of strings ?
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Reading Data From The RS232

1 Jul, 2009 - 10:05 AM
Post #5

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
and use

CODE


if instr(1, info, "-I-") then
textbox1.text=textbox1.text & info & vbCrLf
else
if instr(1, info,"-P-") then
textbox2.text=textbox2.text & info & vbCrLf
else
msgbox "Error wrong data or something ", vbcritical
end if
end if



i gues -P- and -I- will be rear to be found in some of the text only on the start

else you can use mid$() and get in string only the first 3 chars in string and the look if -I- or -P- is there smile.gif

tell me if that is what you want smile.gif

good luck

This post has been edited by NoBrain: 1 Jul, 2009 - 10:07 AM
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Reading Data From The RS232

1 Jul, 2009 - 10:16 AM
Post #6

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
i still think you try to mid$() empty string biggrin.gif
and dude biggrin.gif evry 2 letter is a I or P in this string -P-: -71.3 dBm

This post has been edited by NoBrain: 1 Jul, 2009 - 10:24 AM
User is offlineProfile CardPM
+Quote Post

oe8pck

RE: Reading Data From The RS232

1 Jul, 2009 - 10:35 AM
Post #7

New D.I.C Head
*

Joined: 27 Jun, 2009
Posts: 17


My Contributions
Thanks I will try this and get back. I have to look and see what Instr does exactly.

User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Reading Data From The RS232

1 Jul, 2009 - 10:42 AM
Post #8

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
Function InStr([Start], [String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])
Member of VBA.Strings
Returns the position of the first occurrence of one string within another

with mid$() second char in the string you can look only for i or p smile.gif
return 0 or 1

This post has been edited by NoBrain: 1 Jul, 2009 - 10:43 AM
User is offlineProfile CardPM
+Quote Post

oe8pck

RE: Reading Data From The RS232

1 Jul, 2009 - 09:57 PM
Post #9

New D.I.C Head
*

Joined: 27 Jun, 2009
Posts: 17


My Contributions
It Works!!! Your comments gave me the inspiration, it's so simple when you know how. I just have to remove the help Textscreens.
Many thanks !!!

CODE


Option Explicit

Dim Newdata As String   'each imcoming packet is assembled here

Private Sub Form_Load()
         Form1.Caption = "OE8PCK Digital Wattmeter"
         With MSComm1
            .CommPort = 4
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .PortOpen = True
            ' Leave all other settings as default values.
         End With
        
         OutputDisplay.Text = "Infobox"
         InformationDisplay.Text = "Databox"
         Help.Text = "Helpbox"
         Data.Text = "Databox"
         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
         ' Handle each event or error by placing
         ' code below each case statement.

         ' This template is found in the Example
         ' section of the OnComm event Help topic
         ' in VB Help.

         ' 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 second 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
                    
                        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





[code]


User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Reading Data From The RS232

1 Jul, 2009 - 11:17 PM
Post #10

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
np not that much of help there smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 07:29PM

Live VB Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month