CSharp – Comunicacion PPC ActiveSync con openNETCF

Written by lopezatienza on 18/12/2008 – 13:06 -

A continuación muestro el código de un ejemplo de Comunicación con un PPC mediante ActiveSync con openNETCF

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using OpenNETCF.Desktop.Communication;

using System.IO;

 

namespace OpenNETCF.Samples

{

         /// <summary>

         /// Summary description for Form1.

         /// </summary>

         public class RAPICSharp : System.Windows.Forms.Form

         {

                   private System.Windows.Forms.GroupBox groupBox1;

                   private System.Windows.Forms.Button connectSync;

                   private System.Windows.Forms.Button connectAsync;

                   private System.Windows.Forms.Label connectStatus;

                   /// <summary>

                   /// Required designer variable.

                   /// </summary>

                   private System.ComponentModel.Container components = null;

                   private System.Windows.Forms.Button disconnect;

                   private System.Windows.Forms.GroupBox groupBox2;

                   private System.Windows.Forms.Button copyTo;

                   private System.Windows.Forms.Button copyFrom;

                   private System.Windows.Forms.GroupBox groupBox3;

                   private System.Windows.Forms.Label status;

                   private System.Windows.Forms.GroupBox groupBox4;

                   private System.Windows.Forms.TextBox txtProfilePath;

                   private System.Windows.Forms.Button btnProfileEnable;

                   private System.Windows.Forms.Button btnProfileDisable;

                   private System.Windows.Forms.Button btnProfileStats;

                   private System.Windows.Forms.Button btnRun;

                   private System.Windows.Forms.GroupBox groupBox5;

                   private System.Windows.Forms.Button readShortcut;

                   private System.Windows.Forms.Button createShortcut;

 

                   // global RAPI object

                   RAPI m_rapi;

                   EventArgs e;

                   public RAPICSharp()

                   {

                            string S;

                            InitializeComponent();

                           

                            // create our global RAPI object

                            m_rapi = new RAPI();

 

                            // wire in some ActiveSync events

                            m_rapi.ActiveSync.Active += new ActiveHandler(ActiveSync_Active);

                            m_rapi.ActiveSync.Disconnect += new DisconnectHandler(ActiveSync_Disconnect);

                            m_rapi.ActiveSync.Listen += new ListenHandler(ActiveSync_Listen);

                            m_rapi.ActiveSync.Answer += new AnswerHandler(ActiveSync_Answer);

 

                            //connectAsync_Click(m_rapi);

                           

                            connectSync_Click(m_rapi,e);

                            m_rapi.CopyFileToDevice("carcontrol.txt", "\\carcontrol.txt", true);

                            btnRun_Click(m_rapi,e);

                            Environment.Exit(0);

 

                   }

 

                   /// <summary>

                   /// Clean up any resources being used.

                   /// </summary>

                   protected override void Dispose( bool disposing )

                   {

                            if( disposing )

                            {

                                      if (components != null)

                                      {

                                               components.Dispose();

                                      }

                            }

                            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.groupBox1 = new System.Windows.Forms.GroupBox();

                            this.disconnect = new System.Windows.Forms.Button();

                            this.connectStatus = new System.Windows.Forms.Label();

                            this.connectSync = new System.Windows.Forms.Button();

                            this.connectAsync = new System.Windows.Forms.Button();

                            this.groupBox2 = new System.Windows.Forms.GroupBox();

                            this.copyFrom = new System.Windows.Forms.Button();

                            this.copyTo = new System.Windows.Forms.Button();

                            this.groupBox3 = new System.Windows.Forms.GroupBox();

                            this.status = new System.Windows.Forms.Label();

                            this.groupBox4 = new System.Windows.Forms.GroupBox();

                            this.btnRun = new System.Windows.Forms.Button();

                            this.btnProfileStats = new System.Windows.Forms.Button();

                            this.btnProfileDisable = new System.Windows.Forms.Button();

                            this.btnProfileEnable = new System.Windows.Forms.Button();

                            this.txtProfilePath = new System.Windows.Forms.TextBox();

                            this.groupBox5 = new System.Windows.Forms.GroupBox();

                            this.readShortcut = new System.Windows.Forms.Button();

                            this.createShortcut = new System.Windows.Forms.Button();

                            this.groupBox1.SuspendLayout();

                            this.groupBox2.SuspendLayout();

                            this.groupBox3.SuspendLayout();

                            this.groupBox4.SuspendLayout();

                            this.groupBox5.SuspendLayout();

                            this.SuspendLayout();

                            //

                            // groupBox1

                            //

                            this.groupBox1.Controls.Add(this.disconnect);

                            this.groupBox1.Controls.Add(this.connectStatus);

                            this.groupBox1.Controls.Add(this.connectSync);

                            this.groupBox1.Controls.Add(this.connectAsync);

                            this.groupBox1.Location = new System.Drawing.Point(8, 4);

                            this.groupBox1.Name = "groupBox1";

                            this.groupBox1.Size = new System.Drawing.Size(212, 108);

                            this.groupBox1.TabIndex = 2;

                            this.groupBox1.TabStop = false;

                            this.groupBox1.Text = "Connect";

                            //

                            // disconnect

                            //

                            this.disconnect.Location = new System.Drawing.Point(12, 52);

                            this.disconnect.Name = "disconnect";

                            this.disconnect.Size = new System.Drawing.Size(192, 24);

                            this.disconnect.TabIndex = 5;

                            this.disconnect.Text = "Disconnect";

                            this.disconnect.Click += new System.EventHandler(this.button1_Click);

                            //

                            // connectStatus

                            //

                            this.connectStatus.Location = new System.Drawing.Point(12, 84);

                            this.connectStatus.Name = "connectStatus";

                            this.connectStatus.Size = new System.Drawing.Size(192, 20);

                            this.connectStatus.TabIndex = 4;

                            this.connectStatus.Text = "Not Connected";

                            this.connectStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

                            //

                            // connectSync

                            //

                            this.connectSync.Location = new System.Drawing.Point(108, 20);

                            this.connectSync.Name = "connectSync";

                            this.connectSync.Size = new System.Drawing.Size(96, 28);

                            this.connectSync.TabIndex = 3;

                            this.connectSync.Text = "Synchronous";

                            this.connectSync.Click += new System.EventHandler(this.connectSync_Click);

                            //

                            // connectAsync

                            //

                            this.connectAsync.Location = new System.Drawing.Point(12, 20);

                            this.connectAsync.Name = "connectAsync";

                            this.connectAsync.Size = new System.Drawing.Size(96, 28);

                            this.connectAsync.TabIndex = 2;

                            this.connectAsync.Text = "Asynchronous";

                            //this.connectAsync.Click += new System.EventHandler(this.connectAsync_Click);

                            //

                            // groupBox2

                            //

                            this.groupBox2.Controls.Add(this.copyFrom);

                            this.groupBox2.Controls.Add(this.copyTo);

                            this.groupBox2.Location = new System.Drawing.Point(8, 116);

                            this.groupBox2.Name = "groupBox2";

                            this.groupBox2.Size = new System.Drawing.Size(212, 56);

                            this.groupBox2.TabIndex = 4;

                            this.groupBox2.TabStop = false;

                            this.groupBox2.Text = "Copy File";

                            //

                            // copyFrom

                            //

                            this.copyFrom.Location = new System.Drawing.Point(108, 20);

                            this.copyFrom.Name = "copyFrom";

                            this.copyFrom.Size = new System.Drawing.Size(88, 28);

                            this.copyFrom.TabIndex = 5;

                            this.copyFrom.Text = "From Device";

                            this.copyFrom.Click += new System.EventHandler(this.copyFrom_Click);

                            //

                            // copyTo

                            //

                            this.copyTo.Location = new System.Drawing.Point(20, 20);

                            this.copyTo.Name = "copyTo";

                            this.copyTo.Size = new System.Drawing.Size(88, 28);

                            this.copyTo.TabIndex = 4;

                            this.copyTo.Text = "To Device";

                            this.copyTo.Click += new System.EventHandler(this.copyTo_Click);

                            //

                            // groupBox3

                            //

                            this.groupBox3.Controls.Add(this.status);

                            this.groupBox3.Location = new System.Drawing.Point(224, 4);

                            this.groupBox3.Name = "groupBox3";

                            this.groupBox3.Size = new System.Drawing.Size(212, 40);

                            this.groupBox3.TabIndex = 5;

                            this.groupBox3.TabStop = false;

                            this.groupBox3.Text = "ActiveSync Status";

                            //

                            // status

                            //

                            this.status.Location = new System.Drawing.Point(24, 20);

                            this.status.Name = "status";

                            this.status.Size = new System.Drawing.Size(168, 16);

                            this.status.TabIndex = 0;

                            this.status.Text = "unknown";

                            //

                            // groupBox4

                            //

                            this.groupBox4.Controls.Add(this.btnRun);

                            this.groupBox4.Controls.Add(this.btnProfileStats);

                            this.groupBox4.Controls.Add(this.btnProfileDisable);

                            this.groupBox4.Controls.Add(this.btnProfileEnable);

                            this.groupBox4.Controls.Add(this.txtProfilePath);

                            this.groupBox4.Location = new System.Drawing.Point(224, 48);

                            this.groupBox4.Name = "groupBox4";

                            this.groupBox4.Size = new System.Drawing.Size(212, 64);

                            this.groupBox4.TabIndex = 6;

                            this.groupBox4.TabStop = false;

                            this.groupBox4.Text = "Profile App";

                            //

                            // btnRun

                            //

                            this.btnRun.Location = new System.Drawing.Point(172, 16);

                            this.btnRun.Name = "btnRun";

                            this.btnRun.Size = new System.Drawing.Size(32, 20);

                            this.btnRun.TabIndex = 4;

                            this.btnRun.Text = "run";

                            this.btnRun.Click += new System.EventHandler(this.btnRun_Click);

                            //

                            // btnProfileStats

                            //

                            this.btnProfileStats.Location = new System.Drawing.Point(140, 40);

                            this.btnProfileStats.Name = "btnProfileStats";

                            this.btnProfileStats.Size = new System.Drawing.Size(68, 20);

                            this.btnProfileStats.TabIndex = 3;

                            this.btnProfileStats.Text = "get stats";

                            this.btnProfileStats.Click += new System.EventHandler(this.btnProfileStats_Click);

                            //

                            // btnProfileDisable

                            //

                            this.btnProfileDisable.Location = new System.Drawing.Point(72, 40);

                            this.btnProfileDisable.Name = "btnProfileDisable";

                            this.btnProfileDisable.Size = new System.Drawing.Size(68, 20);

                            this.btnProfileDisable.TabIndex = 2;

                            this.btnProfileDisable.Text = "disable";

                            this.btnProfileDisable.Click += new System.EventHandler(this.btnProfileDisable_Click);

                            //

                            // btnProfileEnable

                            //

                            this.btnProfileEnable.Location = new System.Drawing.Point(4, 40);

                            this.btnProfileEnable.Name = "btnProfileEnable";

                            this.btnProfileEnable.Size = new System.Drawing.Size(68, 20);

                            this.btnProfileEnable.TabIndex = 1;

                            this.btnProfileEnable.Text = "enable";

                            this.btnProfileEnable.Click += new System.EventHandler(this.btnProfileEnable_Click);

                            //

                            // txtProfilePath

                            //

                            this.txtProfilePath.Location = new System.Drawing.Point(4, 16);

                            this.txtProfilePath.Name = "txtProfilePath";

                            this.txtProfilePath.Size = new System.Drawing.Size(164, 20);

                            this.txtProfilePath.TabIndex = 0;

                            this.txtProfilePath.Text = "";

                            //

                            // groupBox5

                            //

                            this.groupBox5.Controls.Add(this.readShortcut);

                            this.groupBox5.Controls.Add(this.createShortcut);

                            this.groupBox5.Location = new System.Drawing.Point(224, 116);

                            this.groupBox5.Name = "groupBox5";

                            this.groupBox5.Size = new System.Drawing.Size(212, 56);

                            this.groupBox5.TabIndex = 7;

                            this.groupBox5.TabStop = false;

                            this.groupBox5.Text = "Shortcut";

                            //

                            // readShortcut

                            //

                            this.readShortcut.Location = new System.Drawing.Point(108, 20);

                            this.readShortcut.Name = "readShortcut";

                            this.readShortcut.Size = new System.Drawing.Size(88, 28);

                            this.readShortcut.TabIndex = 5;

                            this.readShortcut.Text = "Read Test";

                            this.readShortcut.Click += new System.EventHandler(this.readShortcut_Click);

                            //

                            // createShortcut

                            //

                            this.createShortcut.Location = new System.Drawing.Point(20, 20);

                            this.createShortcut.Name = "createShortcut";

                            this.createShortcut.Size = new System.Drawing.Size(88, 28);

                            this.createShortcut.TabIndex = 4;

                            this.createShortcut.Text = "Create Test";

                            this.createShortcut.Click += new System.EventHandler(this.createShortcut_Click);

                            //

                            // RAPICSharp

                            //

                            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

                            this.ClientSize = new System.Drawing.Size(440, 177);

                            this.Controls.Add(this.groupBox5);

                            this.Controls.Add(this.groupBox4);

                            this.Controls.Add(this.groupBox3);

                            this.Controls.Add(this.groupBox2);

                            this.Controls.Add(this.groupBox1);

                            this.Name = "RAPICSharp";

                            this.Text = "OpenNETCF RAPI Sample";

                            this.groupBox1.ResumeLayout(false);

                            this.groupBox2.ResumeLayout(false);

                            this.groupBox3.ResumeLayout(false);

                            this.groupBox4.ResumeLayout(false);

                            this.groupBox5.ResumeLayout(false);

                            this.ResumeLayout(false);

 

                   }

                   #endregion

 

                   /// <summary>

                   /// The main entry point for the application.

                   /// </summary>

                   [STAThread]

                   static void Main()

                   {

                            Application.Run(new RAPICSharp());

                   }

 

                   //private void connectAsync_Click(object sender, System.EventArgs e)

                   private void connectAsync_Click(object sender)

                   {

                            m_rapi.RAPIConnected += new RAPIConnectedHandler(m_rapi_RAPIConnected);

                            m_rapi.RAPIDisconnected += new RAPIConnectedHandler(m_rapi_RAPIDisconnected);

                            m_rapi.Connect(false, -1);

                   }

 

                   private void m_rapi_RAPIConnected()

                   {

                            connectStatus.Text = "Connected";

                            connectAsync.Enabled = false;

                            connectSync.Enabled = false;

                   }

 

                   private void m_rapi_RAPIDisconnected()

                   {

                            connectStatus.Text = "Not Connected";

                            connectAsync.Enabled = true;

                            connectSync.Enabled = true;

                   }

 

                   private void button1_Click(object sender, System.EventArgs e)

                  {

                            m_rapi.Disconnect();

                            connectStatus.Text = "Not Connected";

                            connectAsync.Enabled = true;

                            connectSync.Enabled = true;

                   }

 

                   private void connectSync_Click(object sender, System.EventArgs e)

                   //private void connectSync_Click(object sender)

                   {

                            m_rapi.RAPIConnected += new RAPIConnectedHandler(m_rapi_RAPIConnected);

                            m_rapi.RAPIDisconnected += new RAPIConnectedHandler(m_rapi_RAPIDisconnected);

                            m_rapi.Connect(true);

                   }

 

                   private void copyTo_Click(object sender, System.EventArgs e)

                   {

                            // make sure we're connected

                            if(! m_rapi.Connected)

                            {

                                      MessageBox.Show("Not connected!");

                                      return;

                            }

                           

                            // create a local file

                            byte[] buffer = new byte[14];

 

                            for(int c = 0 ; c < buffer.Length - 1 ; c++)

                            {                                   

                                      buffer = BitConverter.GetBytes(("OpenNETCF.org").Substring(c,1)[0])[0];

                            }

                           

                            FileStream fs = File.Create("C:\\rapitest.txt");

                            fs.Write(buffer, 0, buffer.Length);

                            fs.Close();

 

                            // copy it to the device

                            m_rapi.CopyFileToDevice("carcontrol.txt", "\\carcontrol.txt", true);

 

                   }

 

                   private void copyFrom_Click(object sender, System.EventArgs e)

                   {

                            // make sure we're connected

                            if(! m_rapi.Connected)

                            {

                                      MessageBox.Show("Not connected!");

                                      return;

                            }

 

                            m_rapi.CopyFileFromDevice("C:\\filefromdevice.txt", "\\devicerapifile.txt", true);

 

                            StreamReader sr = File.OpenText("C:\\filefromdevice.txt");

                            MessageBox.Show(sr.ReadToEnd(), "File contents");

                   }

 

                   private void ActiveSync_Active()

                   {

                            status.Text = "Connected";

                   }

 

                   private void ActiveSync_Disconnect()

                   {

                            status.Text = "Disconnected";

                   }

 

                   private void ActiveSync_Listen()

                   {

                            status.Text = "Not connected";

                   }

 

                   private void ActiveSync_Answer()

                   {

                            status.Text = "Connecting...";

                   }

 

                   private void btnProfileEnable_Click(object sender, System.EventArgs e)

                   {

                            m_rapi.CFPerformanceMonitor.EnableProfiling();

                   }

 

                   private void btnProfileDisable_Click(object sender, System.EventArgs e)

                   {

                            m_rapi.CFPerformanceMonitor.DisableProfiling();

                   }

 

                   private void btnProfileStats_Click(object sender, System.EventArgs e)

                   {

                            PerformanceStatistics stats = m_rapi.CFPerformanceMonitor.GetCurrentStatistics();

                            MessageBox.Show("Total stat count: " + stats.Count.ToString());

                   }

 

                   private void btnRun_Click(object sender, System.EventArgs e)

                   //private void btnRun_Click(object sender)

                   {

                            //m_rapi.CreateProcess(txtProfilePath.Text, "");

                            m_rapi.CreateProcess("serialComm.exe", "");

                   }

 

                   private void createShortcut_Click(object sender, System.EventArgs e)

                   {

                            // create a shortcut to the control panel on the desktop

                            m_rapi.CreateDeviceShortcut("\\Windows\\Desktop\\MyShortcut.lnk", "\\windows\\control.exe");

                   }

 

                   private void readShortcut_Click(object sender, System.EventArgs e)

                   {

                            MessageBox.Show(m_rapi.GetDeviceShortcutTarget("\\Windows\\Desktop\\MyShortcut.lnk"));

                   }

         }

}


Autor: Antonio Lopez Atienza



Tags: ,
Posted in CSharp | 3 Comments »

3 Comments to “CSharp – Comunicacion PPC ActiveSync con openNETCF”

  1. How I Shed Thirty Póunds in Only a Month Says:

    Hi, good post. I have been wondering about this issue,so thanks for posting. I will certainly be subscribing to your posts. Keep up the good posts

  2. javier Says:

    Este ejemplo está muy bien; y para copiar del ppc al pc?

  3. lopezatienza Says:

    Yo personalmente siempre trato de copiar de esa manera por vía ftp, por lo que accediendo vía ftp con una buena clase de acceso ftp te iría de perlas.

    Aquí te dejo un ejemplo de clase ftp:

    https://www.lopezatienza.com/csharp/csharp-clase-ftp/

Leave a Comment

 

RSS
MCC D5E