生活随笔
收集整理的這篇文章主要介紹了
.Net Micro Framework研究—TCP/IP通信
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
試驗(yàn)平臺(tái):Digi MF開發(fā)板
關(guān)于網(wǎng)絡(luò)通信方面,Digi提供了兩個(gè)程序,一個(gè)是TCP Server運(yùn)行在Digi的開發(fā)板上,一個(gè)是TCP Client程序,運(yùn)行在PC上,通過(guò)網(wǎng)絡(luò),上位機(jī)很容易控制Digi開發(fā)的IO信號(hào)。客戶端程序運(yùn)行后的界面如下:
?
(圖MF071029004.jpg)
如果僅僅運(yùn)行一下示例程序,那顯然不過(guò)癮!既然串口實(shí)現(xiàn)了Modbus Rtu協(xié)議,那么網(wǎng)口就實(shí)現(xiàn)Modbus Tcp協(xié)議吧,實(shí)現(xiàn)的Modbus Tcp協(xié)議比我們用串口實(shí)現(xiàn)Modbus Rtu的指令要多一個(gè),不僅實(shí)現(xiàn)了3號(hào)命令,也實(shí)現(xiàn)了16號(hào)命令,這樣我們就可以通過(guò)Modbus Tcp讀寫Digi開發(fā)板的數(shù)據(jù)了。這次我們操作的是Digi開發(fā)板上的5個(gè)LED燈。用OutputPort對(duì)象去操作。
操作GPIO的相關(guān)代碼如下:
??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)); ????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]);? ??} ??? ?? 網(wǎng)絡(luò)操作相關(guān)源碼如下:
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 ??????{ ????????????????????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]; ?????????? ????????????????????public?void?Run() ??????????{ ????????????????????????????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); ????????????????????????????for?(i?=?0;?i?<?12;?i++)?m_OutputTCPBuf[i]?=?m_InputTCPBuf[i]; ??????????????????????????????????????????if?(m_InputTCPBuf[7]?==?3) ??????????????{ ??????????????????…?… ??????????????} ??????????????????????????????????????????if?(m_InputTCPBuf[7]?==?16) ??????????????{ ??????????????????…?… ??????????????} ??????????} ????????????????????private?long?DottedDecimalToIp(byte?a1,?byte?a2,?byte?a3,?byte?a4) ??????????{ ??????????????return?(long)((ulong)a4?<<?24?|?(ulong)a3?<<?16?|?(ulong)a2?<<?8?|?(ulong)a1); ??????????} ??????} ??} ?? 程序部署運(yùn)行后,我們就可以用標(biāo)準(zhǔn)的Modbus Tcp Client程序測(cè)試了,我使用的是YFIOServer。
1、? 先配置Modbus Tcp驅(qū)動(dòng)程序
?
(圖MF071029001.jpg)
2、? 再配置IO連接變量
?
(圖MF071029001.jpg)
3、? 開始讀寫GPIO,此時(shí)GPIO燈的亮滅,完全被寫入的數(shù)據(jù)控制了。
?
(圖MF071029001.jpg)
?
總論:很難想像,操作TCP的代碼比C#的代碼還要簡(jiǎn)潔高效,不到十幾分鐘,就把一個(gè)C#代碼改造為MF代碼,并且在很短的時(shí)間內(nèi)就調(diào)試成功。微軟的下一個(gè)戰(zhàn)略看來(lái)馬上就成為現(xiàn)實(shí):全世界的每一個(gè)智能設(shè)備都用MF上網(wǎng)J。
?
?
轉(zhuǎn)載于:https://blog.51cto.com/yfsoft/323409
總結(jié)
以上是生活随笔為你收集整理的.Net Micro Framework研究—TCP/IP通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。