' ' Copyright (c) 2005-2006 Alan J. McFarlane ' ' irdaDotNetCliIrComm.vb ' ' A sample (IrDA) IrCOMM client using VB.NET with the 32feet.NET library. ' ' For greatest simplicity this sample is a command-line client designed to run ' on desktop Windows, however both the 32feet.NET library and the way in which ' it is used here are supported on both desktop Windows and Windows CE/ ' PocketPC (CE/PPC), i.e. on full and Compact Frameworks. ' ' Created: 2006-Jan-01 -- Based on irdaDotNetCliIrLpt.cs ' ' Example usage. ' ' C:\>irdaDotNetCliIrComm_onV2.exe ' 1 Devices found ' 0: Nokia 6210, addr: BC300000, type: PnP, Modem, Fax, Extension, ' Telephony, IrCOMM, Obex ' Selected #0 ' Gonna connect to, BC300000 IrDA:IrCOMM ' Connected ' Sleeping for 5 seconds... ' Using encoding: Western European (IA5) ' Sending AT, response: ' AT ' OK ' ' Sending ATI, response: ' ATI ' Nokia Mobile Phones ' ' OK ' ' Sending ATI1, response: ' ATI1 ' 350146208454394 ' ' OK ' ' Sending AT+GCAP, response: ' AT+GCAP ' +GCAP: +CGSM,+FCLASS,+DS ' ' OK ' ' Disconnecting... ' ' C:\> ' '====================================================================== Option Strict Option Explicit Imports System Imports System.Text 'e.g. Encoding Imports Microsoft.VisualBasic 'e.g. ControlChars Imports System.Net.Sockets 'e.g. SocketOptionLevel, NetworkStream Imports InTheHand.Net 'e.g. IrDAEndPoint Imports InTheHand.Net.Sockets 'e.g. IrDAClient ' Available from http://32feet.net/, version 1.5.51015 or later required. Namespace irdaDotNetCliIrComm Class Program '====================================================================== ' ' User selects the first peer! Do this in your UI... ' Const SelectPeerNum As Integer = 0 ' ' The Service Name to use. We are using IrCOMM, which has the ' Service Name "IrDA:IrCOMM" and it uses IrCOMM Cooked/9-wire mode ' which we enable with setsockopt ( IRLMP_9WIRE_MODE ). ' Const ServiceName As String = "IrDA:IrCOMM" '====================================================================== Public Shared Sub Main() '-------------------------------------------- ' Create client object '-------------------------------------------- Dim cli As New IrDAClient '-------------------------------------------- ' Set connection/socket options '-------------------------------------------- ' Set IrCOMM Cooked/9-wire mode. cli.Client.SetSocketOption( _ CType(IrDASocketOptionLevel.IrLmp, SocketOptionLevel), _ CType(IrDASocketOptionName.NineWireMode, SocketOptionName), _ 1) ' equivalent to 'true' '-------------------------------------------- ' Enumerate peers and select one '-------------------------------------------- ' ' This should really filter (prefer) those peers with the ' Modem Hint Bit set. ' ... = selectIrdaPeer( cli, ' HintsFilter.Prefer | HintsFilter.OneOr, ' IrDAHints.Modem ); ' Dim di As IrDADeviceInfo = selectIrdaPeer(cli) if ( di Is Nothing ) Console.Out.Write("No peers discovered" & ControlChars.Lf) return End If Dim ep As IrDAEndPoint = new IrDAEndPoint(di.DeviceAddress, ServiceName) '-------------------------------------------- ' Connect '-------------------------------------------- Console.Write("Gonna connect to, {0} {1}" & ControlChars.Lf, ep.Address, ep.ServiceName) cli.Connect(ep) Console.Write("Connected" & ControlChars.Lf) '-------------------------------------------- ' Get the maximum send size (limited by an IrLAP PDU). '-------------------------------------------- '(We're not in IrLMP mode, so there's no limit; calling GSO with 'SendPduLength would be pointless). '-------------------------------------------- ' Now use the connection... '-------------------------------------------- Dim strm As NetworkStream = cli.GetStream() ' Sleep for 5 seconds to give the IrDA "connected" icon lots of ' time to appear. Console.Write("Sleeping for 5 seconds..." & ControlChars.Lf) System.Threading.Thread.Sleep(5000) ' Now send some data to the peer. :-) ' Some commands from V.250 (previously V.25ter), see ' http://www.itu.int/rec/recommendation.asp?type=folders&lang=e&parent=T-REC-V.250 v250SendRcv(strm, "AT") v250SendRcv(strm, "ATI") v250SendRcv(strm, "ATI1") v250SendRcv(strm, "AT+GCAP") '-------------------------------------------- ' Close and Shutdown '-------------------------------------------- Console.WriteLine("Disconnecting...") strm.Close() cli.Close() End Sub '====================================================================== Shared Dim m_enc As Encoding Shared Sub v250SendRcv(strm As System.IO.Stream, cmd As String) Dim buf As byte() Dim response As String '-------------------------------------------- ' Get the respective encoding for V.250 '-------------------------------------------- ' Not thread safe, but then neither is the rest of the function. If ( m_enc Is Nothing ) ' V.25ter section 5.1 notes that alphabet IA5 is used. However ' that encoding is present only in Framework Version 2, so ' allow fallback to ASCII, which is only slightly different... Try m_enc = Encoding.GetEncoding("x-IA5") Catch ex As ArgumentException 'expecting "Invalid or unsupported code page type." 'Console.WriteLine("[ex=" & ex.ToString()) m_enc = Encoding.ASCII End Try System.Diagnostics.Debug.Assert(Not(m_enc Is Nothing)) Console.WriteLine("Using encoding: " & m_enc.EncodingName) End If '-------------------------------------------- ' Send and Receive '-------------------------------------------- Console.WriteLine("Sending " & cmd & ", response:") 'V.250 DTE uses Carriage return as line terminator buf = m_enc.GetBytes(cmd & ControlChars.Cr) strm.Write(buf, 0, buf.Length) strm.Flush() ' Receive; making the somewhat dodgy assumption that the response ' will arrive all at once... buf = New Byte(256) {} Dim numRead As Integer = strm.Read(buf, 0, buf.Length) response = m_enc.GetString(buf, 0, numRead) Console.WriteLine(response) End Sub '====================================================================== ' ' As above, we should add filtering/preferring based on Hint Bits to ' this function. See the C example. ' ' Return: the selected device, or null if no devices were discovered. ' Shared Function selectIrdaPeer(cli As IrDAClient) As IrDADeviceInfo '-------------------------------------------- ' Do the discovery '-------------------------------------------- Dim aDi As IrDADeviceInfo() = cli.DiscoverDevices() if (aDi.Length = 0) return Nothing 'None found End If '--------------------------------------------. ' Display the discovered devices '-------------------------------------------- Console.Write("{0} Devices found" & ControlChars.Lf, aDi.Length) Dim i As Integer = 0 For Each curDi As IrDADeviceInfo In aDi Console.WriteLine("{0}: {1}, addr: {2}, type: {3}", _ i, curDi.DeviceName, curDi.DeviceAddress, curDi.Hints) i = i + 1 'Console.WriteLine(" [curDi.CharacterSet: {0}]", curDi.CharacterSet) Next '-------------------------------------------- ' Select which device '-------------------------------------------- Dim selectNum As Integer = SelectPeerNum 'Do this in your UI... Console.Write("Selected #{0}" & ControlChars.Lf, selectNum) return aDi(selectNum) 'The selected device. End Function End Class End Namespace ' EOF