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 2

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 Pocket PC Application
First, let's build the Pocket PC application that allows users to send an image to the desktop using infrared.

Using Visual Studio 2005, create a new Windows Mobile 5.0 Pocket PC application and name it Infrared. Populate the default Form1 with the following controls (see Figure 1):

  • Button
  • PictureBox
  • StatusBar
  • MenuItem
  • Figure 1. Form1: Populated with the various controls.



    Next, add a reference to the System.Net.IrDa library.

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

    Imports System.Net Imports System.IO Imports System.Net.Sockets

    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"

    The DATA_BLOCK_SIZE constant is the maximum size of data exchanged, and MAX_TRIES is the maximum number of tries to send a block of data before giving up. The ServiceName variable is a unique identifier used by the two communicating parties participating in an infrared communication session.

    Code the "Select image to send” button as follows: Private Sub btnSelectImage_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnSelectImage.Click Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.Filter = "JPEG files (*.jpg)|*.jpg" If openFileDialog1.ShowDialog() = _ DialogResult.OK Then '---load the PictueBox control with the select image--- PictureBox1.Image = New Bitmap(openFileDialog1.FileName) End If End Sub

    The above code basically displays the Open File dialog that allows users to select a .jpg image. Once the image is selected, it is displayed in the PictureBox control.

    Next, define the SendData() subroutine, which sends the image to the PC over an infrared connection (shown in Listing 1).

    First, the SendData() subroutine tries to establish a connection with the other device (using the IrDAClient class) until the number of retries is exceeded. Once a connection is established, it writes to the other device using an IO Stream object. Finally, it closes the stream and the connection.

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

    Private Delegate Sub myDelegate(ByVal str As String) Private Sub UpdateStatus(ByVal str As String) '---delegate to update the statusbar control StatusBar1.Text = str End Sub

    Finally, code the Send menu item as follows:

    The mnuSend_Click event handler first converts the image in the PictureBox control into a byte array and then sends it to the PC by calling the SendData() subroutine, defined earlier in Listing 1.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap