Question on OpenNetCF Serial Port Demo

J

Jason Jacob

To all,

I've written a demo program using OpenNETCF's Serial classes
but seems that it won't work (at least can't send out data and the
dataReceved delegate is never called)

the source are as below:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;

// -- OpenNETCF
using OpenNETCF;
using OpenNETCF.Drawing;
using OpenNETCF.Windows.Forms;
using OpenNETCF.IO.Serial;
using OpenNETCF.Threading;

namespace SerialPort_Trial
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
#region IdeDefined
private System.Windows.Forms.TextBox mTxt_Incoming;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox mTxt_Outgoing;
private System.Windows.Forms.Button mBtn_Close;
#endregion
#region UserDefined
// -- Serial Port
private OpenNETCF.IO.Serial.Port mPrt_SerialPort = null;
private OpenNETCF.IO.Serial.DetailedPortSettings
mSet_DetailSettings = null;
private System.Windows.Forms.Button mBtn_Connect;
// -- Messages
private string mStr_IncomingMsg = "";
private string mStr_OutgoingMsg = "";
// -- ThreadEx
private OpenNETCF.Threading.ThreadEx mThr_SendThread = null;
private bool mBool_Stop = false;
// -- send count
private int mInt_Quota = 0;
#endregion

public Form1()
{
InitializeComponent();
init ();
//new COM_Client ().Show ();
}

/// <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()
{
//....
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
ApplicationEx.Run(new Form1());
}


protected void init ()
{
try
{
// no handshake........
this.mSet_DetailSettings =
new OpenNETCF.IO.Serial.HandshakeNone
();
this.mPrt_SerialPort =  new Port
("COM1:", this.mSet_DetailSettings);
this.mPrt_SerialPort.InputLen = 1;
this.mPrt_SerialPort.RThreshold = 1;
this.mPrt_SerialPort.SThreshold = 1;

// delegates.....
this.mPrt_SerialPort.DataReceived +=
new
OpenNETCF.IO.Serial.Port.CommEvent
(mPrt_SerialPort_DataReceived);
//this.mPrt_SerialPort.TxDone +=
new
OpenNETCF.IO.Serial.Port.CommEvent
(mPrt_SerialPort_DataReceived);

// thread
this.mThr_SendThread = new ThreadEx
(new System.Threading.ThreadStart
(sendLoop));
this.mThr_SendThread.Start ();

// form
this.Closing += new
CancelEventHandler(Form1_Closing);
}
catch (Exception e)
{
MessageBox.Show ("Error : " +e.ToString ());
}
}

protected void Form1_Closing (object sender,
System.ComponentModel.CancelEventArgs ce)
{
if (this.mPrt_SerialPort.IsOpen)
{
this.mPrt_SerialPort.Close ();
}

if (this.mThr_SendThread != null)
{
this.mBool_Stop = true;
this.mThr_SendThread.Join ();
}
}

private void mBtn_Connect_Click(object sender,
System.EventArgs e)
{
if (!this.mPrt_SerialPort.IsOpen)
{
this.mPrt_SerialPort.Open ();
}
else
{
this.mPrt_SerialPort.Close ();
}
}

// Delegate...
protected void mPrt_SerialPort_DataReceived ()
{
System.Text.Encoding oEnd = System.Text.Encoding.ASCII;
byte[] bMsg;
string sMsg = "";

//bMsg = this.mPrt_SerialPort.Input;
for (int i=0; i<mPrt_SerialPort.Input.Length; i++)
{
sMsg += Convert.ToChar
(this.mPrt_SerialPort.Input[i]);
}

//sMsg = oEnd.GetString (bMsg, 0, bMsg.Length);
this.mStr_IncomingMsg = sMsg;
this.Invoke (new EventHandler (updateIncomingData));
}

protected void updateIncomingData (object sender,
System.EventArgs e)
{
mTxt_Incoming.Text += mStr_IncomingMsg + "\r\n";
mTxt_Incoming.SelectionStart =
mTxt_Incoming.TextLength;
mTxt_Incoming.ScrollToCaret ();
}

protected void sendLoop ()
{
System.Text.Encoding oEnd = System.Text.Encoding.ASCII;
byte[] bMsg;
string sMsg;

while (!this.mBool_Stop)
{
// send data if it's openned
if (this.mPrt_SerialPort.IsOpen)
{
sMsg = "Sending you some data...... "
+ (++mInt_Quota);
bMsg = oEnd.GetBytes (sMsg);
this.mPrt_SerialPort.Output =
new byte[255];
this.mPrt_SerialPort.Output = bMsg;

//this.mPrt_SerialPort_DataReceived ();
}
Thread.Sleep (5000);
}
}

}
}

Is there any logic problem in this code, please help.

Thanks!

From Jason (Kusanagihk)
 
C

Chris Tacke, eMVP

Just as first glance I don't understand this:

this.mPrt_SerialPort.Output = new byte[255];

That would send out 255 bytes of either undefined or zero data (I don't
recall if the byte allocation also zeros it - I wouldn't bet on it).

If you see no data going out, and none seems to be coming it, I'd also
question if your cabling is correct.

-Chris

Jason Jacob said:
To all,

I've written a demo program using OpenNETCF's Serial classes
but seems that it won't work (at least can't send out data and the
dataReceved delegate is never called)

