S
silverfox
I have been trying to write a VB program to use Bluetooth to
communicate between a PDA running Windows CE 4.2 and another BT
device. I found this article that got me started:
http://www.devx.com/wireless/Article/11511
Using the technique in this article, I can successfully transmit data
but the receive part fails. In fact it slows the PDA to a crawl.
Does anyone have any ideas on how to modify the original program so
that the receive function will work?
BTW, I am using VS 2005 and the original program was for an earlier
version of VS. I am not sure if this is relevant.
Here is the relevant section of the source from the article:
Dim infileHandler As Long
Dim outfileHandler As Long
Dim numReadWrite As Integer
Dim t1 As System.Threading.Thread
Dim stopThread As Boolean = False
Public Sub connect()
'---port number for Bluetooth connection
Dim inPort As Short = 8
Dim outPort As Short = 6
'---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
Public Sub disconnect()
stopThread = True
CloseHandle(infileHandler)
CloseHandle(outfileHandler)
End Sub
<DllImport("coredll.dll")> _
Private Shared Function CreateFile(ByVal lpFileName As String, _
ByVal dwDesiredAccess As
Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As
Integer, _
ByVal dwCreationDisposition As
Integer, _
ByVal dwFlagsAndAttributes As
Integer, _
ByVal hTemplateFile As Integer)
As Integer
End Function
<DllImport("coredll.dll")> _
Private Shared Function ReadFile(ByVal hFile As Integer, _
ByVal Buffer() As Byte, _
ByVal nNumberOfBytesToRead As
Integer, _
ByRef lpNumberOfBytesRead As
Integer, _
ByRef lpOverlapped As Integer) As
Integer
End Function
<DllImport("coredll.dll")> _
Private Shared Function WriteFile(ByVal hFile As Integer, _
ByVal Buffer() As Byte, _
ByVal nNumberOfBytesToWrite As
Integer, _
ByRef lpNumberOfBytesWritten As
Integer, _
ByVal lpOverlapped As Integer)
As Boolean
End Function
<DllImport("coredll.dll")> _
Private Shared Function CloseHandle(ByVal hObject As Integer) As
Integer
End 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
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
'MsgBox("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
The only thing I had to change was the port numbers to match the BT
COM ports on my PDA. The Text box for receiving the incoming stream
immediately becomes filled with repeated instances of -->, as
updateMessageLog is apparently called repeatedly. I hope that there
is a simple solution to this problem.
communicate between a PDA running Windows CE 4.2 and another BT
device. I found this article that got me started:
http://www.devx.com/wireless/Article/11511
Using the technique in this article, I can successfully transmit data
but the receive part fails. In fact it slows the PDA to a crawl.
Does anyone have any ideas on how to modify the original program so
that the receive function will work?
BTW, I am using VS 2005 and the original program was for an earlier
version of VS. I am not sure if this is relevant.
Here is the relevant section of the source from the article:
Dim infileHandler As Long
Dim outfileHandler As Long
Dim numReadWrite As Integer
Dim t1 As System.Threading.Thread
Dim stopThread As Boolean = False
Public Sub connect()
'---port number for Bluetooth connection
Dim inPort As Short = 8
Dim outPort As Short = 6
'---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
Public Sub disconnect()
stopThread = True
CloseHandle(infileHandler)
CloseHandle(outfileHandler)
End Sub
<DllImport("coredll.dll")> _
Private Shared Function CreateFile(ByVal lpFileName As String, _
ByVal dwDesiredAccess As
Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As
Integer, _
ByVal dwCreationDisposition As
Integer, _
ByVal dwFlagsAndAttributes As
Integer, _
ByVal hTemplateFile As Integer)
As Integer
End Function
<DllImport("coredll.dll")> _
Private Shared Function ReadFile(ByVal hFile As Integer, _
ByVal Buffer() As Byte, _
ByVal nNumberOfBytesToRead As
Integer, _
ByRef lpNumberOfBytesRead As
Integer, _
ByRef lpOverlapped As Integer) As
Integer
End Function
<DllImport("coredll.dll")> _
Private Shared Function WriteFile(ByVal hFile As Integer, _
ByVal Buffer() As Byte, _
ByVal nNumberOfBytesToWrite As
Integer, _
ByRef lpNumberOfBytesWritten As
Integer, _
ByVal lpOverlapped As Integer)
As Boolean
End Function
<DllImport("coredll.dll")> _
Private Shared Function CloseHandle(ByVal hObject As Integer) As
Integer
End 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
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
'MsgBox("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
The only thing I had to change was the port numbers to match the BT
COM ports on my PDA. The Text box for receiving the incoming stream
immediately becomes filled with repeated instances of -->, as
updateMessageLog is apparently called repeatedly. I hope that there
is a simple solution to this problem.