Microsoft MVP성태의 닷넷 이야기
사물인터넷: 48. 넷두이노의 C# 네트워크 프로그램 [링크 복사], [링크+제목 복사],
조회: 21396
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

넷두이노의 C# 네트워크 프로그램

(이제는 너무 오래된) 넷두이노 모델 중 "Netduino Plus" 모델은,

넷두이노(Netduino)의 네트워크 설정 방법
; https://www.sysnet.pe.kr/2/0/11702

유선 랜을 지원합니다. 반면 최근 넷두이노 제품을 보면,

Netduino
; https://www.wildernesslabs.co/netduino

이더넷과 와이파이 모델을 모두 판매하고 있는데 대략 각각 $45, $50에 구매할 수 있습니다. 다른 아두이노 호환 모델과 비교해 이 보드의 장점이 있다면... 바로 개발이 무척 쉽다는 점입니다. 어느 정도로 쉽냐면??? 심지어 네트워크의 경우 HttpWebRequest 타입까지도 지원하기 때문에 거의 데스크톱 경험으로 개발할 수 있습니다. 일례로, 다음의 코드는 웹 페이지를 읽어오면 LED를 켜는 기능을 구현하고 있습니다.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.IO;
using System.Text;
using Microsoft.SPOT.Net.NetworkInformation;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace NetworkLED
{
    public class Program
    {
        static byte[] _contentBuffer = new byte[1024];

        public static void Main()
        {
            OutputPort led7 = new OutputPort(Pins.ONBOARD_LED, false);
            EnableNetwork();

            while (true)
            {
                string txt = GetStringFromUrl("https://www.sysnet.pe.kr");

                led7.Write(txt != null && txt.Length != 0);
                Thread.Sleep(1000);
            }
        }

        private static void EnableNetwork()
        {
            Microsoft.SPOT.Net.NetworkInformation.NetworkInterface ni
                = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];

            ni.PhysicalAddress = new byte[] { 0x00, 0x15, 0x5D, 0x51, 0x01, 0x06 };
            ni.EnableDynamicDns();
            ni.EnableDhcp();
            ni.RenewDhcpLease();
        }

        private static string GetStringFromUrl(string url)
        {
            HttpWebRequest hwr = HttpWebRequest.Create(url) as HttpWebRequest;
            HttpWebResponse response = null;

            try
            {
                response = hwr.GetResponse() as HttpWebResponse;

                Stream respStream = response.GetResponseStream();
                respStream.Read(_contentBuffer, 0, _contentBuffer.Length);

                response.Close();
                hwr.Dispose();

                return new string(Encoding.UTF8.GetChars(_contentBuffer));
            }
            catch { }

            return null;
        }
    }
}

위의 소스 코드를 조금만 변형하면, 웹을 이용한 장치 제어를 매우 쉽게 구현할 수 있습니다. 게다가 라인 단위의 실시간 디버깅 기능도 제공하니 이만큼 편한 개발 환경이 없을 것입니다.

반면, 당연히 단점이 있습니다. 기본적으로 TinyCLR이 올라가기 때문에 점유되는 메모리가 있으니 다른 환경에 비해 메모리 관리를 잘 해야 합니다. 사실 제가 가지고 있던 구형 넷두이노 플러스는 가용한 RAM 용량이 50+ KB라고 하니 애당초 복잡한 응용 프로그램으로는 사용할 수 없다고 봐야 합니다. 그나마 신형 모델(넷두이노 3)은 3배나 넓어졌다고 하지만... ^^; 그래도 164+ KB라고 하니 데스크톱 환경에서처럼 메모리를 신경 쓰지 않고 사용할 수는 없습니다.

그렇긴 하지만, 대개의 경우 IoT 제품들이 구현하는 기능들이 단순하다는 것을 감안하면 메모리가 문제 되는 경우는 많지 않으므로... 뭐랄까, 만약 C# 개발자라면 ^^ 하나쯤 가지고 노는 것도 재미있을만한... 그런 제품입니다. 쓰다 보니 제품 홍보가 되었는데, 실은 다른 보드들의 코딩을 실습하면서 넷두이노가 얼마나 개발 환경이 편했는지 새삼 느끼게 되었다는!!! ^^;




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 10/4/2018]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2021-08-21 12시10분
Enterprise-Grade IoT Full .NET, Real Embedded Secure & Scalable
 - Meadow is a complete, IoT platform with defense-grade security that runs full .NET Standard applications on embeddable microcontrollers.
