Missing data from ReadByte()

Hey all, Just finished writing a mostly working VB .NETCF2 program for an iPAQ. I am sending data over bluetooth to the iPAQ which has the BT port mapped as COM8 so that it works exactly like a serial port. I am sending this string from my device "H###L###E" The # are numbers determined by an analog digital converter (10 bits) so the ### following the H are the high byte in BCD and the ### following the L are the BCD's of the low byte and the E is the terminating character. Anyway I am getting the data fine but I will get 3-4 sets of the data with the regular format but then for about 2 samples the string is missing the "H" character. As a workaround I am checking the string to see if it starts with H and if not then discard the string. Here's the program:


Public Class Form1

Dim WithEvents SP1 As New System.IO.Ports.SerialPort
Dim voltage As Int16

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PB1.Maximum = 1050
End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
If SP1.IsOpen Then
SP1.Close()
End If

Try
With SP1
.PortName = "COM8"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
.NewLine = "E"
.ReceivedBytesThreshold = 1
End With
SP1.Open()
lblMessage.Text = " connected."
btnConnect.Enabled = False
btnDisconnect.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
Try
SP1.Close()
lblMessage.Text = " disconnected."
btnConnect.Enabled = True
btnDisconnect.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

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

txtDataReceived.Invoke(New _
myDelegate(AddressOf updateTextBox), _
New Object() {})
End Sub

Public Delegate Sub myDelegate()
Public Sub updateTextBox()
Dim temp As Byte
Dim tempS As String
Dim hval As Short
Dim lval As Byte

tempS = ""
While (SP1.BytesToRead > 0)
temp = SP1.ReadByte()
If Not (Convert.ToChar(temp) = "E") Then
tempS += Convert.ToChar(temp)
Else

If (tempS.StartsWith("H")) Then
hval = Val(tempS.Substring(2, 3))
txtDataReceived.Text = tempS
'Else
' hval = Val(tempS.Substring(1, 3))
Label1.Text = hval
lval = Val(tempS.Substring(6))
Label2.Text = lval
voltage = (hval << 8) + lval
Label3.Text = voltage
PB1.Value = voltage
End If
tempS = ""
End If

End While
End Sub

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
SP1.Close()
Me.Close()
End Sub
End Class


I know that I am sending the correct string the entire time because when I use vxHPC which is a hyperterminal program for the iPAQ it lists the samples perfectly so it must be something with how I am retrieving the values from the Serial buffer. I am using the current ReadByte() method because when I would do things like ReadExisting() or ReadLine() I would only be able to get 2 or 3 samples before it would just stop and sometimes it wouldnt even read from the buffer at all. For all my purposes it works but I would rather not have that workaround without me understanding whats going wrong. Thanks for any help guys!




Answer this question

Missing data from ReadByte()

  • jjjames

    Hi,

    I also have a similar problem with an IPAQ rz1700, However I am using raw infra red port.

    the .NET code for WriteFile always works, however ReadFile does not work as expected, even if the input buffer is empty, the ReadFile will always return 0xFF

    I have seen posting on other forum that it is an issue with rz1700.

    I am aware that the IrComm works on iPAQ, the problem is only when you access raw serial ir port.

    Thanks for any suggestions.


  • Missing data from ReadByte()