Serial Port Timer

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
C

cmdolcet69

This code below write a serial command to the com port then read the
results and disaplays it into the label1.text property when
button1_click event is triggered. I think what is going on is that the
ocp.read is on a timer and reads char by char. How can i change this
code so that the read reads the whole result and not char by char?




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Label1.Text = String.Empty
Try
' Enable the timer.
' Write an user specified Command to the Port.
oCP.Write("[REQ]")
WriteMessage("") ', True)

Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to write to comm port")
Finally
'TextBox1.Text = ""
'TextBox1.Focus()
End Try
End Sub

Private Sub tmrRead_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrRead.Tick
Try
' As long as there is information, read one byte at a time
and
' output it.
While (oCP.Read(1) <> -1)
' Write the output to the screen.
WriteMessage(Chr(oCP.InputStream(0))) ', False)
End While
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


' This subroutine writes a message to the txtStatus TextBox.
Private Sub WriteMessage(ByVal message As String)

' Me.TextBox2.Text += message + vbCrLf
Me.Label1.Text += message
' TextBox2.SelectionStart = TextBox2.Text.Length
End Sub




' This function returns an integer specifying the number of bytes
' read from the Comm Port. It accepts a parameter specifying the
number
' of desired bytes to read.
Public Function Read(ByVal Bytes2Read As Integer) As Integer
Dim iReadChars, iRc As Integer

