-
AuthorPosts
-
07/03/2016 at 1:34 pm #46803
kimhyunki
MemberHello I am a beginer of Ardunio Forum
When call back function is exicuted, I want to poll the index no.
For example when I create three items I want transfer value from buff[1] to firstitem, buff[2] to seconditem,buff[3] to thirditem.
.int callback(const char *itemID, const opcOperation opcOP, const int value){
return buff[index] // how to get index no(the order sorted by oldest created item),;
}OPCEthernet.addItem(“fistitem”,opc_read, opc_int, callback); //index 1
OPCEthernet.addItem(“seconditem”,opc_read, opc_int, callback); //index 2
OPCEthernet.addItem(“thirditem”,opc_read, opc_int, callback); //index 311/03/2016 at 11:02 am #47400i.martinez
KeymasterI think the best solution is using a “if” statement, compare if itemID is firstitem and return buff[0] and go on..
A elegant alternative is to create a enumeration and an array strings and compare…or a variable for each item..
In programming there are a multiples solutions for the same problem…but which is the best? ….
All the best
ilde17/03/2016 at 7:32 am #47402kimhyunki
MemberDear 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 indexclient.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 */ } --------------------------------28/03/2016 at 10:58 am #47410i.martinez
KeymasterHello
yes! great! You got it!
I think is a good solution. I have to think if release a new version with this feature or let the library as simple as posible.
All the best
ilde -
AuthorPosts
- You must be logged in to reply to this topic.