---
title: ".Net Micro Framework研究—IO读写"
author: "mmdev"
date: "2007-10-28T14:43:00Z"
tags: []
slug: "net-micro-framework-io"
draft: false
lastmod: "2012-08-04T21:33:33Z"
id: 1620951
url: "https://mmdev.iteye.com/blog/1620951"
---

试验平台：Digi MF开发板

![](http://p.blog.csdn.net/images/p_blog_csdn_net/yefanqiu/MF071028001.jpg)

（图MF071028001.jpg）

与IO相关的部分如下：

（3）拨码开关SW1 （4）拨码开关SW2 （11）GPIO灯—output （10）GPIO—input（参见下图）

![](http://p.blog.csdn.net/images/p_blog_csdn_net/yefanqiu/MF071028003.jpg)

（图MF071028003.jpg）

首先，要操作相关IO部分，必须拨码开关SW1的拨码全部拨到右边，这时候GPIO有效。（SW1开关是切换串口和GPIO的）。

这时候如果你对拨码开关SW2进行操作，你会发现，当SW2的拨码在右边的时候相对应GPIO灯会亮，拨到左边则灭（这时候要保证没有任何程序运行或有程序运行但无操作GPIO灯的指令操作）。

如果你要使用最右边的GPIO入来控制GPIO灯，你必须使SW2的拨码在左边，这时候GPIO灯亮。你用一个短接线，把pin9和pin10端口短接（如上图），这时候你发现GPIO第1个灯灭，断开，则IO灯亮。相对应你也可以控制GPIO其它灯的亮灭。

好了，知道上面的知识，我们就可以进行相应的IO编程了。

首先我们要知道GPIO的pin，请参见下表。

![](http://p.blog.csdn.net/images/p_blog_csdn_net/yefanqiu/MF071028002.jpg)

（图MF071028002.jpg）

很遗憾，从上表得知，SW1、SW2、GPIO共用同一批GPIO Pin。

分别为0，1，2，5，6。对编程的影响就是，不能同时操作 GPIO入和GPIO出，如果强行同时操作，虽然代码可以编译成功，但运行时会跳出如下异常。

![](http://p.blog.csdn.net/images/p_blog_csdn_net/yefanqiu/MF071028004.jpg)

（图MF071028004.jpg）

下面是读IO入的代码：

using System;

using System.Collections;

using System.Net;

using System.Net.Sockets;

using System.Runtime.CompilerServices;

using System.Text;

using System.Threading;

using Microsoft.SPOT;

using Microsoft.SPOT.Hardware;

using Microsoft.SPOT.Net;

using Microsoft.SPOT.Net.NetworkInformation;

public class DigiTest

{

public static void Main()

{

InputPort\[] input = new InputPort\[5];

Cpu.Pin\[] pin = new Cpu.Pin\[5] { (Cpu.Pin)0, (Cpu.Pin)1, (Cpu.Pin)2, (Cpu.Pin)5, (Cpu.Pin)6 };

for (int i = 0; i &lt; 5; i++)

{

input\[i] = new InputPort(pin\[i], false, Port.ResistorMode.PullUp);

}

while (true)

{

Debug.Print(input\[0].Read().ToString() + " " + input\[1].Read().ToString() + " " + input\[2].Read().ToString() + " " + input\[3].Read().ToString() + " " + input\[4].Read().ToString());

Thread.Sleep(1000);

}

}

}

下面是写IO出的代码，部署运行后你会发现，GPIO会闪烁。

using System;

using System.Collections;

using System.Net;

using System.Net.Sockets;

using System.Runtime.CompilerServices;

using System.Text;

using System.Threading;

using Microsoft.SPOT;

using Microsoft.SPOT.Hardware;

using Microsoft.SPOT.Net;

using Microsoft.SPOT.Net.NetworkInformation;

public class DigiTest

{

public static void Main()

{

OutputPort\[] output = new OutputPort\[5];

Cpu.Pin\[] pin = new Cpu.Pin\[5] { (Cpu.Pin)0, (Cpu.Pin)1, (Cpu.Pin)2, (Cpu.Pin)5, (Cpu.Pin)6 };

for (int i = 0; i &lt; 5; i++)

{

output\[i] = new OutputPort(pin\[i], false);

}

while (true)

{

for (int i = 0; i &lt; 5; i++)

{

output\[i].Write(!output\[i].Read());

}

Thread.Sleep(1000);

}

}

}