' If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = miBufferSize
If mhRS = -1 Then
Throw New ApplicationException( _
"Please initialize and open port before using this
method")
Else
' Get bytes from port
Try
' Purge buffers
'PurgeComm(mhRS, PURGE_RXCLEAR Or PURGE_TXCLEAR)
' Creates an event for overlapped operations
If meMode = Mode.Overlapped Then
pHandleOverlappedRead(Bytes2Read)
Else
' Non overlapped mode
ReDim mabtRxBuf(Bytes2Read - 1)
iRc = ReadFile(mhRS, mabtRxBuf, Bytes2Read,
iReadChars, Nothing)
If iRc = 0 Then
' Read Error
Throw New ApplicationException( _
"ReadFile error " & iRc.ToString)
Else
' Handles timeout or returns input chars
If iReadChars < Bytes2Read Then
Throw New IOTimeoutException("Timeout
error")
Else
mbWaitOnRead = True
Return (iReadChars)
End If
End If
End If
Catch Ex As Exception
' Others generic erroes
Throw New ApplicationException("Read Error: " &
Ex.Message, Ex)
End Try
End If
End Function
 
I have played with the serial port but What I used was by the event.
I did not use a timer.

I dont know if this is relevant for you or not?


Private Sub ListeningSerialPort_DataReceived(ByVal sender As System.Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles ListeningSerialPort.DataReceived

Dim ReceivingText As String = ""
ReceivingText = ListeningSerialPort.ReadLine


I played with this to hook a barcode scanner and throw the readings into a text box.

Miro

This code below write a serial command to the com port then read the
results and disaplays it into the label1.text property when
button1_click event is triggered. I think what is going on is that the
ocp.read is on a timer and reads char by char. How can i change this
code so that the read reads the whole result and not char by char?




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Label1.Text = String.Empty
Try
' Enable the timer.
' Write an user specified Command to the Port.
oCP.Write("[REQ]")
WriteMessage("") ', True)

Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to write to comm port")
Finally
'TextBox1.Text = ""
'TextBox1.Focus()
End Try
End Sub

Private Sub tmrRead_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrRead.Tick
Try
' As long as there is information, read one byte at a time
and
' output it.
While (oCP.Read(1) <> -1)
' Write the output to the screen.
WriteMessage(Chr(oCP.InputStream(0))) ', False)
End While
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


' This subroutine writes a message to the txtStatus TextBox.
Private Sub WriteMessage(ByVal message As String)

' Me.TextBox2.Text += message + vbCrLf
Me.Label1.Text += message
' TextBox2.SelectionStart = TextBox2.Text.Length
End Sub




' This function returns an integer specifying the number of bytes
' read from the Comm Port. It accepts a parameter specifying the
number
' of desired bytes to read.
Public Function Read(ByVal Bytes2Read As Integer) As Integer
Dim iReadChars, iRc As Integer

' If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = miBufferSize
If mhRS = -1 Then
Throw New ApplicationException( _
"Please initialize and open port before using this
method")
Else
' Get bytes from port
Try
' Purge buffers
'PurgeComm(mhRS, PURGE_RXCLEAR Or PURGE_TXCLEAR)
' Creates an event for overlapped operations
If meMode = Mode.Overlapped Then
pHandleOverlappedRead(Bytes2Read)
Else
' Non overlapped mode
ReDim mabtRxBuf(Bytes2Read - 1)
iRc = ReadFile(mhRS, mabtRxBuf, Bytes2Read,
iReadChars, Nothing)
If iRc = 0 Then
' Read Error
Throw New ApplicationException( _
"ReadFile error " & iRc.ToString)
Else
' Handles timeout or returns input chars
If iReadChars < Bytes2Read Then
Throw New IOTimeoutException("Timeout
error")
Else
mbWaitOnRead = True
Return (iReadChars)
End If
End If
End If
Catch Ex As Exception
' Others generic erroes
Throw New ApplicationException("Read Error: " &
Ex.Message, Ex)
End Try
End If
End Function
 
I have played with the serial port but What I used was by the event.
I did not use a timer.

I dont know if this is relevant for you or not?

Private Sub ListeningSerialPort_DataReceived(ByVal sender As System.Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles ListeningSerialPort.DataReceived

Dim ReceivingText As String = ""
ReceivingText = ListeningSerialPort.ReadLine

I played with this to hook a barcode scanner and throw the readings into a text box.

Miro


This code below write a serial command to the com port then read the
results and disaplays it into the label1.text property when
button1_click event is triggered. I think what is going on is that the
ocp.read is on a timer and reads char by char. How can i change this
code so that the read reads the whole result and not char by char?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Label1.Text = String.Empty
Try
' Enable the timer.
' Write an user specified Command to the Port.
oCP.Write("[REQ]")
WriteMessage("") ', True)
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to write to comm port")
Finally
'TextBox1.Text = ""
'TextBox1.Focus()
End Try
End Sub
Private Sub tmrRead_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrRead.Tick
Try
' As long as there is information, read one byte at a time
and
' output it.
While (oCP.Read(1) <> -1)
' Write the output to the screen.
WriteMessage(Chr(oCP.InputStream(0))) ', False)
End While
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
' This subroutine writes a message to the txtStatus TextBox.
Private Sub WriteMessage(ByVal message As String)
' Me.TextBox2.Text += message + vbCrLf
Me.Label1.Text += message
' TextBox2.SelectionStart = TextBox2.Text.Length
End Sub
' This function returns an integer specifying the number of bytes
' read from the Comm Port. It accepts a parameter specifying the
number
' of desired bytes to read.
Public Function Read(ByVal Bytes2Read As Integer) As Integer
Dim iReadChars, iRc As Integer
' If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = miBufferSize
If mhRS = -1 Then
Throw New ApplicationException( _
"Please initialize and open port before using this
method")
Else
' Get bytes from port
Try
' Purge buffers
'PurgeComm(mhRS, PURGE_RXCLEAR Or PURGE_TXCLEAR)
' Creates an event for overlapped operations
If meMode = Mode.Overlapped Then
pHandleOverlappedRead(Bytes2Read)
Else
' Non overlapped mode
ReDim mabtRxBuf(Bytes2Read - 1)
iRc = ReadFile(mhRS, mabtRxBuf, Bytes2Read,
iReadChars, Nothing)
If iRc = 0 Then
' Read Error
Throw New ApplicationException( _
"ReadFile error " & iRc.ToString)
Else
' Handles timeout or returns input chars
If iReadChars < Bytes2Read Then
Throw New IOTimeoutException("Timeout
error")
Else
mbWaitOnRead = True
Return (iReadChars)
End If
End If
End If
Catch Ex As Exception
' Others generic erroes
Throw New ApplicationException("Read Error: " &
Ex.Message, Ex)
End Try
End If
End Function- Hide quoted text -

- Show quoted text -

Miro was that in vb 2003 or vb 2005. I think the code above only apply
to vb 2005, have you ever done anything in vb 2003. I know i have read
in a couple of places that serial COM is very weak in 2003. Please
confirm that the code above is for 2003 or 2005
 
It was for 2005.

But out of the express :) so if you need to write a quick dummy app... you might be
ok downloading vb.net 2005 express.

I have never played with 2003.
Ive been playing with 2005 left and right trying to slowly learn it.

M.
I have played with the serial port but What I used was by the event.
I did not use a timer.

I dont know if this is relevant for you or not?

Private Sub ListeningSerialPort_DataReceived(ByVal sender As System.Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles ListeningSerialPort.DataReceived

Dim ReceivingText As String = ""
ReceivingText = ListeningSerialPort.ReadLine

I played with this to hook a barcode scanner and throw the readings into a text box.

Miro


This code below write a serial command to the com port then read the
results and disaplays it into the label1.text property when
button1_click event is triggered. I think what is going on is that the
ocp.read is on a timer and reads char by char. How can i change this
code so that the read reads the whole result and not char by char?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Label1.Text = String.Empty
Try
' Enable the timer.
' Write an user specified Command to the Port.
oCP.Write("[REQ]")
WriteMessage("") ', True)
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to write to comm port")
Finally
'TextBox1.Text = ""
'TextBox1.Focus()
End Try
End Sub
Private Sub tmrRead_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrRead.Tick
Try
' As long as there is information, read one byte at a time
and
' output it.
While (oCP.Read(1) <> -1)
' Write the output to the screen.
WriteMessage(Chr(oCP.InputStream(0))) ', False)
End While
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
' This subroutine writes a message to the txtStatus TextBox.
Private Sub WriteMessage(ByVal message As String)
' Me.TextBox2.Text += message + vbCrLf
Me.Label1.Text += message
' TextBox2.SelectionStart = TextBox2.Text.Length
End Sub
' This function returns an integer specifying the number of bytes
' read from the Comm Port. It accepts a parameter specifying the
number
' of desired bytes to read.
Public Function Read(ByVal Bytes2Read As Integer) As Integer
Dim iReadChars, iRc As Integer
' If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = miBufferSize
If mhRS = -1 Then
Throw New ApplicationException( _
"Please initialize and open port before using this
method")
Else
' Get bytes from port
Try
' Purge buffers
'PurgeComm(mhRS, PURGE_RXCLEAR Or PURGE_TXCLEAR)
' Creates an event for overlapped operations
If meMode = Mode.Overlapped Then
pHandleOverlappedRead(Bytes2Read)
Else
' Non overlapped mode
ReDim mabtRxBuf(Bytes2Read - 1)
iRc = ReadFile(mhRS, mabtRxBuf, Bytes2Read,
iReadChars, Nothing)
If iRc = 0 Then
' Read Error
Throw New ApplicationException( _
"ReadFile error " & iRc.ToString)
Else
' Handles timeout or returns input chars
If iReadChars < Bytes2Read Then
Throw New IOTimeoutException("Timeout
error")
Else
mbWaitOnRead = True
Return (iReadChars)
End If
End If
End If
Catch Ex As Exception
' Others generic erroes
Throw New ApplicationException("Read Error: " &
Ex.Message, Ex)
End Try
End If
End Function- Hide quoted text -
- Show quoted text -

Miro was that in vb 2003 or vb 2005. I think the code above only apply
to vb 2005, have you ever done anything in vb 2003. I know i have read
in a couple of places that serial COM is very weak in 2003. Please
confirm that the code above is for 2003 or 2005
 
Hi,

I suggest that you go to my homepage and download DesktopSerialIO (free).
This makes reading the serial port much easier. You can use a Timer, or
OnComm receive event processing. Normal code (including the terminal
example included) reads ALL available data each time you call InputString
(text data) or InputArray (binary data).

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
Back
Top