DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Problem with Pocket PC reading its inbound COM port

  1. #1
    Join Date
    Aug 2005
    Posts
    3

    Problem with Pocket PC reading its inbound COM port

    Hi,

    I'm using VB.NET 2003 to write an application for the Pocket PC 2003 running on a HP iPAQ hx2700.

    The program should work like this: when a "Connect" button is clicked, the Pocket PC connects to its Inbound COM port which I understand as the

    incoming data port, please correct me if I'm wrong.

    The Pocket PC is connected to a desktop PC via Bluetooth. The desktop PC is always writing to its Bluetooth port (I'm using CSR's socket for

    Bluetooth connection), so there's always data being transmitted to any device connecting to it via Bluetooth.

    So when Pocket PC is connected to its inbound COM port, it should receive a steady stream of data.

    I'm having problems firstly with connecting to the port, and secondly when I think I've connected, I'm getting streams of empty strings even when

    the desktop PC is not sending anything.

    My main problem seems to be trying to get the Pocket PC to read its inbound port.

    Could somebody please guide me on how to write a simple code to accomplish this.

    Thank you and regards.
    Sashi

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    It could be many things: byte ordering, bad reception, encryption, configuartion mismatch, nabdwidth mismatch, nackground inteference from other bluetooth devices (if any) etc. My advice is therefore to isolate the cause of the problem. Try to send a short stream of bytes with a predefine binary values, say 0x01, and see if this succeeds. Check whether you're receiving the correct number of bytes and the correct byte values. Then try to increase the length of the stream until you reach a saturation point. Remember that even if it looks as if both devices are constantly connected, that doesn't mean that they're actually transmitting constantly.
    Danny Kalev

  3. #3
    Join Date
    Aug 2005
    Posts
    3

    Unhappy

    Thanks for the reply Danny.

    Let me explain in more detail what I'm doing.

    I'm writing an app that reads and writes to a desktop PC from a Pocket PC using VB.Net 2003. The desktop PC is connected to PDA via Bluetooth. The desktop PC uses a CRS Bluetooth Socket adapter connected to COM1.
    I'm using a terminal client on the PC to read and write to that COM port.

    On the PDA, I'm using the CreateFile, ReadFile, WriteFile and CloseHandle functions imported from coredll.

    I've modified the example from a Bluetooth chat app for this purpose.

    I open the ports like this:

    Public Sub ConnectBT()
    '---port number for Bluetooth connection
    Dim inPort As Short = 5
    Dim outPort As Short = 8

    '---Opens the port for Bluetooth
    infileHandler = CreateFile("COM" & inPort & ":", _
    &HC0000000, 0, 0, 3, 0, 0)
    Application.DoEvents()
    outfileHandler = CreateFile("COM" & outPort & ":", _
    &HC0000000, 0, 0, 3, 0, 0)
    Application.DoEvents()

    '---invoke the thread to receive incoming messages
    stopThread = False
    t1 = New Threading.Thread(AddressOf receiveLoop)
    t1.Start()

    End Sub

    Then receiveLoop is invoked:

    Public Sub receiveLoop()
    '---receive the message through the serial port
    Dim inbuff(300) As Byte
    Dim retCode As Integer = ReadFile(infileHandler, _
    inbuff, _
    inbuff.Length, _
    numReadWrite, _
    0)
    Application.DoEvents()
    While True
    If retCode = 0 Or stopThread Then
    txtMessageLog.Text += vbNewLine & "Error reading message."
    Exit While
    Else
    Dim updateDelegate As New _
    myDelegate(AddressOf updateMessageLog)

    updateDelegate.Invoke(byteArrayToString(inbuff))
    ReDim inbuff(300)
    retCode = ReadFile(infileHandler, _
    inbuff, _
    inbuff.Length, _
    numReadWrite, _
    0)
    Application.DoEvents()
    End If
    End While
    End Sub

    Public Delegate Sub myDelegate(ByVal str As String)

    Public Sub updateMessageLog(ByVal str As String)
    If str.Length > 0 Then
    txtMessageLog.Text += "-->" & str
    End If
    End Sub

    When I type in something in another textbox and click "send" button, I call the send() function:

    Public Function send(ByVal message As String) As Integer
    '---send the message through the serial port
    Dim value As String = message & vbCrLf
    Dim retCode As Integer = WriteFile(outfileHandler, _
    stringToByteArray(value), _
    value.Length(), _
    numReadWrite, _
    0)
    txtMessageLog.Text += value
    Return retCode
    End Function

    The functions to convert string to byte arrays and vice versa is this:

    Public Function stringToByteArray(ByVal str As String) As Byte()
    '---e.g. "abcdefg" to {a,b,c,d,e,f,g}
    Dim s As Char()
    s = str.ToCharArray
    Dim b(s.Length - 1) As Byte
    Dim i As Integer
    For i = 0 To s.Length - 1
    b(i) = Convert.ToByte(s(i))
    Next
    Return b
    End Function

    Function byteArrayToString(ByVal b() As Byte) As String
    '---e.g. {a,b,c,d,e,f,g} to "abcdefg"
    Dim str As String
    Dim enc As System.Text.ASCIIEncoding
    enc = New System.Text.ASCIIEncoding
    str = enc.GetString(b, 0, b.Length())
    Return str
    End Function

    My problem is simply that I can send to the PC, but I cannot receive anything.

    When the program is running, I get a series of "-->" flooding the textbox, even though the str value received in updateMessageLog() is nothing (""). Weirdly, when watching the variables, I find the value of str.Length to be 301, even though str itself is ""!

    I'm not too familiar with VB.NET, which is why I'm using this code found online. Could anyone help me to simply read from the Pocket PC port? It doesn't have to be too complicated. I'm in a terrible bind right now and I'd appreciate any assistance..

    Thanks and regards!
    SASHI

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    VB isn't exactly my language of expertise so I'm copying this thread to the .Net forum. Perhaps one of the VB experts can provide a clue.
    Danny Kalev

  5. #5
    Join Date
    Nov 2005
    Posts
    1
    Hi

    i have the same source as you have
    the problem for flood reception is just that it's not the size of str to test but the numReadWrite

    Public Sub updateMessageLog(ByVal str As String)
    If str.Length > 0 Then
    txtMessageLog.Text += "-->" & str
    End If
    End Sub

    -> Public Sub updateMessageLog(ByVal str As String)
    If numReadWrite > 0 Then
    txtMessageLog.Text += "-->" & str
    End If
    End Sub

Similar Threads

  1. TcpListener thread problem
    By David Chu in forum .NET
    Replies: 2
    Last Post: 01-23-2003, 02:27 PM
  2. Reliability Problem
    By elise in forum .NET
    Replies: 0
    Last Post: 10-30-2002, 05:39 AM
  3. Replies: 0
    Last Post: 07-31-2002, 05:17 PM
  4. Replies: 0
    Last Post: 12-13-2001, 01:06 PM
  5. Replies: 0
    Last Post: 10-02-2001, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center