叶帆

.Net Micro Framework研究—TCP/IP通信

0
阅读(2596)

试验平台:Digi MF开发板

关于网络通信方面,Digi提供了两个程序,一个是TCP Server运行在Digi的开发板上,一个是TCP Client程序,运行在PC上,通过网络,上位机很容易控制Digi开发的IO信号。客户端程序运行后的界面如下:
 

(图MF071029004.jpg)
如果仅仅运行一下示例程序,那显然不过瘾!既然串口实现了Modbus Rtu协议,那么网口就实现Modbus Tcp协议吧,实现的Modbus Tcp协议比我们用串口实现Modbus Rtu的指令要多一个,不仅实现了3号命令,也实现了16号命令,这样我们就可以通过Modbus Tcp读写Digi开发板的数据了。这次我们操作的是Digi开发板上的5个LED灯。用OutputPort对象去操作。
操作GPIO的相关代码如下:
//读GPIO信号
DataBuff[0] = 0;
DataBuff[1] = (byte)((output[0].Read() ? 1 : 0) | (output[1].Read() ? 2 : 0) | (output[2].Read() ? 4 : 0) | (output[3].Read() ? 8 : 0) | (output[4].Read() ? 16 : 0));
//写GPIO信号
bool[] bFlag = new bool[5];
bFlag[0]=(DataBuff[1] & 0x01)>0 ? true:false;
bFlag[1]=(DataBuff[1] & 0x02)>0 ? true:false;
bFlag[2]=(DataBuff[1] & 0x04)>0 ? true:false;
bFlag[3]=(DataBuff[1] & 0x08)>0 ? true:false;
bFlag[4]=(DataBuff[1] & 0x10)>0 ? true:false;
for (i = 0; i < 5; i++)
{
   output[i].Write(bFlag[i]);
}
 
网络操作相关源码如下:
using System;
using Microsoft.SPOT;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Microsoft.SPOT.Hardware;
 
namespace MFModbusTcp
{
    public class ModbusTcpSlave
    {
        //MF开发板 IO灯
        private 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 };
        private Socket socketServer;
        private Socket s = null;
 
        //变量缓冲区
        private byte[] m_bytData = new byte[256];
        private byte[] m_InputTCPBuf = new byte[1024];
        private byte[] m_OutputTCPBuf = new byte[1024];
        public byte[] DataBuff = new byte[1024];
       
        //启动Modbus Tcp服务
        public void Run()
        {
            //初始化 GPIO
            for (int i = 0; i < 5; i++)
            {
                output[i] = new OutputPort(pin[i], false);
            }
            socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socketServer.Bind(new IPEndPoint(DottedDecimalToIp(0, 0, 0, 0), 502));
            socketServer.Listen(1);
            Debug.Print("Modbus Tcp Slave Started");
            while (true)
            {
                s = socketServer.Accept();
                while (s != null)
                {
                    if ((int)s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error) != 0) goto ExitServer;
                    if (s.Available > 0)
                    {
                        TCP_DealwithCommand();
                    }            
                    Thread.Sleep(10);
                }
            }
         ExitServer:
            s.Close();
            s = null;
            Debug.Print("Modbus Tcp Slave Exit");
        }
 
        //数据解析
        private void TCP_DealwithCommand()
        {
            int i = 0;
            int lngSendNum = 0;
            int lngDataNum = 0;
            int lngDataAddr = 0;
 
            //接收数据
            int intRet=s.Receive(m_InputTCPBuf);
            //Debug.Print("Receive Num:" + intRet.ToString());
            for (i = 0; i < 12; i++) m_OutputTCPBuf[i] = m_InputTCPBuf[i];
            //---------------------------------------------------------------
            //Modbus 读命令
            if (m_InputTCPBuf[7] == 3)
            {
                … …
            }
            //---------------------------------------------------------------
            //Modbus 写命令
            if (m_InputTCPBuf[7] == 16)
            {
                … …
            }
        }
        //IP地址转化
        private long DottedDecimalToIp(byte a1, byte a2, byte a3, byte a4)
        {
            return (long)((ulong)a4 << 24 | (ulong)a3 << 16 | (ulong)a2 << 8 | (ulong)a1);
        }
    }
}
程序部署运行后,我们就可以用标准的Modbus Tcp Client程序测试了,我使用的是YFIOServer。
1、  先配置Modbus Tcp驱动程序
 

(图MF071029001.jpg)
2、  再配置IO连接变量
 

(图MF071029001.jpg)
3、  开始读写GPIO,此时GPIO灯的亮灭,完全被写入的数据控制了。
 

(图MF071029001.jpg)
 
总论:很难想像,操作TCP的代码比C#的代码还要简洁高效,不到十几分钟,就把一个C#代码改造为MF代码,并且在很短的时间内就调试成功。微软的下一个战略看来马上就成为现实:全世界的每一个智能设备都用MF上网J。