.net micro framework的入门例程-点灯神话-8.串口接收字符D即发送LED闪烁日志
来源:互联网 发布:windows设置ntp客户端 编辑:程序博客网 时间:2024/06/06 00:58
学学怎么使用串口接收
using System;
using System.IO;using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Collections;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.Gadgeteer;
namespace my_LED
{
public class my_led
{
static OutputPort lampOutport = new OutputPort((Cpu.Pin)GHI.Hardware.G120.Pin.P0_4,false);
static InterruptPort switchInterrupt = new InterruptPort(
(Cpu.Pin)GHI.Hardware.G120.Pin.P2_10,
false,
Port.ResistorMode.PullUp,
Port.InterruptMode.InterruptEdgeBoth);
// static private FlashlightUse[] FlashlightUseLog;
static DateTime switchedOn;
private static ArrayList UseLog = new ArrayList();
static SerialPort Uart1 = new SerialPort("COM1", 115200);
public static void Main()
{
// 开按键中断
switchInterrupt.OnInterrupt += new NativeEventHandler(switchInterrupt_OnInterrupt);
//开定时器
System.Threading.Timer timer = new System.Threading.Timer(
new TimerCallback(flashTick),
null,
5000,
5000);
//开串口
Uart1.Open();
// create a string
string counter_string = "Hello micro framework\r\n";
// convert the string to bytes
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(counter_string);
// send the bytes on the serial port
Uart1.Write(buffer, 0, buffer.Length);
Uart1.Flush();
monitorLogRequests();
//System.Threading.Thread serialThread = new System.Threading.Thread(monitorLogRequests);
// System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
static void switchInterrupt_OnInterrupt(uint data1, uint data2, DateTime time)
{
Monitor.Enter(switchInterrupt);
switch (data2)
{
case 0: lampOutport.Write(true); break;
case 1: lampOutport.Write(false); break;
default: break;
}
Monitor.Exit(switchInterrupt);
}
private static void monitorLogRequests()
{
byte[] buffer = new byte[1];
while (true)
{
int count = Uart1.Read(buffer, 0, 1);
if (count > 0)
{
if (buffer[0] == 'D')
{
sendDataList();
}
}
}
}
private static void flashTick(object o)
{
if (!switchInterrupt.Read())
{
return;
}
lampOutport.Write(true);
switchedOn = DateTime.Now;
System.Threading.Thread.Sleep(100);
System.Threading.Monitor.Enter(switchInterrupt);
if (switchInterrupt.Read())
{
lampOutport.Write(false);
FlashlightUse record = new FlashlightUse(switchedOn, DateTime.Now);
UseLog.Clear();
UseLog.Add(record);
// sendDataList();
}
System.Threading.Monitor.Exit(switchInterrupt);
}
static void sendDataList()
{
foreach (FlashlightUse logItem in UseLog)
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(logItem.ToString());
Uart1.Write(buffer, 0, buffer.Length);
byte[] buffer1 = System.Text.Encoding.UTF8.GetBytes("\r\n");
Uart1.Write(buffer1, 0, buffer1.Length);
}
Uart1.Flush();
}
}
public class FlashlightUse
{
public DateTime switchedOn; // 点亮时间
public TimeSpan litDuration; // 持续时间
public override string ToString()
{
return "On : " + switchedOn.ToString() + " for : " + litDuration.ToString();
}
public FlashlightUse(DateTime inSwitchedOn, DateTime inSwithedOff)
{
switchedOn = inSwitchedOn;
litDuration = inSwithedOff.Subtract(switchedOn);
}
public FlashlightUse(DateTime inSwitchedOn, TimeSpan inLitDuration)
{
if (inLitDuration.Ticks < 0)
{
throw new Exception("Invalid FlashlightUse timespan");
}
switchedOn = inSwitchedOn;
litDuration = inLitDuration;
}
}
}
0 0
- .net micro framework的入门例程-点灯神话-8.串口接收字符D即发送LED闪烁日志
- .net micro framework的入门例程-点灯神话-7.队列存储LED灯闪烁时间并用串口发送
- .net micro framework的入门例程-点灯神话-4.加入闪烁LED函数
- .net micro framework的入门例程-点灯神话-5.加入一个线程闪烁LED
- .net micro framework的入门例程-点灯神话-6.使用定时器代替延时函数闪烁LED
- .net micro framework的入门例程-点灯神话-2.按键控制LED灯
- .net micro framework的入门例程-点灯神话-3.按键中断控制LED
- .net micro framework的入门例程-点灯神话-1.神灯自闪
- Microsoft .NET Micro Framework移植开发笔记2之点灯神话
- .Net Micro Framework研究—串口部署的困惑
- .Net Micro Framework 快速入门
- .Net Micro Framework 快速入门
- Net Micro Framework 快速入门
- .Net Micro Framework 快速入门
- .Net Micro Framework 快速入门
- .Net Micro Framework研究—串口操作
- .Net Micro Framework研究—串口操作
- 【.Net Micro Framework PortingKit - 03】调试初步:点亮LED灯
- 选择重传协议 C语言实现
- 发布或重启线上服务时抖动问题解决方案
- 最快速的Android开发环境搭建ADT-Bundle及Hello World
- 微软游戏设备2o14-9月进入中国内地市场
- 写了一个二叉树构造及中序遍历函数
- .net micro framework的入门例程-点灯神话-8.串口接收字符D即发送LED闪烁日志
- myEclipse/Eclipse 自动提示代码?呵呵,来吧!我告诉你
- 八皇后 入门经典
- [POJ 2887] Big String [块状链表]
- 三.Python数据类型の(四).序列类型(字典)
- hdoj2018
- nefu 640 Number Guessing
- codechef Enormous Input Test 快速读入数据 fread
- Chinese Input Methods in Ubuntu 14, 13, 12, 11, and 10(How to Set Up Ubuntu Chinese IMEs)