gpt4 book ai didi

python - 通过 RESTful API 使用 python 与 MOXA ioLogik1214 进行连接,以将数据放入 DO 端口

转载 作者:太空宇宙 更新时间:2023-11-03 20:13:12 25 4
gpt4 key购买 nike

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

我尝试将数字输入 DI-00 配置为 diCounter channel ,并且能够读取和写入该 channel 。

但是,当我尝试写入 DO-00,配置为正常输出 channel 时,我遇到了:

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

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

我的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

最佳答案

刚刚挖了同样的洞,发现Moxa E1214有继电器形式的数字输出。 (DO!= 中继)。

因此网址应该是:

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

和控制的jsons不同:

    # 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,并且 header 值需要是字节或字符串(我认为请求 lib 要求)。

希望有帮助。

关于python - 通过 RESTful API 使用 python 与 MOXA ioLogik1214 进行连接,以将数据放入 DO 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58602654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com