WIRELESS CONTROLLER FOR MOBILE DEVICES Eugene Seah

advertisement
WIRELESS CONTROLLER FOR
MOBILE DEVICES
Eugene Seah
ECE 395 Project Report
14th December 2005
Table of Contents
Abstract
3
Project Description
 Overview
 PIC 16F877A
 Infrared Link Circuit
 Pocket PC
 Controller Signals
4
4
5
6
8
10
References
11
Conclusion / Lessons Learnt
12
Potential for Further Work
13
Appendix
 Hardware List
 Diagrams
 Pocket PC Source Code
14
14
15
18
2
Abstract
The purpose of this project is to create a wireless controller for mobile devices. The
problem this project seeks to solve is the weak user input interface inherent in many
mobile devices today. Many of these mobile devices advertise multimedia features
including games, but the experience of many is that the gaming experience on these
devices are often limited, and far outshone by dedicated gaming handhelds, even though
their hardware capabilities may be comparable. It is the author’s impression that one of
the main reasons for this is the weak user input interface provided on these devices.
This project seeks to address this by interfacing a typical video game console controller
to a mobile device via a wireless link. Button presses on the controller can be read and
encoded into bytes transmitted over a wireless link, which are received by the mobile
device. Given that the mobile device’s operating system has system calls that can be
made to emulate button presses (it seems that most do), a decoding of the bytes can then
determine which button presses are emulated in the mobile device software environment.
The implementation of this project uses IrDA compliant infrared for the wireless link,
and a HP Jornada 568 running Pocket PC 2002 for the mobile device. The result of the
implementation was a step behind completion, with the wireless link between the mobile
device and the IR circuit working, but the transfer and receipt of bytes between the
controller and the Pocket PC incomplete due to time constraints.
The following sections will explain the overall design of the system, with alternative
designs discussed, and completed parts elaborated.
3
Project Description
Overview
The general hardware layout of the system is shown in the diagram below.
PIC
16F877A
Key input
Sega controller
Encoded
Key Data
IrDA
circuit
IrDA
Data
exchange
HP Jornada 568
With IrDA Port
The following sections discuss the design and functionality of each these four hardware
components.
4
PIC 16F877A
This microcontroller unit is central to the functionality of obtaining and encoding
controller data and controlling the IrDA link for transmitting data. The PIC 16F877A was
chosen in particular because of the availability of easy to use sample source code. It also
has a UART which is used to talk to the MCP2140 part.
The microcontroller unit encodes the Sega controller input to some byte of information.
Since the Sega controller uses parallel input for its basic 6 button functionality (up, down,
left, right, b, c), this is relatively simple. The encoded byte would just have a bit assigned
to each of these buttons, with a 1 value denoting the button is pressed, and a 0 value
denoting the button is not pressed.
The byte would look like this : “00 Up Down Left Right B C”
Another functionality that would need to written in the PIC code would be the frequency
of probing the controller values and transmitting the IR codes. The higher the frequency,
the more responsive the controller would likely be, but the transmission rate would have
to be within the 9600 bps provided by the IrDA link. The software running mobile device
would also require more CPU time if it was constantly decoding IrDA packets and
emulating button presses. This would be accomplished by using timers to trigger
interrupts for handling these operations. The ideal transmit rate was not determined due
to time constraint, but some trial and error tests for controller responsiveness and
program responsiveness would have likely made finding a good value easy to
accomplish.
The microcontroller unit also has to probe the IrDA link to determine whether to send
signals to the MCP2140 part via its universal asynchronous receiver-transmitter port
(UART). This UART is set at 9600 baud as well to synchronize with the requirements of
the MCP2140. The detailed state machine for the microcontroller to handle flow control
is shown in the appendix. However, it is likely that this flow control would not be
needed, as the amount of lost controller input data in the time it takes for the MCP2140’s
receive buffer to empty out is probably so insignificant that it would not be noticed.
5
Infrared Link Circuit
The infrared link circuit receives serial data from the microcontroller, which it then
transmits via infrared to the pocket PC.
Originally, the option of using raw infrared to transmit data was considered, but it was
realized that programming the Pocket PC to decode raw infrared codes would be a huge
hassle, and so IrDA (which was supported in the .NET Compact Framework) was
preferred over the raw IR option.
This circuit consists of two main components, the MCP2140 and the TFDU4100 parts.
The MCP2140 is an IrDA Stack Controller, which means it handles the complexity of the
IrDA standard within the IC, and allows users to send simple serial data via the host
UART and still achieve IrDA functionality.
The TFDU4100 is an IrDA Transceiver, which is an integrated chip that consists of an IR
LED and a photodiode, and this handles the actual receiving and transmitting of IR to and
from the mobile device.
One of the main problems faced in terms of researching and implementing the project
was the IR transceiver. Because I had little circuit experience, my search for circuits that
would function as IR transmitters and IR receivers was extremely confusing. Looking up
these circuits turned up multiple designs. What I did not understand was how they
differed, and so was unsure of which would work in my application. Some of the factors
that varied were the frequency of the infrared signal and the types of output produced by
these circuits.
What came in useful in sorting through this confusion was a reference circuit that I found
for attaching an integrated optical transceiver to the MCP2140. I was able to purchase
the recommended TFDU4100 transceiver part from Mouser Electronics to continue
making progress on this infrared link. However, the reference circuit also cited a part
named MMUN2111LT1, which I could not obtain. This part was a Bias Resistor
Transistor, and was used as a simple pulse-width modulator to match the MCP2140 RX
pin’s requirement on pulse width.
With my limited knowledge of transistors, I blindly thought to replace this simply with a
transistor of the same beta value to obtain the same functionality. There was little wonder
that my infrared circuit did not work at this stage. From much further research, I realized
all the other factors I had to consider in order to try and replace the part.
This document at the following link came in very useful in this process.
(http://www1.jaycar.com.au/images_uploaded/transubs.pdf)
I finally settled on the 2N2862 transistor part as a close enough match. Based on logic
analyzer outputs, I was able to determine that the pulse width modulation was indeed
6
working. However, the output of the circuit was inverted from the expected input at the
RX pin of the MCP2140. I thought to use a simple TTL inverter but realized later from
logic analyzer outputs that the voltage ranges of the original output violated TTL voltage
ranges. I thus used a 74HCT04 CMOS to TTL inverter to solve my problem. Now the
logic analyzer output showed the right format of the pulses, but the noise from the circuit
caused blips in the pulse that likely affected the connection process, as a logic one was
not held entirely for the pulse width required. I eventually solved this by lowering the
reference voltage input to the MCP2140 by adding an extra 4.7k ohm resistor before the
RXPDDEF pin (see circuit diagram in appendix), resulting in clearer logic levels for the
chip.
All these fixes and solutions resulted in a fully working infrared link at the end of the
project.
7
Pocket PC
The choice made in the mobile device for my project was entirely based on availability. I
managed to check out a HP Jornada 568 from the ECE department, which meant
avoiding having to purchase a PDA/cellphone for this project. However, this also took
away the option of using Bluetooth for the wireless link, as the PDA provided was
relatively old.
The Pocket PC operating system running on the Jornada 568 had an advantage of being
capable of running the .NET Compact Framework, which meant being able to program
using fairly high level system calls and APIs. Given the short time frame of the project,
and the focus on hardware implementation rather than software, I didn’t want to spend
too much time writing software instead of working on the hardware.
One problem though was my unfamiliarity with either the .NET Framework, or basic
Windows CE/Pocket PC programming. It was very confusing trying to find example code
for a particular purpose and find it written in Basic, C#, and C++ all at once. This meant
that I had to spend quite a bit of time doing background reading on the Windows
CE/Pocket PC programming and the .NET Framework to figure out what language I
would be best working with. This turned out to be C#, as its syntax was very C++-like,
but I would be able to avoid meddling with Win32 API calls (which are known to be nontrivial to learn and debug). It also had the ability to still make Win32 API calls if
necessary (needed by the button emulation system call) by using what is called PInvoke
to call from managed code to unmanaged code.
The software on this mobile device would need to be able to do three things
1) Connect to the MCP2140 device and open a IrDA socket for data transfer
2) Map data bytes received to emulated button presses
3) Forward these emulated button presses globally to the OS or directly to a chosen
application (i.e. these button presses should not only be caught and handled
locally in this application but rather used in another application, like a game)
My approach to this was to research the three functionalities quite intensively at the
beginning of the semester, as I didn’t want to find out late in the project that some
limitation in the OS would result in the project being impossible to complete. I managed
to get the second functionality working quite early, and made an early demo of it which
remains in my final submitted application. This used the keybd_event call found in the
coredll.dll library in Pocket PCs.
Testing the first functionality proved to be difficult without having built my circuit first,
and this slowed the software process quite a bit. I had hoped to try and test the socket
calls directly with a PC (with IrDA) at first, but there was no application that I could
easily use on my Windows PC to do this without having to spend time writing one myself
to do this. I did however managed to test that my code discovered my laptop computer
over IrDA and displayed its name correctly, and this proved helpful.
8
Due to the amount of time spent on testing the IR link, I was not able to test the last
functionality of running in the background and forwarding the keypresses globally.
Suggested applications which did this similarly (eg. Wireless keyboards) indicated that
this was very much possible and would have been relatively easy to implement.
Source code for my application is provided in the Appendix.
9
Controller Signals
The controller I chose to use for this project was the Sega Genesis 6-button controller.
From some quick research, I found that this was actually a good choice in terms of ease
of implementation, as the Sega Controller actually has 6 parallel button pins, enough for
simple functionality of the four directional buttons and 2 trigger buttons (B and C)
without having to send data serially. How the Sega controller outputs the other 5 buttons (
X, Y, Z, A and Start) is by using the select signal, which has its own other pin on the DB9 connector, and this essentially multiplexes the signals. However, some complexity
holds due to the need for Sega to make this controller compatible with its earlier 3-button
controller, and so only A and Start are multiplexed in this manner, and a more complex
timing scheme is used to detect X, Y and Z button presses.
The details of the workings of the controller are very well documented in this text online
by author Charles Rosenberg. (http://www.cs.cmu.edu/~chuck/infopg/segasix.txt)
Since the Pocket PC platform only has a few hardware buttons anyway, I decided to stick
with the basic 6-button functionality by just inputting these 6 parallel output pins to the
PIC microcontroller for use. Each of the button pins has a voltage level that is pulled low
when the button is pressed, and kept high when it is not pressed, making the wiring of the
controller fairly simple. I just got a DB-9 solder adapter to wire up the controller pins, so
I could avoid damaging my controller.
10
References
1) www.microchip.com provided the datasheets required for the PIC16F877A, as well as
the MCP2140. Several reference designs, and errata sheets are also included, listed below
–
–
–
–
http://ww1.microchip.com/downloads/en/DeviceDoc/21790a.pdf
http://ww1.microchip.com/downloads/en/DeviceDoc/51487a.pdf
http://ww1.microchip.com/downloads/en/DeviceDoc/21883A.pdf
http://ww1.microchip.com/downloads/en/AppNotes/00756a.pdf
2) 74HCT04 datasheet
– http://www.futurlec.com/74HCT/74HCT04.shtml
3) MSDN Library had example codes to base my Pocket PC sources from.
– http://msdn.microsoft.com/library/default.asp?url=/library/enus/dnanchor/html/mobileembedded.asp
– http://msdn2.microsoft.com/en-us/library/y055sf11(en-US,VS.80).aspx (IRDA
Client Class)
– http://msdn.microsoft.com/library/default.asp?url=/library/enus/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputrefe
rence/keyboardinputfunctions/keybd_event.asp ( keybd_event function)
4) Charles Rosenberg Sega 6-button controller reference
– http://www.cs.cmu.edu/~chuck/infopg/segasix.txt
5) ECE 445 PIC Tutorial
– http://courses.ece.uiuc.edu/ece445/howtos/pic/
6) Choosing a replacement transistor
– http://www1.jaycar.com.au/images_uploaded/transubs.pdf
11
Conclusion / Lessons Learnt
I managed to get an infrared link established between the Pocket PC and my MCP2140
IC chip, shown on demo day indicated by the DSR status LED on pin 10 of the
MCP2140. I also managed to write the software needed to try and connect on IrDA and
send data. I researched and made use of the controller to output LCD display (by
modifying the provided TEST board code) of button presses via the PIC 16F877A. The
only final functionality missing was the PIC transactions over the UART that would send
data from the controller port to the MCP 2140 for infrared transmission.
This project was extremely educational, and I learnt many skills despite only getting part
of the intended functionality complete.
The skills I felt I had learnt or improved on in this semester:
1) Practical circuit skills
– Having taken mostly Computer Engineering courses over my education, I had
forgotten most of my circuit knowledge, and had little experience using it. I learnt
about decoupling capacitors, the use of transistors, reading resistor and capacitor
values, and many other things that will come in useful for hobby projects in
future. It is very useful to be able to think outside of just binary 1s and 0s for
practical applications of hardware.
2) .NET and C# programming
- Doing the background reading for this project and learning about organization of
code in .NET between managed and unmanaged code, as well as learning the
syntax of C# for my Pocket PC programming will come in useful in future.
3) Importance of getting the most important part of the project done first
- I had worked on the peripheral parts of the project first, leaving the IR link for
later because I had not envisioned it to be as challenging as it was. This put a
serious constraint on my time towards the end of the semester.
4) Reading and finding datasheets and documentation
- The many different example designs and parts used meant that I had to learn to be
apt in obtaining information from datasheets to serve the purpose of my project.
In this sense, while I was dissatisfied with not being able to complete the functionality of
my project, I was very pleased with the skills I took away from it, and would definitely
be much better at any further projects of this nature.
12
Potential for Further Work
As envisioned in my project proposal, the wireless controller could be quite marketable if
miniaturized to a size that would be easy to attach to particular mobile devices. Further
work could be done to design such a form factor, and have power supplied by a battery
instead.
Since newer devices are very often including Bluetooth and Wifi together with or instead
of IrDA, the device could be made to handle these various protocols as well, choosing
between the protocols used based on the type of signals received in the connection
process.
A more complete software suite can also be worked on, so that users can customize the
hardware buttons emulated and reconfigure settings, even possibly change the rate at
which the controller is probed.
13
Appendix
Final Hardware List
– 1 x MCP2140
– 1 x PIC 16F877A
– 1 x Sega Genesis 6-button controller
– 1 x HP Jornada 568 PDA
– 1 x 2N2862 PNP 100mA switching transistor
– 1 x 74HCT04 Hex Inverter (CMOS to TTL)
14
Diagrams
Pin
Name (Select=GND)
1
Up
2
Down
3
Gnd / Left
4
Gnd / Right
5
+5VDC
6
Button A
7
Select
8
Ground
9
Start
Sega DB-9 Pin Out
Name (Select=+5V)
Up
Down
+5VDC
Button B
Select
Ground
Button C
Recommended Circuit for MCP2140 to TFDU 4100
(Note: RXPDREF does not have 4.7k ohm in series vs final circuit, and MMUN2111LT1 part replaced with
bias resistor circuit and 2N2862 part)
15
Flowchart for UART Flow Control
16
Circuit Diagram for System on Protoboard
17
Source Code for Pocket PC Program
using
using
using
using
using
using
using
using
using
System;
System.Drawing;
System.Collections;
System.Windows.Forms;
System.Data;
System.Runtime.InteropServices;
System.Net.Sockets; // for IrDA classes
System.Net; // for EndPoint class
System.IO;
namespace SmartDeviceApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button leftButton;
private System.Windows.Forms.Button bButton;
private System.Windows.Forms.Button cButton;
private System.Windows.Forms.Button upButton;
private System.Windows.Forms.Button rightButton;
private System.Windows.Forms.Button downButton;
private System.Windows.Forms.Label mainLabel;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox irCodeBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.MainMenu mainMenu1;
//IRDA classes
private IrDAListener irListen;
private IrDAClient irClient;
private IrDAEndPoint irEndP;
private IrDADeviceInfo[] irDevices;
private System.Windows.Forms.Button connect;
private System.Windows.Forms.Label statuslabel;
private System.Windows.Forms.Button discover;
string irServiceName;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
irClient = new IrDAClient();
18
//test windows irda:ircomm
irServiceName = "IrDA:IrCOMM"; //"IrDAFtp";
}
/// <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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.leftButton = new System.Windows.Forms.Button();
this.bButton = new System.Windows.Forms.Button();
this.cButton = new System.Windows.Forms.Button();
this.upButton = new System.Windows.Forms.Button();
this.rightButton = new System.Windows.Forms.Button();
this.downButton = new System.Windows.Forms.Button();
this.mainLabel = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.irCodeBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.discover = new System.Windows.Forms.Button();
this.connect = new System.Windows.Forms.Button();
this.statuslabel = new System.Windows.Forms.Label();
//
// leftButton
//
this.leftButton.Location = new System.Drawing.Point(16, 88);
this.leftButton.Size = new System.Drawing.Size(24, 20);
this.leftButton.Text = "<";
this.leftButton.Click += new
System.EventHandler(this.leftButton_Click);
//
// bButton
//
this.bButton.Location = new System.Drawing.Point(136, 88);
this.bButton.Size = new System.Drawing.Size(24, 20);
this.bButton.Text = "B";
this.bButton.Click += new System.EventHandler(this.bButton_Click);
//
// cButton
//
this.cButton.Location = new System.Drawing.Point(176, 88);
this.cButton.Size = new System.Drawing.Size(24, 20);
this.cButton.Text = "C";
this.cButton.Click += new System.EventHandler(this.cButton_Click);
//
// upButton
//
19
this.upButton.Location = new System.Drawing.Point(40, 64);
this.upButton.Size = new System.Drawing.Size(24, 20);
this.upButton.Text = "^";
this.upButton.Click += new System.EventHandler(this.button5_Click);
//
// rightButton
//
this.rightButton.Location = new System.Drawing.Point(64, 88);
this.rightButton.Size = new System.Drawing.Size(24, 20);
this.rightButton.Text = ">";
this.rightButton.Click += new
System.EventHandler(this.rightButton_Click);
//
// downButton
//
this.downButton.Location = new System.Drawing.Point(40, 112);
this.downButton.Size = new System.Drawing.Size(24, 20);
this.downButton.Text = "V";
this.downButton.Click += new
System.EventHandler(this.downButton_Click);
//
// mainLabel
//
this.mainLabel.Location = new System.Drawing.Point(24, 32);
this.mainLabel.Size = new System.Drawing.Size(184, 32);
this.mainLabel.Text = "Push buttons to simulate button presses in
other programs";
this.mainLabel.ParentChanged += new
System.EventHandler(this.label1_ParentChanged);
//
// textBox1
//
this.textBox1.AcceptsReturn = true;
this.textBox1.Location = new System.Drawing.Point(24, 136);
this.textBox1.Multiline = true;
this.textBox1.Size = new System.Drawing.Size(160, 40);
this.textBox1.Text = "This is test text that can be scrolled over ";
this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
//
// irCodeBox
//
this.irCodeBox.Location = new System.Drawing.Point(88, 248);
this.irCodeBox.Size = new System.Drawing.Size(96, 22);
this.irCodeBox.Text = "";
this.irCodeBox.TextChanged += new
System.EventHandler(this.textBox2_TextChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 248);
this.label1.Size = new System.Drawing.Size(56, 20);
this.label1.Text = "IR Code:";
//
// discover
//
this.discover.Location = new System.Drawing.Point(24, 192);
20
this.discover.Text = "discover";
this.discover.Click += new System.EventHandler(this.button1_Click);
//
// connect
//
this.connect.Location = new System.Drawing.Point(128, 192);
this.connect.Text = "connect";
this.connect.Click += new System.EventHandler(this.connect_Click);
//
// statuslabel
//
this.statuslabel.Location = new System.Drawing.Point(24, 224);
this.statuslabel.Text = "status text";
//
// Form1
//
this.BackColor = System.Drawing.Color.Silver;
this.ClientSize = new System.Drawing.Size(234, 280);
this.Controls.Add(this.statuslabel);
this.Controls.Add(this.connect);
this.Controls.Add(this.discover);
this.Controls.Add(this.label1);
this.Controls.Add(this.irCodeBox);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.mainLabel);
this.Controls.Add(this.downButton);
this.Controls.Add(this.rightButton);
this.Controls.Add(this.upButton);
this.Controls.Add(this.cButton);
this.Controls.Add(this.bButton);
this.Controls.Add(this.leftButton);
this.Location = new System.Drawing.Point(0, 90);
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void label1_ParentChanged(object sender, System.EventArgs e)
{
}
private const byte KEYEVENTF_KEYDOWN = 0x01;
private const byte KEYEVENTF_SILENT = 0x02;
private const byte KEYEVENTF_KEYUP = 0x04;
21
private
private
private
private
const
const
const
const
byte
byte
byte
byte
VK_UP = 0x26;
VK_DOWN = 0x28;
VK_LEFT = 0x25;
VK_RIGHT = 0x27;
[DllImport("coredll.dll")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
uint dwExtraInfo);
private void rightButton_Click(object sender, System.EventArgs e)
{
this.textBox1.Focus();
keybd_event(VK_RIGHT,0x45,KEYEVENTF_KEYDOWN, 0);
//keybd_event(VK_RIGHT,0x45,KEYEVENTF_KEYUP, 0);
}
private void downButton_Click(object sender, System.EventArgs e)
{
this.textBox1.Focus();
keybd_event(VK_DOWN,0,KEYEVENTF_KEYDOWN, 0);
//keybd_event(VK_DOWN,0,KEYEVENTF_KEYUP|KEYEVENTF_SILENT, 0);
}
private void button5_Click(object sender, System.EventArgs e)
{
this.textBox1.Focus();
keybd_event(VK_UP,0,KEYEVENTF_KEYDOWN, 0);
//keybd_event(VK_UP,0,KEYEVENTF_KEYUP|KEYEVENTF_SILENT, 0);
}
private void bButton_Click(object sender, System.EventArgs e)
{
this.textBox1.Focus();
keybd_event(0xd1,0,KEYEVENTF_KEYDOWN, 0);
//keybd_event(0xd1,0,KEYEVENTF_KEYUP|KEYEVENTF_SILENT, 0);
}
private void cButton_Click(object sender, System.EventArgs e)
{
this.textBox1.Focus();
keybd_event(0xd2,0,KEYEVENTF_KEYDOWN, 0);
//keybd_event(0xd2,0,KEYEVENTF_KEYUP|KEYEVENTF_SILENT, 0);
}
private void leftButton_Click(object sender, System.EventArgs e)
{
this.textBox1.Focus();
keybd_event(VK_LEFT,0,KEYEVENTF_KEYDOWN, 0);
//keybd_event(VK_LEFT,0,KEYEVENTF_KEYUP|KEYEVENTF_SILENT, 0);
}
//try to connect to irda service
private void connect_Click(object sender, System.EventArgs e)
{
// Create an IrDA client with a service name that must
22
// match the service name of the receiving IrDA client.
NetworkStream byteStream;
Socket m_sock;
// connect using 9-wire mode to MCP 2140
try
{
System.Net.IrDAEndPoint endP = new
System.Net.IrDAEndPoint(irDevices[0].DeviceID, "IrDA:IrCOMM");
m_sock = new Socket(AddressFamily.Irda, SocketType.Stream,
ProtocolType.Unspecified);
m_sock.SetSocketOption((SocketOptionLevel)0x00FF,(SocketOptionName)0x00000016,1);
m_sock.Connect(endP);
//name = irDevices[0].DeviceName.ToString();
}
catch(SocketException exS)
{
MessageBox.Show("Create socket error: " + exS.Message );
return;
}
// Initialize NetworkStream
try
{
byteStream = new NetworkStream(m_sock, FileAccess.ReadWrite,
true);
}
catch (Exception exc)
{
MessageBox.Show("NetworkStream init: " + exc.Message );
return;
}
byte[] readByte = new byte[1];
//try reading byte from stream
if(byteStream.CanRead)
{
int numberOfBytesRead = 0;
// Incoming message may be larger than the buffer size.
if (byteStream.DataAvailable)
{
MessageBox.Show("byteStream Data Available");
numberOfBytesRead = byteStream.Read(readByte, 0, 1);
// Print out the received message to the console.
MessageBox.Show("Received " +
System.Convert.ToString(readByte));
}
23
else
{
MessageBox.Show("byteStream Data NOT Available, blocking
read call may fail");
return;
}
}
else
{
MessageBox.Show("Can't read byteStream");
}
}
// Discover Button
private void button1_Click(object sender, System.EventArgs e)
{
// Create a collection of a maximum of three devices.
irDevices = irClient.DiscoverDevices(2);
// Show a message if no devices are found.
if (irDevices.Length == 0)
{
MessageBox.Show("No remote infrared devices found!");
return;
}
else
{
MessageBox.Show("Found " + irDevices.Length.ToString() +
"devices, device 0 name is " + irDevices[0].DeviceName.ToString());
}
// display device info
foreach(IrDADeviceInfo irDevice in irDevices)
{
//ID = BitConverter.ToInt32(irDevice.DeviceID, 0);
//device = ID.ToString() + " " + irDevice.DeviceName + " "
//+ irDevice.CharacterSet + " " + irDevice.Hints;
//listBox1.Items.Add(device);
}
}
}
}
24
Download