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