跳转至

输入输出


概述

演示盒子的输入输出接口。


代码


var model = Box.model();
GPIO.set(GPIO.POWER, 1);    //打开电源灯

GPIO.listen(GPIO.IN1);  //侦听输入口
if(model == "DG03" || model == "DG03S")
{
    GPIO.listen(GPIO.IN2);
    GPIO.listen(GPIO.IN3);
    GPIO.listen(GPIO.IN4);
}

//先读输入口当前状态
if(model == "DG03" || model == "DG03S")
{   
    print(GPIO.get(GPIO.IN1), GPIO.get(GPIO.IN2), GPIO.get(GPIO.IN3), GPIO.get(GPIO.IN4));
}
else
{
    print(GPIO.get(GPIO.IN1));
}

//启动定时器读模拟量
Timer.start(1, 5000);

while(true)
{
    var m = Event.get();
    switch(m.msg)
    {
        case Event.PIO_CHANGE:
            print("IN"+(m.port - GPIO.IN1 + 1), m.level);
            //根据输入口状态设置指示灯和继电器
            GPIO.set(m.port - GPIO.IN1 + GPIO.LED1, m.level);
            GPIO.set(m.port - GPIO.OUT1 + GPIO.LED1, m.level);
            break;
        case Event.TIMER_OUT:
            if(model == "DG03" || model == "DG03S")
            {
                //读取输入电流4-20ma, 读输入电压1-5v用getv
                print(ADC.geti(ADC.ADC1), ADC.geti(ADC.ADC2), ADC.geti(ADC.ADC3), ADC.geti(ADC.ADC4));
            }

            if(model == "DG02S" || model == "DG03S")
            {
                //读电源电压
                print("DC:", ADC.getv(ADC.DC));
            }

            Timer.start(1, 5000);
            break;
    }
}

下载代码(右键另存为):pio.js