Reading Data from the RS232

Problem with seperating data

Page 1 of 1

9 Replies - 7177 Views - Last Post: 02 July 2009 - 12:17 AM Rate Topic: -----

#1 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Reading Data from the RS232

Posted 01 July 2009 - 02:37 AM

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.


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






Is This A Good Question/Topic? 0
  • +

Replies To: Reading Data from the RS232

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Reading Data from the RS232

Posted 01 July 2009 - 03:48 AM

i dont see where you fill something in newdata befor mid$ it in this line

 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
		 Newdata = ""   'initialize to empty



then

			   InBuff = MSComm1.Input  'received 1 or more characters



then

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 :) 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

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: 01 July 2009 - 03:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Re: Reading Data from the RS232

Posted 01 July 2009 - 04:56 AM

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.


-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
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Reading Data from the RS232

Posted 01 July 2009 - 10:55 AM

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 ?
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Reading Data from the RS232

Posted 01 July 2009 - 11:05 AM

and use


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 :)

tell me if that is what you want :)

good luck

This post has been edited by NoBrain: 01 July 2009 - 11:07 AM

Was This Post Helpful? 1
  • +
  • -

#6 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Reading Data from the RS232

Posted 01 July 2009 - 11:16 AM

i still think you try to mid$() empty string :D
and dude :D evry 2 letter is a I or P in this string -P-: -71.3 dBm

This post has been edited by NoBrain: 01 July 2009 - 11:24 AM

Was This Post Helpful? 0
  • +
  • -

#7 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Re: Reading Data from the RS232

Posted 01 July 2009 - 11:35 AM

Thanks I will try this and get back. I have to look and see what Instr does exactly.
Was This Post Helpful? 0
  • +
  • -

#8 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Reading Data from the RS232

Posted 01 July 2009 - 11:42 AM

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 :)
return 0 or 1

This post has been edited by NoBrain: 01 July 2009 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#9 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Re: Reading Data from the RS232

Posted 01 July 2009 - 10:57 PM

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 !!!


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]
Was This Post Helpful? 0
  • +
  • -

#10 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Reading Data from the RS232

Posted 02 July 2009 - 12:17 AM

np not that much of help there :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1