MF 模拟器读取PC串口数据

MF 模拟器读取PC串口数据

 

 

 

MF 模拟器读取PC串口数据

 

 

 

  1 using System;
  2 using Microsoft.SPOT;
  3 using Microsoft.SPOT.Input;
  4 using Microsoft.SPOT.Presentation;
  5 using Microsoft.SPOT.Presentation.Controls;
  6 using System.IO;
  7 using System.IO.Ports;
  8 using System.Threading;
  9 using Microsoft.SPOT.Hardware;
 10 using Microsoft.SPOT.Touch;
 11 using Microsoft.SPOT.IO;
 12 
 13 namespace MFWindowApplication1
 14 {
 15     public class Program : Microsoft.SPOT.Application
 16     {
 17         public static void Main()
 18         {
 19             Program myApplication = new Program();
 20 
 21             Window mainWindow = myApplication.CreateWindow();
 22 
 23             // Create the object that configures the GPIO pins to buttons.
 24             GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
 25 
 26             #region Test
 27 
 28             //InterruptPort myBtn = new InterruptPort((Cpu.Pin)(0),
 29             //    true,
 30             //    Port.ResistorMode.PullDown,
 31             //    Port.InterruptMode.InterruptEdgeBoth);
 32             //myBtn.OnInterrupt += (a, b, c) =>
 33             //{
 34             //};
 35 
 36             // InputPort[] inputs = new InputPort[5];
 37             //Cpu.Pin[] pins = new Cpu.Pin[]{
 38             //Cpu.Pin.GPIO_Pin0,
 39             //};
 40 
 41             #endregion
 42 
 43             // Start the application
 44             myApplication.Run(mainWindow);
 45         }
 46 
 47         private Window mainWindow;
 48 
 49         public Window CreateWindow()
 50         {
 51             // Create a window object and set its size to the
 52             // size of the display.
 53             mainWindow = new Window();
 54             mainWindow.Height = SystemMetrics.ScreenHeight;
 55             mainWindow.Width = SystemMetrics.ScreenWidth;
 56 
 57             // Create a single text control.
 58             Text text = new Text();
 59 
 60             text.Font = Resources.GetFont(Resources.FontResources.small);
 61             text.TextContent = Resources.GetString(Resources.StringResources.String1);
 62             text.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
 63             text.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
 64 
 65             // Add the text control to the window.
 66             mainWindow.Child = text;
 67 
 68             // Connect the button handler to all of the buttons.
 69             mainWindow.AddHandler(Buttons.ButtonUpEvent, new RoutedEventHandler(OnButtonUp), false);
 70 
 71             // Set the window visibility to visible.
 72             mainWindow.Visibility = Visibility.Visible;
 73 
 74             // Attach the button focus to the window.
 75             Buttons.Focus(mainWindow);
 76 
 77             SerialPort sp = new SerialPort("COM1", 9600);
 78             sp.Open();
 79             sp.DataReceived += (a, b) =>
 80             {
 81                 int nDL = sp.BytesToRead;
 82                 int i = 0;
 83                 System.Text.StringBuilder sb = new System.Text.StringBuilder();
 84                 while (i < nDL)
 85                 {
 86                     byte[] datas = new byte[nDL];
 87                     int nLen = sp.Read(datas, 0, datas.Length);
 88                     string s = new string(System.Text.Encoding.UTF8.GetChars(datas, 0, nLen));//0, nLen
 89                     sb.Append(s);
 90                     i += nLen;
 91                 }
 92 
 93                 text.Dispatcher.Invoke(new TimeSpan(1),
 94                     new DispatcherOperationCallback((v) =>
 95                     {
 96                         text.TextContent = v == null ? "#ERROR" : v.ToString();
 97                         return true;
 98                     }),
 99                     sb.ToString());
100             };
101             return mainWindow;
102         }
103 
104         private void OnButtonUp(object sender, RoutedEventArgs evt)
105         {
106             ButtonEventArgs e = (ButtonEventArgs)evt;
107 
108             // Print the button code to the Visual Studio output window.
109             Debug.Print(e.Button.ToString());
110         }
111     }
112 }

 

 MF 模拟器读取PC串口数据

 

 

 

MF 模拟器读取PC串口数据

 

上一篇:将DataGrip连接到MS SQL Server的方法


下一篇:DataGrip for Mac激活码