网络


概述

演示网络的基本用法。网络准备好后就连接服务器(xianhe.tpddns.cn:10808);连接成功则发送hello到服务器;当收到"test"串时,打开DATA提示灯和输出2秒。


代码


GPIO.set(GPIO.POWER, 1)
Net.init()
while(true)
{
    var m = Event.get();
    print(m);
    switch(m.msg)
    {
        case Event.NET_EVENT:
            switch(m.event)
            {
                case Net.READY:
                    Net.connect(1, "xianhe.tpddns.cn", 10808);
                    break;
                case Net.CONN_OK:
                    Net.send(1, "hello");
                    break;
                case Net.CONN_FAIL:
                    print("connect failed");
                    break;
                case Net.CONN_CLOSE:
                    print("connect closed");
                    break;
                case Net.RECV:
                    var s = m.data.gets();
                    if(s == "test")
                    {
                        GPIO.set(GPIO.DATA, 1);
                        GPIO.set(GPIO.OUT1, 1);
                        Timer.start(1, 2000);
                        Net.send(1, "ok");
                    }
                    else
                    {
                        Net.send(1, "wrong");
                    }
                    break;
            }
            break;
        case Event.TIMER_OUT:
            if(m.id == 1)
            {
                GPIO.set(GPIO.DATA, 0);
                GPIO.set(GPIO.OUT1, 0);
            }
            break;
    }
}