Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Infrared Programming Using the .NET Compact Framework : Page 3

If you've ever used a TV remote control, you've experienced the convenience of using infrared communications. See how to use infrared to communicate between a Windows Mobile device and a PC.


advertisement
Building the PC Application
With the Pocket PC application built, you can now build the PC application that listens for incoming infrared data.

Using Visual Studio 2005, create a new Windows application and name it Infrared_Desktop. Populate the default Form with the PictureBox control (see Figure 2).

Figure 2. Form1: Populated with the PictureBox control.



Add a reference to the InTheHand.Net.Personal library.

Switch to the code-behind of Form1 and import the following namespaces:

Imports System.IO Imports System.Net.Sockets Imports InTheHand.Net.Sockets As usual, define the following constants and member variable: Public Class Form1 '---define the constants--- Const DATA_BLOCK_SIZE As Integer = 8192 Const MAX_TRIES As Integer = 3 Private ServiceName As String = "default"

Define the ReceiveData() function so that it can receive incoming infrared data (shown in Listing 2).

You use the IrDAListener class to listen for an IrDa connection. Once a connection is established, you read the incoming data using a Stream object. Because incoming data may not be read in one go, you will repeatedly read from the stream until no more data is available. The function returns the data received as a byte array.

Next, define the ReceiveLoop() subroutine that calls the ReceiveData() function in a infinite loop:

Public Sub ReceiveLoop() Dim dataReceived As Byte() '---listen for incoming data--- dataReceived = ReceiveData() While True If DataReceived.Length > 0 Then PictureBox1.BeginInvoke( _ New myDelegate(AddressOf DisplayImage), _ New Object() {dataReceived}) End If '---keep on listening for incoming data--- dataReceived = ReceiveData() End While End Sub

Define the DisplayImage() subroutine and its delegate as follows:

Private Delegate Sub myDelegate(ByVal data As Byte()) Private Sub DisplayImage(ByVal data As Byte()) Try Dim ms As MemoryStream = New MemoryStream(data) PictureBox1.Image = New Bitmap(Image.FromStream(ms)) Catch ex As Exception MsgBox(ex.ToString) End Try End Sub

The DisplayImage() subroutine converts the received byte array into an image so that it can be displayed by the PictureBox control.

Finally, when the form is loaded, invoke the ReceiveLoop() subroutine using a thread so that it can listen for incoming data in the background:

Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load '---receive incoming data as a separate thread--- Dim t1 As System.Threading.Thread t1 = New Threading.Thread(AddressOf ReceiveLoop) t1.Start() End Sub



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap