Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection not bindig #722

Open
fenilGhoghari opened this issue Nov 16, 2023 · 2 comments
Open

Connection not bindig #722

fenilGhoghari opened this issue Nov 16, 2023 · 2 comments

Comments

@fenilGhoghari
Copy link

Hello @stephane ,

I have used your library in QT framework and i am trying to make one QT application but when I first time launch my application at a time i am able to make connection but when i dis-connect with current client and using same IP and PORT at a time my server is not closed and try to connect with my client but i am not able to connect with my second client can you help me to close the Binding hear.

I am sure here port is not closed completely and this make issue to new connection. I didn't modify your disconnect function and i am using as it is you have written.

Environment I used:
System : Windows 10 and Debian
QT version: 5.15.0

How I tested?
-> First launch application using QT Creature
-> Launch Server
-> Connect with client ( Success)
-> Client Disconnect ( Success)
-> Relaunch Client ( Same IP and Port) ( Fail)

Expected Outcome:
-> Server Able to connect with Client

I hope you will help me out to solve this issue I will be grateful to you if you help me here!!!

My Code for Server

if (strippedIP == "")
        {
            mainWin->showUpInfoBar(tr("Connection failed\nWrong IP Address."), MyInfoBar::Error);
            QLOG_ERROR() << "Connection failed. Blank IP Address";
            return;
        }
        else
        {
            m_modbus = modbus_new_tcp(strippedIP.toLatin1().constData(), port);
            mainWin->hideInfoBar();
            QLOG_INFO() << "Connecting to IP : " << ip << ":" << port;
        }
//        m_timeOut = timeOut;

        mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, MODBUS_MAX_READ_REGISTERS, 0);
           if (mb_mapping == NULL)
           {
               fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
               modbusDisConnect();
               return;
           }
           else
           {
               QLOG_INFO() << "Mapping Success";
           }
    server_socket = modbus_tcp_listen(m_modbus, NB_CONNECTION);

    if (server_socket == -1)
        {
            fprintf(stderr, "Unable to listen TCP connection\n");
            modbusDisConnect();
            return;
        }
        else
        {
            QLOG_INFO() << "Listening for connections...";
        }

    if(modbus_tcp_accept(m_modbus, &server_socket)<0){
        fprintf(stderr, "Failed to accept connection: %s\n", modbus_strerror(errno));
        m_connected = false;
    }else {
        // Connection established, start the data sending timer
        m_connected = true;
        data_sendTimer->start(1000);

Disconnect Function

void ModbusAdapter::modbusDisConnect()
{
    //Modbus disconnect

    QLOG_INFO()<<  "Modbus disconnected";

    if(m_modbus) {
        QLOG_INFO()<<  "Modbus disconnected process";
        modbus_mapping_free(mb_mapping);
        modbus_close(m_modbus);
        modbus_free(m_modbus);
        free(m_modbus->backend_data);
        m_modbus = NULL;
    }

    m_connected = false;

    m_ModBusMode = EUtils::None;

}

Thanks & Regards,
Fenil

@ladmanj
Copy link

ladmanj commented Nov 20, 2023 via email

@stephane
Copy link
Owner

@ladmanj could you confirm the issue is still present (docs and code). I don't understand why calling modbus_close from C++ code would be an issue. Do you a snippet C++ code to compile to observe the issue?

BTW don't free(m_modbus->backend_data) after a call to modbus_free.

https://github.com/stephane/libmodbus/blob/master/src/modbus-rtu.c#L1169
https://github.com/stephane/libmodbus/blob/master/src/modbus-tcp.c#L816

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants