Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Mobile
  4. MODBUS TCP with multiple nodes

MODBUS TCP with multiple nodes

Scheduled Pinned Locked Moved Mobile
wpfwcfsysadminquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10265805
    wrote on last edited by
    #1

    I'm working on win ce 6 modbus tcp client server, application is developed for a client server communication and it is working fine. now req is my slave device should respond to the diff slave addresses polled by master/client. Can I just change slave id and establish connection or I need to close previous connection and again establish new one below is the code, which is working fine for one node, if I polled with other node ID then it gives exception. what change it req to communicate with other node simultaneously. My device should be able to communicate with 32 diff nodes on modbus tcp. Shall I create individual threads for each node but how they will communicate on same port? before establishing connection with other node shall I close previous node?

    startupServer(int slaveAddr, const TCHAR * const hostName)
    {

    int result;
    int tcpOption;
    struct sockaddr_in hostAddress;

    if (isStarted())
    return (FTALK_ILLEGAL_STATE_ERROR);

    // Note: For TCP we allow 0 as slave address, -1 means ignore slave adr
    if ((slaveAddr < -1) || (slaveAddr > 255))
    return (FTALK_ILLEGAL_ARGUMENT_ERROR);
    this->slaveAddr = slaveAddr;

    //
    // Special treatment for the Win32 platform, needs to load WinSock DLL
    //
    #ifdef _WINSOCKAPI_
    WSADATA wsaData;

    result = WSAStartup(0x0101, &wsaData);
    if (result != 0)
    return (FTALK_SOCKET_LIB_ERROR);
    #endif

    //
    // Open socket
    //
    listenSocket = socket(PF_INET, SOCK_STREAM, 0);
    if (listenSocket == INVALID_SOCKET)
    {
    shutdownServer();
    return (FTALK_OPEN_ERR);
    }

    //
    // Configure listen socket options (we ignore errors here)
    //
    #ifdef SO_REUSEADDR
    tcpOption = 1; // Enable option
    setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR,
    (char *) &tcpOption, sizeof (tcpOption));
    #endif

    //
    // Binding the listen socket to the port
    //
    hostAddress.sin_family = AF_INET;
    if ((hostName == NULL) || (hostName[0] == '\0'))
    hostAddress.sin_addr.s_addr = htonl(INADDR_ANY);
    else
    {
    hostAddress.sin_addr.s_addr = inet_addr((char *) hostName);
    #if !defined(__VXWORKS__) // We don't support host name resolving with VxWorks
    if (hostAddress.sin_addr.s_addr == INADDR_NONE)
    {
    struct hostent *hostInfo;

         hostInfo = gethostbyname((char \*) hostName);
    
         if (hostInfo == NULL)
            return (FTALK\_TCPIP\_CONNECT\_ERR);
         hostAddress.sin\_addr = \*(struct in\_addr \*) hostIn
    
    L S 2 Replies Last reply
    0
    • U User 10265805

      I'm working on win ce 6 modbus tcp client server, application is developed for a client server communication and it is working fine. now req is my slave device should respond to the diff slave addresses polled by master/client. Can I just change slave id and establish connection or I need to close previous connection and again establish new one below is the code, which is working fine for one node, if I polled with other node ID then it gives exception. what change it req to communicate with other node simultaneously. My device should be able to communicate with 32 diff nodes on modbus tcp. Shall I create individual threads for each node but how they will communicate on same port? before establishing connection with other node shall I close previous node?

      startupServer(int slaveAddr, const TCHAR * const hostName)
      {

      int result;
      int tcpOption;
      struct sockaddr_in hostAddress;

      if (isStarted())
      return (FTALK_ILLEGAL_STATE_ERROR);

      // Note: For TCP we allow 0 as slave address, -1 means ignore slave adr
      if ((slaveAddr < -1) || (slaveAddr > 255))
      return (FTALK_ILLEGAL_ARGUMENT_ERROR);
      this->slaveAddr = slaveAddr;

      //
      // Special treatment for the Win32 platform, needs to load WinSock DLL
      //
      #ifdef _WINSOCKAPI_
      WSADATA wsaData;

      result = WSAStartup(0x0101, &wsaData);
      if (result != 0)
      return (FTALK_SOCKET_LIB_ERROR);
      #endif

      //
      // Open socket
      //
      listenSocket = socket(PF_INET, SOCK_STREAM, 0);
      if (listenSocket == INVALID_SOCKET)
      {
      shutdownServer();
      return (FTALK_OPEN_ERR);
      }

      //
      // Configure listen socket options (we ignore errors here)
      //
      #ifdef SO_REUSEADDR
      tcpOption = 1; // Enable option
      setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR,
      (char *) &tcpOption, sizeof (tcpOption));
      #endif

      //
      // Binding the listen socket to the port
      //
      hostAddress.sin_family = AF_INET;
      if ((hostName == NULL) || (hostName[0] == '\0'))
      hostAddress.sin_addr.s_addr = htonl(INADDR_ANY);
      else
      {
      hostAddress.sin_addr.s_addr = inet_addr((char *) hostName);
      #if !defined(__VXWORKS__) // We don't support host name resolving with VxWorks
      if (hostAddress.sin_addr.s_addr == INADDR_NONE)
      {
      struct hostent *hostInfo;

           hostInfo = gethostbyname((char \*) hostName);
      
           if (hostInfo == NULL)
              return (FTALK\_TCPIP\_CONNECT\_ERR);
           hostAddress.sin\_addr = \*(struct in\_addr \*) hostIn
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You already posted this in the C++ forum. Please do not crosspost.

      Veni, vidi, abiit domum

      1 Reply Last reply
      0
      • U User 10265805

        I'm working on win ce 6 modbus tcp client server, application is developed for a client server communication and it is working fine. now req is my slave device should respond to the diff slave addresses polled by master/client. Can I just change slave id and establish connection or I need to close previous connection and again establish new one below is the code, which is working fine for one node, if I polled with other node ID then it gives exception. what change it req to communicate with other node simultaneously. My device should be able to communicate with 32 diff nodes on modbus tcp. Shall I create individual threads for each node but how they will communicate on same port? before establishing connection with other node shall I close previous node?

        startupServer(int slaveAddr, const TCHAR * const hostName)
        {

        int result;
        int tcpOption;
        struct sockaddr_in hostAddress;

        if (isStarted())
        return (FTALK_ILLEGAL_STATE_ERROR);

        // Note: For TCP we allow 0 as slave address, -1 means ignore slave adr
        if ((slaveAddr < -1) || (slaveAddr > 255))
        return (FTALK_ILLEGAL_ARGUMENT_ERROR);
        this->slaveAddr = slaveAddr;

        //
        // Special treatment for the Win32 platform, needs to load WinSock DLL
        //
        #ifdef _WINSOCKAPI_
        WSADATA wsaData;

        result = WSAStartup(0x0101, &wsaData);
        if (result != 0)
        return (FTALK_SOCKET_LIB_ERROR);
        #endif

        //
        // Open socket
        //
        listenSocket = socket(PF_INET, SOCK_STREAM, 0);
        if (listenSocket == INVALID_SOCKET)
        {
        shutdownServer();
        return (FTALK_OPEN_ERR);
        }

        //
        // Configure listen socket options (we ignore errors here)
        //
        #ifdef SO_REUSEADDR
        tcpOption = 1; // Enable option
        setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR,
        (char *) &tcpOption, sizeof (tcpOption));
        #endif

        //
        // Binding the listen socket to the port
        //
        hostAddress.sin_family = AF_INET;
        if ((hostName == NULL) || (hostName[0] == '\0'))
        hostAddress.sin_addr.s_addr = htonl(INADDR_ANY);
        else
        {
        hostAddress.sin_addr.s_addr = inet_addr((char *) hostName);
        #if !defined(__VXWORKS__) // We don't support host name resolving with VxWorks
        if (hostAddress.sin_addr.s_addr == INADDR_NONE)
        {
        struct hostent *hostInfo;

             hostInfo = gethostbyname((char \*) hostName);
        
             if (hostInfo == NULL)
                return (FTALK\_TCPIP\_CONNECT\_ERR);
             hostAddress.sin\_addr = \*(struct in\_addr \*) hostIn
        
        S Offline
        S Offline
        Sunasara Imdadhusen
        wrote on last edited by
        #3

        please try to post minimum code to get higher chance for getting answer from the experts, because on one has time to read your thousands lines of code.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups