C# sample code for SPL engine - SPL MF WindowApplication source


1. Open C# project file
  1. Double-click "Samples" directory.

 

  1. Double-click "SPLMFEngine.WindowApps" directory.



  2. Double-click "SPLMFEngine.WindowApps.sln" file.



  3. Open Program.cs file.




2. C# source code for the SPL MF WindowApplication
  1. Program.cs

      
    
    using System;
    using System.Text;
    
    using Microsoft.SPOT;
    using Microsoft.SPOT.Input;
    using Microsoft.SPOT.Presentation;
    using Microsoft.SPOT.Presentation.Controls;
    using Microsoft.SPOT.Hardware;
    
    using SPL.Script.Engine;
    
    namespace SPLMFEngine.WindowApps
    {
        public class Program : Microsoft.SPOT.Application
        {
            public static void Main()
            {
                Program myApplication = new Program();
    
                Window mainWindow = myApplication.CreateWindow();
    
                GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
    
                myApplication.Run(mainWindow);
            }
    
            private Window mainWindow;
    
            //##########################  Sample Code  #################################
            private SPLScriptEngine _engine = null;
            private System.IO.Ports.SerialPort _serialPort = null;
            //##########################  Sample Code  #################################
    
            public Window CreateWindow()
            {
                mainWindow = new Window();
                mainWindow.Height = SystemMetrics.ScreenHeight;
                mainWindow.Width = SystemMetrics.ScreenWidth;
    
                //##########################  Sample Code  #################################
                Font font = Resources.GetFont(Resources.FontResources.small);
                //##########################  Sample Code  #################################
    
                mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);
                mainWindow.AddHandler(Buttons.ButtonDownEvent, new ButtonEventHandler(OnButtonDown), false);
    
                mainWindow.Visibility = Visibility.Visible;
    
                Buttons.Focus(mainWindow);
    
                //##########################  Sample Code  #################################
                _engine = new SPLScriptEngine(mainWindow, null, Dispatcher.CurrentDispatcher);
                _engine.AddFont("small", Resources.GetFont(Resources.FontResources.small));
    
                _serialPort = new System.IO.Ports.SerialPort("COM2", 9600);
                _serialPort.Open();
                _engine.AddSerialPortInstance("COM2", _serialPort);
    
                System.Threading.Thread serialReaderThread = new System.Threading.Thread(SerialPortReader);
                serialReaderThread.Start();
                //##########################  Sample Code  #################################
    
                return mainWindow;
            }
    
            private void OnButtonUp(object sender, ButtonEventArgs e)
            {
                Debug.Print("ButtonUp -> " + e.Button.ToString());
            }
    
            private void OnButtonDown(object sender, ButtonEventArgs e)
            {
                Debug.Print("ButtonDown -> " + e.Button.ToString());
            }
    
    
            private void SerialPortReader()
            {
                byte[] sizeBuff = new byte[4];
                byte[] readBuff = null;
                int rCnt = 0;
                uint readDataSize = 0;
    
                Debug.Print("<<< Start SPL Listening >>>");
    
                byte[] msgBuff = Encoding.UTF8.GetBytes("SPL Engine is ready.");
                _serialPort.Write(msgBuff, 0, msgBuff.Length);
    
                while (true)
                {
                    try
                    {
                        readDataSize = 0;
    
                        rCnt = _serialPort.Read(sizeBuff, 0, 4);
    
                        if (sizeBuff[0] == 0x01 && sizeBuff[1] == 0x00)
                        {
                            readDataSize = Utility.ExtractValueFromArray(sizeBuff, 2, 2);
                        }
    
                        if (readDataSize > 0)
                        {
                            readBuff = ReadSerialData((int)readDataSize);
    
                            //SPL script starts with "".
                            if (readBuff[0] == (byte)'<' && readBuff[1] == (byte)'!' && readBuff[readDataSize - 2] == (byte)'!' && readBuff[readDataSize - 1] == (byte)'>')
                            {
                                byte[] dataBuff = Utility.ExtractRangeFromArray(readBuff, 2, readBuff.Length - 4);
    
                                if (dataBuff != null)
                                {
                                    if (dataBuff.Length > 0)
                                    {
                                        string script = new string(Encoding.UTF8.GetChars(dataBuff));
    
                                        if (script != string.Empty)
                                        {
                                            _engine.ExecuteScriptText(script);
                                        }
                                    }
                                }                           
                            }
                            else
                            {
                                _engine.OnSerialPortReceived(_serialPort.PortName, readBuff);
                            }
                        }
                    }
                    catch { }
                }
            }
    
            private byte[] ReadSerialData(int buff_length)
            {
                byte[] readBuff = new byte[buff_length];
                int rCnt = _serialPort.Read(readBuff, 0, buff_length);
    
                return readBuff;
            }
        }
    }
    
    
      



3. Execute sample code
  1. Open "Properties" page of C# project.

  2. Choose ".NET Micro Framework" tab.

  3. Select "SPLMFWindowEmulator" from the emulator list.



  4. Press "F5" key to run sample code.