通过RESTful API通过python的接口将MOXA ioLogik1214与python接口,以将数据放置到DO端口

问题描述 投票:1回答:1

我正在尝试编写PUT数据(RESTful API),以将MOXA ioLogik1214设备的数字输出通道设置为1。

我尝试将数字输入DI-00配置为diCounter通道,并且能够对该通道进行读写。

但是,当我尝试写入DO-00时,遇到了配置为普通输出通道的问题:

“发生HTTP错误:404客户端错误:未找到”(如##注释所示。

我已检查DO-00是否为读/写数字输出通道。任何建议表示赞赏!谢谢!

我的python代码如下:

def getData(url, headers):
    try:
        response = requests.get(url, headers=headers)

        # If the response was successful, no Exception will be raised.
        response.raise_for_status()
    except HTTPError as http_err:
        print("HTTP error occurred: %s" % http_err)
        if (response.status_code == 400):
            if len(response.text) > 0:
                print("response.text:\n%s" % response.text)
    except Exception as err:
        print("Other error occurred: %s" % err)
    else:
        if len(response.text) > 0:
                print("response.text:\n%s" % response.text)
        else:
            print("Success!")

    # Print empty line.
    print


def putData(url, headers, data):
    try:
        response = requests.put(url, headers=headers, data=data)

        # If the response was successful, no Exception will be raised.
        response.raise_for_status()
    except HTTPError as http_err:
        print("HTTP error occurred: %s" % http_err)
        if (response.status_code == 400):
            if len(response.text) > 0:
                print("response.text:\n%s" % response.text)
    except Exception as err:
        print("Other error occurred: %s" % err)
    else:
        if len(response.text) > 0:
                print("response.text:\n%s" % response.text)
        else:
            print("Success!")

    # Print empty line.
    print


if __name__ == '__main__':
    # Prepare Digital Output URL.
    do00Status_URL =  "http://"+MOXA_IP+"/api/slot/0/io/do/0/doStatus"

    # Prepare GET headers.
    getHeaders = {'Content-Type': 'application/json', 'Accept': 'vdn.dac.v1'}

    # Use GET method to get DO-00 Status.
    print("1) GET DigitalOutput00_Status response...")
    getData(do00Status_URL, getHeaders)
    ## Result => HTTP error occurred: 404 Client Error: Not found

    # Prepare DO-00_ON data.
    do00Status_ON = '{"slot":0,"io":{"do":{"0":{"doStatus":1}}}}'

    # Prepare DO-00_OFF data.
    do00Status_OFF = '{"slot":0,"io":{"do":{"0":{"doStatus":0}}}}'

    # Prepare PUT headers.
    putHeaders_do00Status = getHeaders.copy()
    putHeaders_do00Status['Content-Length'] = len(json.dumps(do00Status_OFF))  # do00Status_ON/OFF is the same length.

    # PUT Counter data & print result.
    print("2a) PUT DO-00_ON response...")
    putData(do00Status_URL, putHeaders_do00Status, do00Status_ON)
    ## Result => HTTP error occurred: 404 Client Error: Not found

    # GET DO-00_Status Info data & print them.
    print("2b) GET DO-00_Status response...")
    getData(do00Status_URL, getHeaders)
    ## Result => HTTP error occurred: 404 Client Error: Not found

    time.sleep(3);

    # PUT Counter data & print result.
    print("2c) PUT DO-00_OFF response...")
    putData(do00Status_URL, putHeaders_do00Status, do00Status_OFF)
    ## Result => HTTP error occurred: 404 Client Error: Not found

    # GET DO-00_Status Info data & print them.
    print("2d) GET DO-00_Status response...")
    getData(do00Status_URL, getHeaders)
    ## Result => HTTP error occurred: 404 Client Error: Not found
python rest
1个回答
0
投票

只是挖了一个洞,发现Moxa E1214具有继电器形式的数字输出。 (DO!=中继)。

因此,URL应该是:

   "http://"+MOXA_IP+"/api/slot/0/io/relay/0/relayStatus".

和用于控制的json不同:

    # Prepare DO-00_ON data.
    do00Status_ON = '{"slot":"0","io":{"relay":{"0":{"relayStatus":"0"}}}}'

    # Prepare DO-00_OFF data.
    do00Status_OFF = '{"slot":"0","io":{"relay":{"0":{"relayStatus":"1"}}}}'

第三件事是行:

   putHeaders_do00Status['Content-Length'] = len(json.dumps(do00Status_OFF))

有效的方法:

   putHeaders_do00Status['Content-Length'] = str(len(do00Status_OFF))

do00Status_OFF是字符串,无需转储字符串的json,并且标头值必须为字节或字符串(我认为是lib要求)。

希望有帮助。

热门问题
推荐问题
最新问题