I am using the libnodave library to communicate with a Siemens S7 PLC. It works fine, until I try to write bytes. I almost always times out.

The code I have to connect:

fds.rfd = libnodave.openSocket(port, address.ToString());
fds.wfd = fds.rfd;

if (fds.rfd > 0) {
    di = new libnodave.daveInterface(fds, "IF1", 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k);
    di.setTimeout(10000000);

    dc = new libnodave.daveConnection(di, 0, 0, 2);

    if (0 == dc.connectPLC()) {
        _status.connected = true;
    }
}

and to write the bytes (in another method, called in a later stage):

dc.writeBytes(libnodave.daveDB, datablock, address, bytestowrite, buftosend);

which almost always returns -1025 (timeout).

Changing the timeout, or moving the connect routine directly before the writing has no effect.

有帮助吗?

解决方案

As always as things get weird, and there is multithreading going on, there is the problem.

I was calling writeBytes while there was a call of readBytes still going on, messing up the results.

Adding a mutex and better timing fixed it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top