• AVR Freaks

Hot!ModbusTCP implementation using MPLAB Harmony (any version)

Author
ravic
think_do_repeat
  • Total Posts : 137
  • Reward points : 0
  • Joined: 2016/02/04 03:02:20
  • Location: Pune, MH, India
  • Status: offline
2016/11/23 21:05:47 (permalink)
0

ModbusTCP implementation using MPLAB Harmony (any version)

Hi,
There was default TCPIP demo application in MLA library, application note to implement the modbusTCP (AN1568) and sample code for same. Is there similar kind of application note or sample code for implementing modbusTCP on PIC32MX795F512L using MPLAB Harmony?
Any help is appreciated!
thanks
#1

9 Replies Related Threads

    rainad
    Moderator
    • Total Posts : 1190
    • Reward points : 0
    • Joined: 2009/05/01 13:39:25
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2016/11/25 11:09:45 (permalink)
    3 (1)
    There is no such AN or sample code for this.
    However, translation from MLA TCP/IP to Harmony should be straightforward, since the socket API is similar.
    The stack initialization and the rest is very different, but if you start from a working Harmony demo, then you can easily convert an existing TCP/UDP application.. There is a porting guide as part of the documentation.
     
    #2
    jipihorn
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2015/08/13 04:04:01
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2016/11/28 09:15:21 (permalink)
    3 (1)
    Hello,
     
    Don't use this sample code, it is absolutely crap.
    As I did, just get the Modbus specifications and create/read frames using a socket from the IP stack.
    It is quite straightforward, this protocol is very simple.You will take much more time to understand and debug crap code instead of writing your own. It took me a couple of days to fully implement the Modbus as client and server with a PIc32 and the specifications.
     
    Using a tool like ModbusMaster (qModMaster) or Ananas is very useful, especially the first one that shows the content of the frames.
     
    Now I have to implement Profinet or OPC-DA on pic32, but it is an other story...
     
    Jerome.
    #3
    skynet78
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2009/03/17 06:30:41
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2016/12/20 07:17:27 (permalink)
    3 (1)
    Hi, 
    see my simple ModbusTCP app
    /*****************************************************************************
      Function:
     void MODBUSTCPServer(void)

      Summary:
     Implements a MODBUS_RX TCP Server.

      Precondition:
     TCP is initialized.

     * Parameters:
     None

      Returns:
       None
      ***************************************************************************/
    void MODBUSTCPServer(void)
    {
        static WORD w;
        WORD wMaxGet;
        TCP_SOCKET MySocket;

        static bool isInit = false;
        static BOOL MB_Busy = false;

        if (!isInit){
            for (SKT_Idx = 0; SKT_Idx< MAX_MODBUS_TCP_SOKET; SKT_Idx++){
                w_Vett[SKT_Idx] = 0;
                wMaxGet_Vett[SKT_Idx] = 0;
                MySocket_Vett[SKT_Idx] = INVALID_SOCKET;
                MB_State[SKT_Idx] = SM_MTCP_HOME;
            }
            isInit = true;
        }

        if (!MB_Busy)
        {
           SKT_Idx++;
           if (SKT_Idx >= MAX_MODBUS_TCP_SOKET)
               SKT_Idx = 0;
        }

        w = w_Vett[SKT_Idx];
        wMaxGet = wMaxGet_Vett[SKT_Idx];
        MySocket = MySocket_Vett[SKT_Idx];
        MODBUSTCPServerDATA.state = MB_State[SKT_Idx];

        // Reset our state if the remote client disconnected from us
        if(MySocket != INVALID_SOCKET)
        {
                if(TCPIP_TCP_WasReset(MySocket))
                MODBUSTCPServerDATA.state = SM_RECEIVE_DATA;
        }

            switch(MODBUSTCPServerDATA.state)
            {
                    case SM_MTCP_HOME:
                            MySocket = TCPIP_TCP_ServerOpen(IP_ADDRESS_TYPE_IPV4, 502, 0);

                             if(MySocket == INVALID_SOCKET)
                                    return;

                            MODBUSTCPServerDATA.conn_state = false;
                            MODBUSTCPServerDATA.state = SM_RECEIVE_DATA;
                            break;

                    case SM_RECEIVE_DATA:
                            // See if anyone is connected to us
                            if(!TCPIP_TCP_IsConnected(MySocket) || MB_Busy)
                                return;

                            MODBUSTCPServerDATA.conn_state = true;
                            // Figure out how many bytes have been received and how many we can transmit.
                            wMaxGet = TCPIP_TCP_GetIsReady(MySocket); // Get TCP RX FIFO byte count

                            if(wMaxGet>0 && wMaxGet<MODBUS_RX_BUFFER_SIZE)
                            {
                                MB_Busy = true;
                                TCPIP_TCP_ArrayGet(MySocket, MODBUS_RX, wMaxGet);
                                ProcessReceivedMessage();
                                TCPreceCount = wMaxGet-6;

                                if(MODBUS_COMMAND.UnitID == localAddr && !Illegal_Function)
                                {
                                    switch (MODBUS_COMMAND.FunctionCode)
                                    {
             case READ_COIL:
              TCPreadCoil();
              //Assemble the data
              //Function code
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = MODBUS_TX[8];
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x03 + MODBUS_RX[8];

              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 9);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x81))
               w = 0x09 + MODBUS_TX[8];
              else
               w = 9;

              MODBUSTCPServerDATA.state = SM_SEND_DATA;
              break;

             case READ_DISCRETE_INPUTS:
              TCPreadInputStatus();

              //Assemble the data
              //Function code
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = MODBUS_TX[8];
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x03 + MODBUS_RX[8];

              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 9);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x82))
               w = 0x09 + MODBUS_TX[8];
              else
               w = 9;

              MODBUSTCPServerDATA.state = SM_SEND_DATA;
              break;

             case READ_HOLDING_REGISTER:
              TCPreadRegisters();

              //Assemble the data
              //Function code
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = (char)MODBUS_COMMAND.NumberOfRegister.Val*2;
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x03 + MODBUS_RX[8];

              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 9);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x83))
               w = 0x09 + MODBUS_TX[8];
              else
               w = 9;

              MODBUSTCPServerDATA.state = SM_SEND_DATA;
              break;

             case READ_INPUT_REGISTER:
              TCPreadInputRegister();

              //Assemble the data to be sent
              //Function code
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = (char)MODBUS_COMMAND.NumberOfRegister.Val*2;
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x03 + MODBUS_RX[8];

              //Copy MODBUS_RX and HOLEING_REG into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 9);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x84))
               w = 0x09 + MODBUS_TX[8];
              else
               w = 9;

              MODBUSTCPServerDATA.state = SM_SEND_DATA;
              break;

                                        case WRITE_SINGLE_COIL:
              TCPforceSingleCoil();
             
              //Assemble the data to be sent
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = 0x00; //Byte Count
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x06;

              //Copy MODBUS_RX into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 12);
              w = 12;
              MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                            break;

                                        case WRITE_SINGLE_REGISTER:
                                            TCPpresetSingleRegister();
              
              //Assemble the data
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x06;

              //Copy MODBUS_RX into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 12);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x86))
               w = 12;
              else
               w = 9;
               
              MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                            break;

                                        case WRITE_MULTIPLE_COILS:
                                            TCPforceMultipleCoils();
              
              //Assemble the data to be sent
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;

              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x06;

              //Copy MODBUS_RX into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 12);
              w = 12;
              
              MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                            break;

                                        case WRITE_MULTIPLE_REGISTER:
                                            TCPpresetMultipleRegisters();
              
              //Assemble the data
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              //MODBUS_RX[8] = 0x00; //Byte Count
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x06;

              //Copy MODBUS_RX into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 12);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x90))
               w = 12;
              else
               w = 9;
              
              MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                            break;
                                    }
                                }
                                else
                                {
                                    MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                }
                                CounterSocketNoSpace[SKT_Idx] = 0;
                            }
                            break;

                    case SM_SEND_DATA:
                            // See if we're ready to write data
                            if(TCPIP_TCP_PutIsReady(MySocket) < w)
                            {
                                if(CounterSocketNoSpace[SKT_Idx] < 3)
                                {
                                    CounterSocketNoSpace[SKT_Idx]++;
                                    break;
                                }
                                else
                                {
                                    ModbusError(0x03); // Illegal Data Value
                                    Illegal_Function = 1;
                                }
                            }

                            if(!Illegal_Function)
                            {
                                TCPIP_TCP_ArrayPut(MySocket, MODBUS_TX, w);
                                TCPIP_TCP_Flush(MySocket);
                            }
                            // If the client requested a function which is not supported by this server
                            else
                            {
                                TCPIP_TCP_ArrayPut(MySocket, MODBUS_TX, 9);
                                TCPIP_TCP_Flush(MySocket);
                                Illegal_Function = 0;
                            }
                            MODBUSTCPServerDATA.state = SM_RECEIVE_DATA;
                            MB_Busy = false;
                            break;
                    }

            w_Vett[SKT_Idx] = w;
            wMaxGet_Vett[SKT_Idx] = wMaxGet;
            MySocket_Vett[SKT_Idx] = MySocket;
            MB_State[SKT_Idx] = MODBUSTCPServerDATA.state;

    }
    #endif //#if defined(STACK_USE_MODBUS_TCP_SERVER)

     
    The sub function are similar to
    //------------------------------------------------------
    // READ REGISTER Function
    //------------------------------------------------------
    void TCPreadRegisters(void)
    {
     WORD tempAddr;
     //WORD_VAL crcData;
     WORD byteCount;
     WORD i;
     WORD_VAL tempData;
     
     tempAddr = (MODBUS_COMMAND.StartAddress.Val & MB_TCP_ADDRESS_MASK);
     byteCount = MODBUS_COMMAND.NumberOfRegister.Val * 2;
     
     tempData.Val = 0;
     for(i=0;i<byteCount;i+=2,tempAddr++)
     {
      TCPgetRegisterVal(tempAddr,&tempData.Val);
      MODBUS_TX[i+9] = tempData.byte.HB;
      MODBUS_TX[i+10] = tempData.byte.LB;
      if((i+8) >= MODBUS_TX_BUFFER_SIZE)
       break;
     }
     
     MODBUS_TX[6] = localAddr;
     MODBUS_TX[7] = 0x03;
     MODBUS_TX[8] = byteCount;
     byteCount += 3;
    }

     
    //------------------------------------------------------
    // GET REGISTER VALUE Function
    //------------------------------------------------------
    void TCPgetRegisterVal(WORD addr, WORD *tempData)
    {
    if(addr<LIMIT_SETTINGS_REGISTER)
        {
            switch(addr)
            {
            case 0:
                *tempData = value1;
                break;
            case 1:
                *tempData = value2;
                break;

            default:
                *tempData = 0x0000;
                break;
            }
        }
    }

    please contact me for other info.
    #4
    Wulffdk
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2014/01/06 03:57:57
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2017/03/21 06:08:42 (permalink)
    0
    skynet78
    Hi, 
    see my simple ModbusTCP app
    /*****************************************************************************
      Function:
     void MODBUSTCPServer(void)

      Summary:
     Implements a MODBUS_RX TCP Server.

      Precondition:
     TCP is initialized.

     * Parameters:
     None

      Returns:
       None
      ***************************************************************************/
    void MODBUSTCPServer(void)
    {
        static WORD w;
        WORD wMaxGet;
        TCP_SOCKET MySocket;

        static bool isInit = false;
        static BOOL MB_Busy = false;

        if (!isInit){
            for (SKT_Idx = 0; SKT_Idx< MAX_MODBUS_TCP_SOKET; SKT_Idx++){
                w_Vett[SKT_Idx] = 0;
                wMaxGet_Vett[SKT_Idx] = 0;
                MySocket_Vett[SKT_Idx] = INVALID_SOCKET;
                MB_State[SKT_Idx] = SM_MTCP_HOME;
            }
            isInit = true;
        }

        if (!MB_Busy)
        {
           SKT_Idx++;
           if (SKT_Idx >= MAX_MODBUS_TCP_SOKET)
               SKT_Idx = 0;
        }

        w = w_Vett[SKT_Idx];
        wMaxGet = wMaxGet_Vett[SKT_Idx];
        MySocket = MySocket_Vett[SKT_Idx];
        MODBUSTCPServerDATA.state = MB_State[SKT_Idx];

        // Reset our state if the remote client disconnected from us
        if(MySocket != INVALID_SOCKET)
        {
                if(TCPIP_TCP_WasReset(MySocket))
                MODBUSTCPServerDATA.state = SM_RECEIVE_DATA;
        }

            switch(MODBUSTCPServerDATA.state)
            {
                    case SM_MTCP_HOME:
                            MySocket = TCPIP_TCP_ServerOpen(IP_ADDRESS_TYPE_IPV4, 502, 0);

                             if(MySocket == INVALID_SOCKET)
                                    return;

                            MODBUSTCPServerDATA.conn_state = false;
                            MODBUSTCPServerDATA.state = SM_RECEIVE_DATA;
                            break;

                    case SM_RECEIVE_DATA:
                            // See if anyone is connected to us
                            if(!TCPIP_TCP_IsConnected(MySocket) || MB_Busy)
                                return;

                            MODBUSTCPServerDATA.conn_state = true;
                            // Figure out how many bytes have been received and how many we can transmit.
                            wMaxGet = TCPIP_TCP_GetIsReady(MySocket); // Get TCP RX FIFO byte count

                            if(wMaxGet>0 && wMaxGet<MODBUS_RX_BUFFER_SIZE)
                            {
                                MB_Busy = true;
                                TCPIP_TCP_ArrayGet(MySocket, MODBUS_RX, wMaxGet);
                                ProcessReceivedMessage();
                                TCPreceCount = wMaxGet-6;

                                if(MODBUS_COMMAND.UnitID == localAddr && !Illegal_Function)
                                {
                                    switch (MODBUS_COMMAND.FunctionCode)
                                    {
             case READ_COIL:
              TCPreadCoil();
              //Assemble the data
              //Function code
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = MODBUS_TX[8];
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x03 + MODBUS_RX[8];

              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 9);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x81))
               w = 0x09 + MODBUS_TX[8];
              else
               w = 9;

              MODBUSTCPServerDATA.state = SM_SEND_DATA;
              break;

             case READ_DISCRETE_INPUTS:
              TCPreadInputStatus();

              //Assemble the data
              //Function code
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = MODBUS_TX[8];
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x03 + MODBUS_RX[8];

              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 9);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x82))
               w = 0x09 + MODBUS_TX[8];
              else
               w = 9;

              MODBUSTCPServerDATA.state = SM_SEND_DATA;
              break;

             case READ_HOLDING_REGISTER:
              TCPreadRegisters();

              //Assemble the data
              //Function code
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = (char)MODBUS_COMMAND.NumberOfRegister.Val*2;
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x03 + MODBUS_RX[8];

              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 9);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x83))
               w = 0x09 + MODBUS_TX[8];
              else
               w = 9;

              MODBUSTCPServerDATA.state = SM_SEND_DATA;
              break;

             case READ_INPUT_REGISTER:
              TCPreadInputRegister();

              //Assemble the data to be sent
              //Function code
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = (char)MODBUS_COMMAND.NumberOfRegister.Val*2;
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x03 + MODBUS_RX[8];

              //Copy MODBUS_RX and HOLEING_REG into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 9);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x84))
               w = 0x09 + MODBUS_TX[8];
              else
               w = 9;

              MODBUSTCPServerDATA.state = SM_SEND_DATA;
              break;

                                        case WRITE_SINGLE_COIL:
              TCPforceSingleCoil();
             
              //Assemble the data to be sent
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              MODBUS_RX[8] = 0x00; //Byte Count
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x06;

              //Copy MODBUS_RX into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 12);
              w = 12;
              MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                            break;

                                        case WRITE_SINGLE_REGISTER:
                                            TCPpresetSingleRegister();
              
              //Assemble the data
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x06;

              //Copy MODBUS_RX into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 12);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x86))
               w = 12;
              else
               w = 9;
               
              MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                            break;

                                        case WRITE_MULTIPLE_COILS:
                                            TCPforceMultipleCoils();
              
              //Assemble the data to be sent
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;

              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x06;

              //Copy MODBUS_RX into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 12);
              w = 12;
              
              MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                            break;

                                        case WRITE_MULTIPLE_REGISTER:
                                            TCPpresetMultipleRegisters();
              
              //Assemble the data
              MODBUS_RX[7] = MODBUS_COMMAND.FunctionCode;
              //MODBUS_RX[8] = 0x00; //Byte Count
              //Length
              MODBUS_RX[4] = 0x00;
              MODBUS_RX[5] = 0x06;

              //Copy MODBUS_RX into MODBUS_TX and send MODBUS_TX as response
              memcpy((void*)MODBUS_TX, (void*)MODBUS_RX, 12);

              //Test if server sends Exception error to the client
              if (!(MODBUS_TX[7] == 0x90))
               w = 12;
              else
               w = 9;
              
              MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                            break;
                                    }
                                }
                                else
                                {
                                    MODBUSTCPServerDATA.state = SM_SEND_DATA;
                                }
                                CounterSocketNoSpace[SKT_Idx] = 0;
                            }
                            break;

                    case SM_SEND_DATA:
                            // See if we're ready to write data
                            if(TCPIP_TCP_PutIsReady(MySocket) < w)
                            {
                                if(CounterSocketNoSpace[SKT_Idx] < 3)
                                {
                                    CounterSocketNoSpace[SKT_Idx]++;
                                    break;
                                }
                                else
                                {
                                    ModbusError(0x03); // Illegal Data Value
                                    Illegal_Function = 1;
                                }
                            }

                            if(!Illegal_Function)
                            {
                                TCPIP_TCP_ArrayPut(MySocket, MODBUS_TX, w);
                                TCPIP_TCP_Flush(MySocket);
                            }
                            // If the client requested a function which is not supported by this server
                            else
                            {
                                TCPIP_TCP_ArrayPut(MySocket, MODBUS_TX, 9);
                                TCPIP_TCP_Flush(MySocket);
                                Illegal_Function = 0;
                            }
                            MODBUSTCPServerDATA.state = SM_RECEIVE_DATA;
                            MB_Busy = false;
                            break;
                    }

            w_Vett[SKT_Idx] = w;
            wMaxGet_Vett[SKT_Idx] = wMaxGet;
            MySocket_Vett[SKT_Idx] = MySocket;
            MB_State[SKT_Idx] = MODBUSTCPServerDATA.state;

    }
    #endif //#if defined(STACK_USE_MODBUS_TCP_SERVER)

     
    The sub function are similar to
    //------------------------------------------------------
    // READ REGISTER Function
    //------------------------------------------------------
    void TCPreadRegisters(void)
    {
     WORD tempAddr;
     //WORD_VAL crcData;
     WORD byteCount;
     WORD i;
     WORD_VAL tempData;
     
     tempAddr = (MODBUS_COMMAND.StartAddress.Val & MB_TCP_ADDRESS_MASK);
     byteCount = MODBUS_COMMAND.NumberOfRegister.Val * 2;
     
     tempData.Val = 0;
     for(i=0;i<byteCount;i+=2,tempAddr++)
     {
      TCPgetRegisterVal(tempAddr,&tempData.Val);
      MODBUS_TX[i+9] = tempData.byte.HB;
      MODBUS_TX[i+10] = tempData.byte.LB;
      if((i+8) >= MODBUS_TX_BUFFER_SIZE)
       break;
     }
     
     MODBUS_TX[6] = localAddr;
     MODBUS_TX[7] = 0x03;
     MODBUS_TX[8] = byteCount;
     byteCount += 3;
    }

     
    //------------------------------------------------------
    // GET REGISTER VALUE Function
    //------------------------------------------------------
    void TCPgetRegisterVal(WORD addr, WORD *tempData)
    {
    if(addr<LIMIT_SETTINGS_REGISTER)
        {
            switch(addr)
            {
            case 0:
                *tempData = value1;
                break;
            case 1:
                *tempData = value2;
                break;

            default:
                *tempData = 0x0000;
                break;
            }
        }
    }

    please contact me for other info.





     
    Hi do you have files for this implementation? 
    I just started TCPIP stack and Harmony(MHC), but somewhat familiar with the PIC itself.
    #5
    MarPICnus
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/08/16 11:13:00
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2018/11/14 06:20:34 (permalink)
    0
    Hi Skynet78
     
    Is that Modbus code for Harmony or MLA.
    I need something that runs on Harmony demo TCP/IP
     
    Marinus
    #6
    phil_probert
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2015/07/06 10:52:00
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2019/02/06 06:11:18 (permalink)
    0
    Hi Skynet,
    I came across this thread, and your ModbusTCP app could be really useful for me!
    I copied your code and have been trying to interface it with my Harmony tcp/ip code.
    Obviously I've had to create my own variables, buffers, states, defs, etc. for it to compile and port into my code.
    I've come unstuck on the section:
                                ProcessReceivedMessage();
                                TCPreceCount = wMaxGet-6;

                                if(MODBUS_COMMAND.UnitID == localAddr && !Illegal_Function)
                                {
                                    switch (MODBUS_COMMAND.FunctionCode)
                                             "
                                             "
                                             "
    As I'm struggling to second-guess how your variables might be defined, and what 'ProcessReceivedMessage()' actually does, etc etc.
    Otherwise I could see interfacing nicely to my Harmony code, but could do with a hand getting your code to port over.
    Do you have any header files and/or a list of your enums, typedefs, etc?
    It would be nice to get the modbus app working in harmony with Harmony, and post back my findings on here :-)
     
    Cheers,
    Phil
    #7
    _javier_
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2018/08/10 04:58:07
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2019/05/13 02:23:49 (permalink)
    0
    Hi jipihorn,
     
    Did you manage to implement OPC on PIC32?
    I'm trying to do it but I can't find much information.
    Any help on this would be really appreciated.
     
    Thanks in advance.
    #8
    MarPICnus
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/08/16 11:13:00
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2019/05/20 05:26:21 (permalink)
    0
    Hi Phil_Probert
     
    Did you manage the modTCP on Harmony?
    I need some help, I'm on the pIC32MZ with Harmony 2 demo code: tcpip_tcp_server
     
    Regards
    Marinus
     
    #9
    phil_probert
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2015/07/06 10:52:00
    • Location: 0
    • Status: offline
    Re: ModbusTCP implementation using MPLAB Harmony (any version) 2019/05/20 07:15:31 (permalink)
    0
    Hi Marinus,
     
    Yes, I did.
    Is there any section in particular that you're having trouble with?
    I could post some source code, but it will be quite large, as it is from my fully working application.
    It is a mixture of the demo code (which does not work), and some other helpful posts eariler in this thread.
    If your Harmony code already has these functions:
    TCPIP_TCP_ServerOpen()
    TCPIP_TCP_IsConnected()
    TCPIP_TCP_GetIsReady()
    TCPIP_TCP_ArrayGet()
    TCPIP_TCP_ArrayPut()
     
    Then I can reduce the amount of source code I need to post :-)
    -Phil
    #10
    Jump to:
    © 2019 APG vNext Commercial Version 4.5