; https://www.wildernesslabs.co/

Hello, Meadow!
; http://developer.wildernesslabs.co/Meadow/Getting_Started/Hello_World/
정성태

... 76  77  78  79  80  [81]  82  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11908정성태5/22/201919809.NET Framework: 836. C# - Python range 함수 구현파일 다운로드1
11907정성태5/22/201916572오류 유형: 541. msbuild - MSB4024 The imported project file "...targets" could not be loaded
11906정성태5/21/201916781.NET Framework: 835. .NET Core/C# - 리눅스 syslog에 로그 남기는 방법
11905정성태5/21/201917362.NET Framework: 834. C# - 폴더 경로 문자열에서 "..", "." 표기를 고려한 최종 문자열을 얻는 방법 - 두 번째 이야기
11904정성태5/21/201925731.NET Framework: 833. C# - Open Hardware Monitor를 이용한 CPU 온도 정보 [1]파일 다운로드1
11903정성태5/21/201919552오류 유형: 540. .NET Core - System.PlatformNotSupportedException: The named version of this synchronization primitive is not supported on this platform.
11902정성태5/21/201917740오류 유형: 539. mstest 실행 시 "The directory name is invalid." 오류 발생
11901정성태5/21/201919617오류 유형: 538. msbuild 오류 - Could not find a part of the path '%LOCALAPPDATA%\Temp\2\.NETFramework,Version=v4.0.AssemblyAttributes.cs'
11900정성태5/18/201918554오류 유형: 537. "sfc /scannow" 실행 중 시스템이 부팅되는 현상
11899정성태5/17/201919410Linux: 9. Linux에서 윈도우의 OutputDebugString 대신 사용할 수 있는 syslog [1]
11898정성태5/16/201921208VC++: 130. C++ string의 c_str과 data 함수의 차이점 [3]
11897정성태5/16/201928194오류 유형: 536. Visual Studio - "Developer Pack"을 설치했는데도 "대상 프레임워크" 목록에 나오지 않는 경우 [2]
11896정성태5/15/201923086개발 환경 구성: 440. C#, C++ - double의 Infinity, NaN 표현 방식파일 다운로드1
11895정성태5/12/201920956.NET Framework: 832. ML.NET Model Builder - 회귀(Regression), 다중 분류(Multi-class classification) 예제파일 다운로드1
11894정성태5/10/201922671VS.NET IDE: 135. Visual Studio - ML.NET Model Builder 소개 [5]
11893정성태5/10/201919602오류 유형: 535. C# 6.0 이상의 문법을 컴파일 시 오류가 발생한다면?
11892정성태5/10/201919424웹: 38. HTTP Cookie의 expires 시간 형식(RFC7231)
11891정성태5/9/201922513.NET Framework: 831. (번역글) .NET Internals Cookbook Part 12 - Memory structure, attributes, handles
11890정성태5/8/201917586개발 환경 구성: 439. "Visual Studio Enterprise is required to execute the test." 메시지와 관련된 코드 기록
11889정성태5/8/201918320개발 환경 구성: 438. mstest, QTAgent의 로그 파일 설정 방법
11888정성태5/8/201935671.NET Framework: 830. C# - 비동기 호출을 취소하는 CancellationToken의 간단한 예제 코드 [1]파일 다운로드1
11887정성태5/8/201921257.NET Framework: 829. C# - yield 문을 사용할 수 있는 메서드의 조건
11886정성태5/7/201919117오류 유형: 534. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 [2]
11885정성태5/7/201915991오류 유형: 533. mstest.exe 실행 시 "File extension specified '.loadtest' is not a valid test extension." 오류 발생
11884정성태5/5/201920824.NET Framework: 828. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기
11883정성태5/3/201926013.NET Framework: 827. C# - 인터넷 시간 서버로부터 받은 시간을 윈도우에 적용하는 방법파일 다운로드1
... 76  77  78  79  80  [81]  82  83  84  85  86  87  88  89  90  ...