IrDA, IRComm and Timers...

M

Markus Stoeckli

I try to interface an embedded device with an iPAQ running PPC2002 over IrDA
(C#, VS7). The embedded device uses a MCP2150 controller which handles the
IRComm protocol. With the iPAQ I want to periodically poll status data from
the device. This is done by sending a request string which is answered by
the device. My strategy is to send the request and the answer will trigger a
receive event. After this event, the next request is send. A timer is used
to check for a broken connection and will re-initiate the connection if
needed. So far so good. The example code (see below) works fine for a while,
but then no more events are triggered (no receive and timer) and the
application hangs.

My questions:

1. is this a good way of handling this communication or is there a better
solution
2. how can it happen that the timer stops firing

Thanks,

Markus




using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using CFSerialClass.SerialIO;
using System.Threading;
namespace CSharpExample
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtTerm;
private System.Windows.Forms.Timer Watchdog;
private CFSerialClass.SerialIO.SerialPort irPort;
private int irStep;
private int irTimer;
private System.Windows.Forms.Timer timer1;
private int irCount;
private int irSubCount;
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.txtTerm = new System.Windows.Forms.TextBox();
this.Watchdog = new System.Windows.Forms.Timer();
this.timer1 = new System.Windows.Forms.Timer();
//
// txtTerm
//
this.txtTerm.Location = new System.Drawing.Point(5, 46);
this.txtTerm.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtTerm.Size = new System.Drawing.Size(77, 22);
this.txtTerm.Text = "";
this.txtTerm.TextChanged += new
System.EventHandler(this.txtTerm_TextChanged);
//
// Watchdog
//
this.Watchdog.Enabled = true;
this.Watchdog.Interval = 1000;
this.Watchdog.Tick += new System.EventHandler(this.Watchdog_Tick);
//
// Form1
//
this.Controls.Add(this.txtTerm);
this.MaximizeBox = false;
this.MinimizeBox = false;
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 Form1_Load(object sender, System.EventArgs e)
{
irPort = new SerialPort();
irPort.CommPort = 3;
irPort.BitRate = 9600;
irPort.DataBits = 8;
irPort.RTSEnable = true;
irPort.DTREnable = true;
irPort.StopBits = 1;
irPort.EnableOnComm = true;
irPort.PortOpen = true;
irPort.OnComm += new SerialPort.OnCommEventHandler(dataReceived);
irTimer = 10;
irStep = 1;
irCount = 0;
irSubCount = 0;
irCom("1");
}
private void Form1_Closing(object sender, System.EventArgs e)
{
if (irPort.PortOpen == true) irPort.PortOpen = false;
}
private void dataReceived()
{
string inputData;
// read input text
inputData = irPort.InputString();
// display it
irCount++;
irSubCount ++;
if (irSubCount == 10)
{
txtTerm.Text = irCount.ToString() + ": " + inputData;
irSubCount = 0;
Thread.Sleep (0);
}
irCom (inputData);
}

private void txtTerm_TextChanged(object sender, System.EventArgs e)
{
}
private void irCom (string inputData)
{
irTimer=2;
if (inputData == "") irStep = 0;
switch (irStep)
{
case 0:
irPort.PortOpen = false;
irPort.PortOpen = true;
goto case 1;
irStep++;
break;
case 1:
irPort.Output ("A");
irStep++;
irTimer=10;
break;
case 2:
irPort.Output ("A");
break;
}
}
private void Watchdog_Tick(object sender, System.EventArgs e)
{
-- irTimer;
if (irTimer==0) irCom ("");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top