pythonwindowssocketsmodbuslibmodbus

Closing a socket created in a dll


I'm creating a modbus slave using the libmodbus.dll in python on Windows OS. I've used ctypes to load the dll and make use of its features. There is a sample code I'm mimicking here. One of the call's in the dll ends up opening a socket

s = modbus_tcp_listen(ctx, 1);

At the end of the example it has a line where it close's the socket like so

close(s);

close() is not part of the libmodbus.dll. It's from the library #include <unistd.h>. Is there a way to close the socket without having to obtain a dll with the close() functionality? If not, how would I obtain access to the close() command on a windows platform? From what I know unistd.h is for posix.


Solution

  • You can use the socket.close(fd) which takes the socket descriptor and closed it.

    import socket
    ...
    server_socket = mbpi.modbus_tcp_listen(self._ctx, 1)
    ...
    socket.close(server_socket.value) # Need to call attribute value since its a ctypes.c_int