Re: .NET CF中的红外通讯

自主开发软件发布区

.NET CF中的红外通讯


被遗弃的小指 2005-12-08, 11:17

前两天去CSIP讲课,教材中有一个红外通讯的例子,觉得不错,也简单易懂。就贴出来,大家自己看看吧~

 

Re: .NET CF中的红外通讯


被遗弃的小指 2005-12-08, 11:18

我倒。。。。上传不了附件了?

C#代码:

using System; using System.Drawing; using System.Collections; using System.Windows.Forms; using System.Data; using System.Net; using System.Net.Sockets; using System.Text; namespace IrDATextTransmitter { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { internal System.Windows.Forms.TextBox sendReceiveTextBox; internal System.Windows.Forms.MenuItem exitMenuItem; internal System.Windows.Forms.MenuItem MenuItem1; internal System.Windows.Forms.Button receiveButton; internal System.Windows.Forms.Button sendButton; internal System.Windows.Forms.MainMenu MainMenu1; private string serviceName = "IRDA_SOCKET_CONNECTION"; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.sendReceiveTextBox = new System.Windows.Forms.TextBox(); this.exitMenuItem = new System.Windows.Forms.MenuItem(); this.MenuItem1 = new System.Windows.Forms.MenuItem(); this.receiveButton = new System.Windows.Forms.Button(); this.sendButton = new System.Windows.Forms.Button(); this.MainMenu1 = new System.Windows.Forms.MainMenu(); // // sendReceiveTextBox // this.sendReceiveTextBox.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular); this.sendReceiveTextBox.Location = new System.Drawing.Point(72, 48); this.sendReceiveTextBox.Text = ""; // // exitMenuItem // this.exitMenuItem.Text = "Exit"; this.exitMenuItem.Click += new System.EventHandler(this.exitMenuItem_Click); // // MenuItem1 // this.MenuItem1.MenuItems.Add(this.exitMenuItem); this.MenuItem1.Text = "File"; // // receiveButton // this.receiveButton.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular); this.receiveButton.Location = new System.Drawing.Point(88, 152); this.receiveButton.Text = "Receive"; this.receiveButton.Click += new System.EventHandler(this.receiveButton_Click); // // sendButton // this.sendButton.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular); this.sendButton.Location = new System.Drawing.Point(88, 112); this.sendButton.Text = "Send"; this.sendButton.Click += new System.EventHandler(this.sendButton_Click); // // MainMenu1 // this.MainMenu1.MenuItems.Add(this.MenuItem1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; // // Form1 // this.BackColor = System.Drawing.Color.White; this.ClientSize = new System.Drawing.Size(240, 270); this.Controls.Add(this.receiveButton); this.Controls.Add(this.sendButton); this.Controls.Add(this.sendReceiveTextBox); this.Menu = this.MainMenu1; this.Text = "Form1"; } #endregion /// <summary> /// The main entry point for the application. /// </summary> static void Main() { Application.Run(new Form1()); } private byte[] WaitToReadIRData() { byte[] bytesRead = new byte[128]; IrDAListener listener = new IrDAListener(serviceName); IrDAClient client = null; System.IO.Stream stream = null; try { listener.Start(); client = listener.AcceptIrDAClient(); // blocking call stream = client.GetStream(); stream.Read(bytesRead, 0, 128); } finally { if (stream != null) { stream.Close(); } if (client != null) { client.Close(); } listener.Stop(); } return bytesRead; } private void SendIRData(int numRetries, byte[] buffer, int bufferLen) { IrDAClient client = null; int currentTries = 1; sendButton.Enabled = false; do { try { client = new IrDAClient(serviceName); } catch (SocketException se) { if (currentTries > numRetries) { throw se; } else { MessageBox.Show("Peer is not in listening mode. Enable listening mode by clicking the Receive button", "Attempt " + currentTries.ToString() + " of " + numRetries.ToString()); } } catch (Exception) { MessageBox.Show("Connection not found. Align infrared ports and try again", "Attempt " + currentTries.ToString() + " of " + numRetries.ToString()); } currentTries = currentTries + 1; } while ((client == null) && (currentTries - 1 < numRetries)); if (client == null) { // timeout occurred throw new SocketException(); } System.IO.Stream stream = null; try { stream = client.GetStream(); stream.Write(buffer, 0, bufferLen); } finally { if (stream != null) { stream.Close(); } client.Close(); } sendButton.Enabled = true; } private void receiveButton_Click(object sender, System.EventArgs e) { byte[] readBuf; string readString; receiveButton.Enabled = false; // this is a blocking call readBuf = WaitToReadIRData(); readString = ConvertBytesToString(readBuf); sendReceiveTextBox.Text = readString; receiveButton.Enabled = true; } private void sendButton_Click(object sender, System.EventArgs e) { string stringValue = sendReceiveTextBox.Text; byte[] byteBuffer; byteBuffer = ConvertStringToBytes(stringValue, stringValue.Length); SendIRData(5, byteBuffer, stringValue.Length); } private void exitMenuItem_Click(object sender, System.EventArgs e) { Application.Exit(); } private byte[] ConvertStringToBytes(string stringValue, int stringLength) { char charValue; int counter; byte[] byteBuffer = new byte[stringLength]; for (counter = 0; counter < stringLength; counter++) { charValue = stringValue[counter]; byteBuffer[counter] = Convert.ToByte(charValue); } return byteBuffer; } private string ConvertBytesToString(byte[] byteBuffer) { StringBuilder sb = new StringBuilder(); int counter; for (counter = 0; counter < byteBuffer.Length; counter++) { sb.Append(Convert.ToChar(byteBuffer[counter])); } return sb.ToString(); } } }

VB.NET代码:

Imports System.Net Imports System.Net.Sockets Imports System.Text Public Class Form1 Inherits System.Windows.Forms.Form Friend WithEvents sendReceiveTextBox As System.Windows.Forms.TextBox Friend WithEvents sendButton As System.Windows.Forms.Button Friend WithEvents receiveButton As System.Windows.Forms.Button Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Private serviceName As String = "IRDA_SOCKET_CONNECTION" #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) MyBase.Dispose(disposing) End Sub 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents exitMenuItem As System.Windows.Forms.MenuItem Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.exitMenuItem = New System.Windows.Forms.MenuItem Me.sendReceiveTextBox = New System.Windows.Forms.TextBox Me.sendButton = New System.Windows.Forms.Button Me.receiveButton = New System.Windows.Forms.Button ' 'MainMenu1 ' Me.MainMenu1.MenuItems.Add(Me.MenuItem1) ' 'MenuItem1 ' Me.MenuItem1.MenuItems.Add(Me.exitMenuItem) Me.MenuItem1.Text = "File" ' 'exitMenuItem ' Me.exitMenuItem.Text = "Exit" ' 'sendReceiveTextBox ' Me.sendReceiveTextBox.Location = New System.Drawing.Point(72, 48) Me.sendReceiveTextBox.Text = "" ' 'sendButton ' Me.sendButton.Location = New System.Drawing.Point(88, 112) Me.sendButton.Text = "Send" ' 'receiveButton ' Me.receiveButton.Location = New System.Drawing.Point(88, 152) Me.receiveButton.Text = "Receive" ' 'Form1 ' Me.Controls.Add(Me.receiveButton) Me.Controls.Add(Me.sendButton) Me.Controls.Add(Me.sendReceiveTextBox) Me.Menu = Me.MainMenu1 Me.Text = "IrDA Text Transmitter" End Sub #End Region Private Sub ReceiveData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles receiveButton.Click Dim readBuf() As Byte Dim readString As String receiveButton.Enabled = False ' this is a blocking call readBuf = WaitToReadIRData() readString = ConvertBytesToString(readBuf) sendReceiveTextBox.Text = readString receiveButton.Enabled = True End Sub Private Function WaitToReadIRData() As Byte() Dim bytesRead(128) As Byte Dim listener As IrDAListener = New IrDAListener(serviceName) Dim client As IrDAClient = Nothing Dim stream As System.IO.Stream = Nothing Try listener.Start() client = listener.AcceptIrDAClient() ' blocking call stream = client.GetStream() stream.Read(bytesRead, 0, 128) Finally If (Not stream Is Nothing) Then stream.Close() End If If (Not client Is Nothing) Then client.Close() End If listener.Stop() End Try Return bytesRead End Function Private Sub SendIRData(ByVal numRetries As Integer, ByVal buffer() As Byte, ByVal bufferLen As Integer) Dim client As IrDAClient = Nothing Dim currentTries As Integer = 1 sendButton.Enabled = False Do Try client = New IrDAClient(serviceName) Catch se As SocketException If (currentTries > numRetries) Then Throw se Else MessageBox.Show("Peer is not in listening mode. Enable listening mode by clicking the Receive button", "Attempt " & currentTries & " of " & numRetries) End If Catch e As Exception MessageBox.Show("Connection not found. Align infrared ports and try again", "Attempt " & CurrentTries & " of " & NumRetries) End Try currentTries = currentTries + 1 Loop While client Is Nothing And currentTries - 1 < numRetries If (client Is Nothing) Then 'timeout occurred Throw New SocketException End If Dim stream As System.IO.Stream = Nothing Try stream = client.GetStream() stream.Write(buffer, 0, bufferLen) Finally If (Not stream Is Nothing) Then stream.Close() End If client.Close() End Try sendButton.Enabled = True End Sub Private Sub sendButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendButton.Click Dim stringValue As String = sendReceiveTextBox.Text Dim byteBuffer() As Byte byteBuffer = ConvertStringToBytes(stringValue, stringValue.Length) SendIRData(5, byteBuffer, stringValue.Length) End Sub Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click Application.Exit() End Sub Private Function ConvertStringToBytes(ByVal stringValue As String, ByVal stringLength As Integer) As Byte() Dim charValue As Char Dim counter As Integer Dim byteBuffer(stringLength) As Byte For counter = 0 To stringLength - 1 charValue = stringValue.Chars(counter) byteBuffer(counter) = Convert.ToByte(charValue) Next Return byteBuffer End Function Private Function ConvertBytesToString(ByVal byteBuffer() As Byte) As String Dim sb As New StringBuilder Dim counter As Integer For counter = 0 To byteBuffer.Length - 1 sb.Append(Convert.ToChar(byteBuffer(counter))) Next Return sb.ToString End Function End Class

Re: .NET CF中的红外通讯


被遗弃的小指 2005-12-08, 11:22
使用方式:
1、编译好之后,部署到两台PPC上面,并运行起来。
2、将两台PPC红外对准。
3、按下A机的receive按钮
4、在B机的TextBox中输入字符串,点击Send
5、A机可以收到B发出的字符串
6、反之亦可。

Re: .NET CF中的红外通讯


xxxcyy 2005-12-13, 16:03
有单机使用obex的例子吗?谢谢
Copy Left WinFans(R)

Powered by Community Server Powered by CnForums.Net