我早些时候尝试了相同的代码,这是完美的运行。后来我编程了 使用“ LOGO!Soft Comfort 8.2”的“ Siemens LOGO 12/24 rce”,即使这样也可以完美工作。
但是当我使用Python重新编程时,它显示: 发生异常:ConnectionResetError [WinError 10054]远程主机强行关闭了现有连接
代码:
#include <utility> // std::pair, std::make_pair
#include <string> // std::string
#include <iostream> // std::cout
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
using namespace std;
struct test
{
int x;
int *y;
map<string, uintptr_t> *m;
};
int main ()
{
int x=11,y=22;
void *x1 = &x, *y1=&y;
test *t1 = static_cast<test*>(calloc(1,sizeof(test)));
t1->m = new map<string, uintptr_t>();
t1->m->emplace(string("x"),reinterpret_cast<std::uintptr_t>(x1));
t1->m->emplace(string("y"),reinterpret_cast<std::uintptr_t>(y1));
cout<<"====== retrive the map in a calloc memory area==="<<endl;
auto itr = t1->m->find(string("x"));
if(itr != t1->m->end())
{
int *i = reinterpret_cast<int*>(itr->second);
cout<<"found x="<<*i<<endl;
}
return 0;
}
错误:
from __future__ import print_function
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_tcp, hooks
import logging
import time
def main():
"""main"""
logger = modbus_tk.utils.create_logger("console", level=logging.DEBUG)
try:
def on_before_connect(args):
master = args[0]
logger.debug("on_before_connect {0} {1}".format(master._host, master._port))
hooks.install_hook("modbus_tcp.TcpMaster.before_connect", on_before_connect)
def on_after_recv(args):
response = args[1]
logger.debug("on_after_recv {0} bytes received".format(len(response)))
print(type(master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 64)))
hooks.install_hook("modbus_tcp.TcpMaster.after_recv", on_after_recv)
# Connect to the slave
master = modbus_tcp.TcpMaster()
master._host = "192.168.0.150"
master._port = 502
master.set_timeout(5.0)
logger.info("connected")
while(True):
logger.info(master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 64))
time.sleep(0.2)
except modbus_tk.modbus.ModbusError as exc:
logger.error("%s- Code=%d", exc, exc.get_exception_code())
if __name__ == "__main__":
main()
请帮助解决此问题。...提前感谢
答案 0 :(得分:0)
首先,我建议您检查代码中的缩进级别,因为现在看起来有点混乱。
然后,请使用以下方法连接到您的设备:
master = modbus_tcp.TcpMaster("192.168.0.150")
我从来没有像您这样使用过它。 _host
和_port
是不是某种私有变量,因此您无法正确连接到设备?