the source are as below:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;

// -- OpenNETCF
using OpenNETCF;
using OpenNETCF.Drawing;
using OpenNETCF.Windows.Forms;
using OpenNETCF.IO.Serial;
using OpenNETCF.Threading;

namespace SerialPort_Trial
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
#region IdeDefined
private System.Windows.Forms.TextBox mTxt_Incoming;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox mTxt_Outgoing;
private System.Windows.Forms.Button mBtn_Close;
#endregion
#region UserDefined
// -- Serial Port
private OpenNETCF.IO.Serial.Port mPrt_SerialPort = null;
private OpenNETCF.IO.Serial.DetailedPortSettings
mSet_DetailSettings = null;
private System.Windows.Forms.Button mBtn_Connect;
// -- Messages
private string mStr_IncomingMsg = "";
private string mStr_OutgoingMsg = "";
// -- ThreadEx
private OpenNETCF.Threading.ThreadEx mThr_SendThread = null;
private bool mBool_Stop = false;
// -- send count
private int mInt_Quota = 0;
#endregion

public Form1()
{
InitializeComponent();
init ();
//new COM_Client ().Show ();
}

/// <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()
{
//....
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
ApplicationEx.Run(new Form1());
}


protected void init ()
{
try
{
// no handshake........
this.mSet_DetailSettings =
new OpenNETCF.IO.Serial.HandshakeNone
();
this.mPrt_SerialPort =  new Port
("COM1:", this.mSet_DetailSettings);
this.mPrt_SerialPort.InputLen = 1;
this.mPrt_SerialPort.RThreshold = 1;
this.mPrt_SerialPort.SThreshold = 1;

// delegates.....
this.mPrt_SerialPort.DataReceived +=
new
OpenNETCF.IO.Serial.Port.CommEvent
(mPrt_SerialPort_DataReceived);
//this.mPrt_SerialPort.TxDone +=
new
OpenNETCF.IO.Serial.Port.CommEvent
(mPrt_SerialPort_DataReceived);

// thread
this.mThr_SendThread = new ThreadEx
(new System.Threading.ThreadStart
(sendLoop));
this.mThr_SendThread.Start ();

// form
this.Closing += new
CancelEventHandler(Form1_Closing);
}
catch (Exception e)
{
MessageBox.Show ("Error : " +e.ToString ());
}
}

protected void Form1_Closing (object sender,
System.ComponentModel.CancelEventArgs ce)
{
if (this.mPrt_SerialPort.IsOpen)
{
this.mPrt_SerialPort.Close ();
}

if (this.mThr_SendThread != null)
{
this.mBool_Stop = true;
this.mThr_SendThread.Join ();
}
}

private void mBtn_Connect_Click(object sender,
System.EventArgs e)
{
if (!this.mPrt_SerialPort.IsOpen)
{
this.mPrt_SerialPort.Open ();
}
else
{
this.mPrt_SerialPort.Close ();
}
}

// Delegate...
protected void mPrt_SerialPort_DataReceived ()
{
System.Text.Encoding oEnd = System.Text.Encoding.ASCII;
byte[] bMsg;
string sMsg = "";

//bMsg = this.mPrt_SerialPort.Input;
for (int i=0; i<mPrt_SerialPort.Input.Length; i++)
{
sMsg += Convert.ToChar
(this.mPrt_SerialPort.Input[i]);
}

//sMsg = oEnd.GetString (bMsg, 0, bMsg.Length);
this.mStr_IncomingMsg = sMsg;
this.Invoke (new EventHandler (updateIncomingData));
}

protected void updateIncomingData (object sender,
System.EventArgs e)
{
mTxt_Incoming.Text += mStr_IncomingMsg + "\r\n";
mTxt_Incoming.SelectionStart =
mTxt_Incoming.TextLength;
mTxt_Incoming.ScrollToCaret ();
}

protected void sendLoop ()
{
System.Text.Encoding oEnd = System.Text.Encoding.ASCII;
byte[] bMsg;
string sMsg;

while (!this.mBool_Stop)
{
// send data if it's openned
if (this.mPrt_SerialPort.IsOpen)
{
sMsg = "Sending you some data...... "
+ (++mInt_Quota);
bMsg = oEnd.GetBytes (sMsg);
this.mPrt_SerialPort.Output =
new byte[255];
this.mPrt_SerialPort.Output = bMsg;

//this.mPrt_SerialPort_DataReceived ();
}
Thread.Sleep (5000);
}
}

}
}

Is there any logic problem in this code, please help.

Thanks!

From Jason (Kusanagihk)
 
J

Jason Jacob

To Chris

The Output = new byte[255] is just my previous testing code, actually,
I will use Encoding.GetBytes ("some string text") instead... But no
matter what, the result is still not working....

About the cabling, I am not sure is it abnormal, I just connect COM1
of my development machine directly to a COM port in the WinCE machine
(through a null modem cable)

But when I use the ActiveSync program, it can't sense any Device
attaching to the development machine (??)

From Jason (Kusanagihk)
 
P

Paul G. Tobey [eMVP]

No ActiveSync indicates that you don't have a fully-connected serial cable.
So, there are some signals that don't get wired from one end to the other.
Connect that same cable to anther PC, run Hyperterminal on both PCs, and see
if you can chat.

Paul T.
 

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