2 Replies - 4144 Views - Last Post: 03 November 2009 - 06:28 AM Rate Topic: -----

#1 joltron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-October 09

Reading from serial port into a string

Posted 22 October 2009 - 09:04 AM

Looking for some help. My background is in PLC programming and starting to dive into some VB for some fun and to simplify some stuff.
There is a bit of a learning curve so far.
For starters I'm using VB 2008 express edition
I have code that writes a string out of a serial port.
My issue is that the machine sends data back and I don't know how to catch it.

Any help would be greatly appreciated!

This is my latest attempt at creating an array of characters to create a string later but doesn't work.
I've been trying at this for 2 days and figure that has to be a simple solution that I"m not seeing

-Joel

Private Sub tmrReadCommPort_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadCommPort.Tick
		Try
			' As long as there is information, read one byte at a time and 
			'   output it.
			Dim count As Integer = 0
			While (m_CommPort.Read(1) <> -1)
				' Write the output to the screen.
				WriteMessage(Chr(m_CommPort.InputStream(0)), False)

				ArrayChars(count) = Chr(m_CommPort.InputStream(0))
				
				count = count + 1
			End While


			ComReadString1 = New String(ArrayChars)
			Console.WriteLine(ComReadString1)
			ComReadString.Text = ComReadString1
		

		Catch exc As Exception
			' An exception is raised when there is no information to read.
			'   Don't do anything here, just let the exception go.
		End Try

	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Reading from serial port into a string

#2 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Reading from serial port into a string

Posted 23 October 2009 - 06:53 AM

Try here:
http://www.dreaminco...wtopic37361.htm
Was This Post Helpful? 1
  • +
  • -

#3 joltron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-October 09

Re: Reading from serial port into a string

Posted 03 November 2009 - 06:28 AM

Is this good practice for waiting on serial data?

Prior to code i enable a timer that every tick it reads in from the serial buffer if there is data.

 Private Sub WaitForString(ByVal intWaitTime As Integer)
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		'Loop that waits for feed back from COM read or time out	   '
		'IMPORTANT the combination of the number compared to temploop  '
		'and the sleep timer together adjust how long this loops until '
		'exiting													   '
		''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
		Dim temploop As Integer = 1

		While temploop < intWaitTime And String.Compare(strComReadglob, "") = 0
			System.Threading.Thread.Sleep(500)
			txtbStatus.Text = (strComReadglob)
			'MessageBox.Show(Convert.ToString(temploop))
			temploop = temploop + 1
			Application.DoEvents()
		End While

	End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1