7 Replies - 3750 Views - Last Post: 13 October 2009 - 09:43 AM Rate Topic: -----

#1 adams.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-October 09

Reading Between two characters

Posted 11 October 2009 - 06:32 PM

Hello

I've been searching the net for the last month, and could not figure this out.

Trying to read and display the text between two re-ocurring characters with a Serial connected device. The beginning of string starts off with "+8" and ends up with "0+". The string displays correctly between the characters, however I do not wish to see these characters.
Here is my code that I used from a serial comm chat.

Thanks for your help.

P.S. this displays on two lines, the characters that I'm asking about are on the second line.
Private Sub DataReceived(ByVal sender _
	As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
	Handles serialPort.DataReceived
		txtDataRecived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
		Replace(Chr(43) & Chr(56), Chr(0), 1)

	End Sub

	Public Delegate Sub myDelegate()
	Public Sub updateTextBox()
		Replace(Chr(43) & Chr(56), Chr(32), 100)
		txtDataRecived.ScrollToCaret()
		With txtDataRecived
			.WordWrap = True
			'.Font = New Font("Arial", 12.0!, FontStyle.Bold)
			'.SelectionColor = Color.Red
			.AppendText(serialPort.ReadExisting)
			Replace(Chr(43) & Chr(56), Chr(0), 1)
			'.ScrollToCaret()
		End With
	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Reading Between two characters

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Reading Between two characters

Posted 12 October 2009 - 07:06 PM

What is your question? What is wrong - error message, wrong results, no results? At first look, I am puzzed by:
Replace(Chr(43) & Chr(56), Chr(32), 100) which appears to replace with superscript '(2)'
Also, none of the Replace statements provide the string to search in, just the characters to find and replace.
Also, with Chr(0) you will get leading spaces. If you don't want them, try the empty string (pair of quote marks).
And why use the Chr codes and not the literal characters?

This post has been edited by June7: 12 October 2009 - 07:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 adams.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-October 09

Re: Reading Between two characters

Posted 13 October 2009 - 01:34 AM

View PostJune7, on 12 Oct, 2009 - 06:06 PM, said:

What is your question? What is wrong - error message, wrong results, no results? At first look, I am puzzed by:
Replace(Chr(43) & Chr(56), Chr(32), 100) which appears to replace with superscript '(2)'
Also, none of the Replace statements provide the string to search in, just the characters to find and replace.
Also, with Chr(0) you will get leading spaces. If you don't want them, try the empty string (pair of quote marks).
And why use the Chr codes and not the literal characters?

Was This Post Helpful? 0
  • +
  • -

#4 adams.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-October 09

Re: Reading Between two characters

Posted 13 October 2009 - 01:42 AM

Helllo

Thank you for your reply, and sorry if I was not clear enough.

My program interfaces with a machine via Serial port. It works without any errors, but when the machine replies I get two lines of display. The display is also correct except for the second line. The beginning of the second line starts with "+8" characters and ends with "0+". All I wanted is to prevent these characters from showing up. Here is an example of the reply:

Total Output
+8Product 80+0

The use of the CHR line was my attempt in eliminating these unwanted characters from showing in when the machine replies.

Best Regards

Adam
Was This Post Helpful? 0
  • +
  • -

#5 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Reading Between two characters

Posted 13 October 2009 - 02:30 AM

it is so easy follow this link

Replace in vb6

if you are not able to find the result there

http://www.vb-helper...to_replace.html

This post has been edited by thava: 13 October 2009 - 02:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Reading Between two characters

Posted 13 October 2009 - 05:48 AM

This is VB.NET. Moving.
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Reading Between two characters

Posted 13 October 2009 - 08:23 AM

Try this

 txtUpdateTextbox.Text = txtUpdateTextbox.Text.Substring(2, txtUpdateTextbox.Text.Length - 4)



Or if you could just as easily do something like this

		txtUpdateTextbox.Text = txtUpdateTextbox.Text.Replace("+8", "")
		txtUpdateTextbox.Text = txtUpdateTextbox.Text.Replace("+0", "")


Or, as I just discovered...

txtUpdateTextbox.Text = txtUpdateTextbox.Text.Replace("+8", "").Replace("+0", "")


This post has been edited by CharlieMay: 13 October 2009 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#8 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Reading Between two characters

Posted 13 October 2009 - 09:43 AM

Your replace call is very wonky.

Public Function Replace(
   ByVal Expression As String,
   ByVal Find As String,
   ByVal Replacement As String) As String


First of all note that it returns a String. This is the result of replacing any occurrences of the Find parameter with the Replacement parameter in the string specified by the Expression parameter. So:

Replace(Chr(43) & Chr(56), Chr(0), 1)

Chr(43) & Chr(56) is much easier to read as "+8"...

Replace("+8", Chr(0), 1)

The 1 at the end should be a String not an Integer. You can't have Option Strict switched on or it wouldn't compile. VB.Net will convert the integer to a string. So again we can re-write it:

Replace("+8", Chr(0), "1")

It looks in the String "+8" for instances of chr(0) - the null character, commonly used in C, but not used in VB.Net, and replaces it with "1". Then it throws away the result, because you don't do anything with it. The result would be "+8" anyway as it won't contain Chr(0).

Strings Are Immutable. This complicated sounding phrase means "strings cannot be changed". Whenever you do something that looks like you are changing a String in .Net, you aren't - you are creating a New String that has the changes in it. This is why Replace returns a String value - you are supposed to assign it to something.

If we have "+8Hello World0+" and we want to get rid of the +8 and 0+ then we can use any of the String methods mentioned so far. Substring is pretty simple. It's probably best to check that the String starts with +8 before you do it though.


	Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
		Dim data As String = serialPort.ReadExisting
		' Strings are immutable - you cannot change a string. '
		' Substring will return a new String without the +8 at the start, '
		' and assign it to data. So it just looks like you changed it. '
		If data.StartsWith("+8") Then data = data.Substring(2, data.Length - 2)
		If data.EndsWith("0+") Then data = data.Substring(0, data.Length - 2)
		' Send the string off to the other function. It will do the cross-thread '
		' marshalling itself. '
		UpdateTxtDataReceived(data)
	End Sub

	Public Delegate Sub UpdateTxtDataReceivedDelegate(ByVal text As String)
	Public Sub UpdateTxtDataReceived(ByVal text As String)
		If txtDataReceived.InvokeRequired Then
			' We are in the wrong thread and cannot safely update the UI. '
			Dim params() As Object = {text}
			Dim del As New UpdateTxtDataReceivedDelegate(AddressOf UpdateTxtDataReceived)
			txtDataReceived.Invoke(del, params)
		Else
			' We are in the UI thread and can make changes to the UI. '
			txtDataReceived.AppendText(text)
			txtDataReceived.ScrollToCaret()
		End If
	End Sub

This post has been edited by crepitus: 13 October 2009 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1