Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: How to get Item Index no. #47402

    kimhyunki
    Member

    Dear Expert.

    I could solve by modifing header, and cpp file as following to get the item index.

    Thank you and Best regards.

    Header:
    ———————————-
    struct OPCItemType {
    char *itemID;
    int itemidx; //add item index
    opcAccessRights opcAccessRight;
    opctypes itemType;
    unsigned int ptr_callback;
    };

    ——————————-

    cpp

    void OPC::addItem(const char *itemID, opcAccessRights opcAccessRight, opctypes opctype, bool (*function)(const char *itemID,const int itemidx, const opcOperation opcOP, const bool value)) //add itemidx element in the call back routine

    void OPC::internaladdItem(const char *itemID, opcAccessRights opcAccessRight, opctypes opctype, int callback_function)
    {
    //extern int __heap_start, *__brkval;
    //int v;
    //Serial.print(F(“Free memory:”));
    //Serial.println( (int) &v – (__brkval == 0 ? (int) &__heap_start : (int) __brkval));

    OPCItemList = (OPCItemType *) realloc(OPCItemList, (OPCItemsCount + 1) * sizeof(OPCItemType));
    if (OPCItemList != NULL) {
    OPCItemList[OPCItemsCount].itemType = opctype;

    OPCItemList[OPCItemsCount].itemID = (char *) malloc(strlen(itemID)+1);
    OPCItemList[OPCItemsCount].itemidx=OPCItemsCount; // when opc item is added , transfer counter into itemidx element.
    strncpy(&OPCItemList[OPCItemsCount].itemID[0], itemID, strlen(itemID)+1);

    OPCItemList[OPCItemsCount].opcAccessRight = opcAccessRight;

    OPCItemList[OPCItemsCount].ptr_callback = callback_function;
    OPCItemsCount++;
    } else {
    Serial.println(F(“Not enough memory”));
    }

    void OPCEthernet::processClientCommand()
    {
    char *p,*j;
    bool matched = false;
    bool (*bool_callback)(const char *itemID,const int itemidx, const opcOperation opcOP, const bool value);//add item index
    byte (*byte_callback)(const char *itemID,const int itemidx, const opcOperation opcOP, const byte value); //add item index
    int (*int_callback)(const char *itemID,const int itemidx, const opcOperation opcOP, const int value); //add item index
    float (*float_callback)(const char *itemID,const int itemidx, const opcOperation opcOP, const float value); //add item index

    client.println(F(“HTTP/1.1 200 OK\r\nContent-Type: text/json\r\nConnection: close\r\n”));

    if (!strcmp(buffer, “itemsmap”)) {
    sendOPCItemsMap();
    }
    else
    {
    p = strtok_r(buffer,”=”,&j);
    if (!j[0]) {
    for (int i = 0; i < OPCItemsCount; i++) { if (!strcmp(buffer, OPCItemList[i].itemID)) { // Execute the stored handler function for the command client.print(F("[{\"ItemId\":\"")); client.print(buffer); client.print(F("\",\"ItemValue\":\"")); switch (OPCItemList[i].itemType) { case opc_bool : bool_callback = (bool (*)(const char *itemID,const int itemidx, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback);//add item index client.print(bool_callback(OPCItemList[i].itemID,OPCItemList[i].itemidx,opc_opread,NULL)); //add item index break; case opc_byte : byte_callback = (byte (*)(const char *itemID,const int itemidx, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback);//add item index client.print(byte_callback(OPCItemList[i].itemID,OPCItemList[i].itemidx,opc_opread,NULL));//add item index break; case opc_int : int_callback = (int (*)(const char *itemID, const int itemidx,const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback); //add item index client.print(int_callback(OPCItemList[i].itemID,OPCItemList[i].itemidx,opc_opread,NULL)); //add item index break; case opc_float : float_callback = (float (*)(const char *itemID,const int itemidx, const opcOperation opcOP, const float value))(OPCItemList[i].ptr_callback); //변경 client.print(float_callback(OPCItemList[i].itemID,OPCItemList[i].itemidx,opc_opread,NULL)); //add item index break; } /* end switch */ client.print(F("\"}]")); matched = true; break; } /* end if */ } /* end for */ } /* end if */ else { for (int i = 0; i < OPCItemsCount; i++) { if (!strcmp(buffer, OPCItemList[i].itemID)) { // Call the stored handler function for the command switch (OPCItemList[i].itemType) { case opc_bool : bool_callback = (bool (*)(const char *itemID,const int itemidx, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback);//add item index bool_callback(OPCItemList[i].itemID,OPCItemList[i].itemidx,opc_opwrite,atoi(j));//add item index break; case opc_byte : byte_callback = (byte (*)(const char *itemID,const int itemidx, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback);//add item index byte_callback(OPCItemList[i].itemID,OPCItemList[i].itemidx,opc_opwrite,atoi(j));//add item index break; case opc_int : int_callback = (int (*)(const char *itemID,const int itemidx,const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback); //add item index int_callback(OPCItemList[i].itemID,OPCItemList[i].itemidx,opc_opwrite,atoi(j)); //add item index break; case opc_float : float_callback = (float (*)(const char *itemID,const int itemidx, const opcOperation opcOP, const float))(OPCItemList[i].ptr_callback);//add item index float_callback(OPCItemList[i].itemID,OPCItemList[i].itemidx,opc_opwrite,atof(j)); //add item index break; } /* end case */ matched = true; break; } /* end if */ } /* end for */ } /* end else */ } /* end else */ } --------------------------------

Viewing 1 post (of 1 total)