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:
Is there any logic problem in this code, please help.
Thanks!
From Jason (Kusanagihk)
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)