Serial COM.Read

C

cmdolcet69

Can anyone help me out? I have some code below that writes a packet of
bytes into the Serial COM. When i call the Ocp.Read (7) i pass in all
7 bytes and i can;t get it to return byte(2) and byte(3) position?
The mabtRxBuf stores all the byte written in however it return an
integer i want it to return the byte (2) and byte(3) so i can convert
this into an integer myself....


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Packet(0) = 42 'Constant BMS message
Packet(1) = 4 'Constant Length of Table upload
Packet(2) = 6 ' Constant Command of Table upload
Packet(3) = 23
Packet(4) = 152
oCP.Write(Packet, 0, 4)
oCP.Read(7)

Thread.Sleep(5)
WriteMessage(TextBox1.Text, 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



'This is the oCp.Read function
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
 
D

Dick Grier

Hi,

What serial object are you using?

If you are using VS2005, then your best bet is to use the built-in serial
port object. If you are using VS2002 or VS2003, you might want to use my
DesktopSerialIO dll (or the Serial object from www.opennetcf.org). All of
the heavy lifting is done for you. See Downloads on my homepage.

You should not PurgeComm, IMO. Suppose that data already have been buffered
by the UART FIFO, prior to this call? Data will be discarded. There really
is no benefit to be derived that I can think of.

If you were to use DesktopSerialIO, you code might look something like this
(written without testing)

Dim Timeout As Now.TimeOfDat.TotalMilliseconds + SomeNumber
Dim PacketReceived As Boolean = False
Do Until Now.TimeOfDat.TotalMilliseconds >= Timeout
With SerialPort
If .InBufferCount >= 7 Then
Dim Buffer(.InBufferCount -1) As Byte
Buffer = .InputArray()
PacketReceived = True
Exit Do
End If
End With
Loop
If PacketReceived = True Then
Process Buffer 'you write this code
Else
ThereWasATimeout
End If

This is only one way - there are several others that I can think of.

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

Dick Grier

BTW, if you are using VS2005, and the built-in serial port control, use the
BytesToRead property and the Read method instead.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top