C code for Dictionary client

C, C++, and Visual C++
Nds

C code for Dictionary client

Postby Nds » Wed Dec 23, 2009 7:44 am

Hi,
Sorry, I have posted a message under the wrong section (SBC)

I'm trying to write the code for the dictionary client inside of the sbc.

I don't find any example about and i'm trying to understand how to write it.
I have some trouble with the CPhidgetDictionary_set_OnKeyChange_Handler function.

i write this CPhidgetDictionary_set_OnKeyChange_Handler((CPhidgetDictionaryHandle)dict,(CPhidgetDictionaryListenerHandle)&key, ".*", (CPhidgetDictionary_OnKeyChange_Function)KeyChanged, NULL);

but from the compiler i get this error:
fw_dict.c:215: warning: passing argument 2 of 'CPhidgetDictionary_set_OnKeyChange_Handler' from incompatible pointer type

this is from the documentation:

int CPhidgetDictionary_set_OnKeyChange_Handler ( CPhidgetDictionaryHandle dict,
CPhidgetDictionaryListenerHandle * dictlistener,
const char * pattern,
CPhidgetDictionary_OnKeyChange_Function fptr,
void * userPtr
)

This is the part of code where i declare the variables:

CPhidgetDictionaryHandle dict;
CPhidgetDictionaryListenerHandle key;

there the handler for the dictionary:
CPhidgetDictionary_set_OnServerConnect_Handler ((CPhidgetDictionaryHandle)dict, DictionaryServerConnected, NULL);
CPhidgetDictionary_set_OnServerDisconnect_Handler ((CPhidgetDictionaryHandle)dict, DictionaryServerDisconnected, NULL);
CPhidgetDictionary_set_OnError_Handler ((CPhidgetDictionaryHandle)dict, DictionaryError, NULL);

and no error are produced from these handler in the gcc.

At the moment the function are empty (inside simple a return 0).


any suggestion?

regards
Nds

Nds

Re: C code for Dictionary client

Postby Nds » Wed Dec 23, 2009 7:53 am

Done,
I realize that i can set the variable as NULL,
At the moment i don't wont remove the handler and don't need to set this variable.

Nds

Nds

Re: C code for Dictionary client

Postby Nds » Wed Dec 23, 2009 9:00 am

No problem in compilation, but, when i run the program after the

Code: Select all

CPhidgetDictionary_openRemoteIP((CPhidgetDictionaryHandle)dict, "192.168.1.2",5001,NULL);

The program exit without error (probably i need to improve the debug)

gcc don't return any error or warning on compilation.

Sorry for the ugly code but is a test, and is a lot of time that i don't code in c

The row of code about the dictionary are:

Code: Select all

CPhidgetDictionaryHandle         dict;
CPhidgetDictionaryListenerHandle          key;

int DictionaryError(CPhidgetDictionaryHandle dict, void *userPtr, int errorCode, const char *errorString) {
   printf("ERRORE %d : %s",errorCode,errorString);   
   return 0;
}
int DictionaryServerConnected(CPhidgetDictionaryHandle dict, void *userptr) {
   int port;
   const char *address;
   CPhidgetDictionary_getServerAddress(dict,&address,&port);
    printf("Server connesso %s : %s",address,port);
   return 0;
}
int DictionaryServerDisconnected(CPhidgetDictionaryHandle dict, void *userptr) {
    printf("Server disconnesso");
   return 0;
}
int KeyChanged(CPhidgetDictionaryHandle dict, void *userPtr, const char *key, const char *value,CPhidgetDictionary_keyChangeReason reason) {
   if      (PHIDGET_DICTIONARY_VALUE_CHANGED   == reason) {printf("KEY CHANGED    %s : %s",value,key);}
    else if (PHIDGET_DICTIONARY_ENTRY_ADDED      == reason) {printf("KEY ADDED      %s : %s",value,key);}
   else if (PHIDGET_DICTIONARY_ENTRY_REMOVING   == reason) {printf("KEY REMOVING   %s : %s",value,key);}
   else if (PHIDGET_DICTIONARY_CURRENT_VALUE   == reason) {printf("KEY CURRENT      %s : %s",value,key);}
   return 0;
}

CPhidgetDictionary_create(&dict);

//Dictionary
   CPhidgetDictionary_set_OnServerConnect_Handler      ((CPhidgetDictionaryHandle)dict, DictionaryServerConnected,      NULL);
   CPhidgetDictionary_set_OnServerDisconnect_Handler   ((CPhidgetDictionaryHandle)dict, DictionaryServerDisconnected,   NULL);
   CPhidgetDictionary_set_OnError_Handler            ((CPhidgetDictionaryHandle)dict, DictionaryError,            NULL);
//Key
   CPhidgetDictionary_set_OnKeyChange_Handler((CPhidgetDictionaryHandle)dict,NULL,".*",(CPhidgetDictionary_OnKeyChange_Function)KeyChanged,NULL);

sleep(1)
CPhidgetDictionary_openRemoteIP((CPhidgetDictionaryHandle)dict, "192.168.1.2",5001,NULL);



Anyone can help me to understand why appen this behavior?

Regards
Nds

User avatar
Patrick
Lead Developer
Posts: 3091
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: C code for Dictionary client

Postby Patrick » Wed Dec 23, 2009 10:18 am

Hi,

CPhidgetDictionary_set_OnKeyChange_Handler needs to be called after a connection is made to the server - I usually call it in the ServerConnected event.

The CPhidgetDictionaryListenerHandle is not optional - you're probably getting the compiler error because you're trying to cast as (CPhidgetDictionaryListenerHandle), where the function requires a (CPhidgetDictionaryListenerHandle *) - &key is already the right data type, there is no need to cast it.

-Patrick

Nds

Re: C code for Dictionary client

Postby Nds » Wed Dec 23, 2009 10:53 am

Thanks Patrick,

I made the change you suggest me in this funciton

Code: Select all

int DictionaryServerConnected(CPhidgetDictionaryHandle dict, void *userptr) {
   int port;
   const char *address;
   CPhidgetDictionary_getServerAddress(dict,&address,&port);
    CPhidgetDictionary_set_OnKeyChange_Handler((CPhidgetDictionaryHandle)dict,&key,".*",(CPhidgetDictionary_OnKeyChange_Function)KeyChanged,NULL);
    printf("Server connesso %s : %s",address,port);
   return 0;
}


No error from gcc, but the program exit after this sleep:

Code: Select all

    sleep(4);
    printf("Connecting);
   CPhidgetDictionary_openRemoteIP((CPhidgetDictionaryHandle)dict, "192.168.1.2",5001,NULL);
    printf("Connected??");



Without print to stdout anything

Nds

Nds

Re: C code for Dictionary client

Postby Nds » Wed Dec 23, 2009 12:22 pm

I have commented some row and the program don't terminate silenty

Code: Select all

int DictionaryServerConnected(CPhidgetDictionaryHandle dict, void *userptr) {
CPhidgetDictionary_set_OnKeyChange_Handler((CPhidgetDictionaryHandle)dict,&key,".*",(CPhidgetDictionary_OnKeyChange_Function)KeyChanged,NULL);
   return 0;
}


Now i have tried to add a key and has worked, but i think my code is really dirty

there are any tutorial or documentation on how is the best practice for make application inside the sbc?

At the moment for keep the program running i make a recursive function that exit and close the phidgets when an input change state, but i have big dubt about the quality of my code.

regards
Nds

User avatar
Patrick
Lead Developer
Posts: 3091
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: C code for Dictionary client

Postby Patrick » Wed Dec 23, 2009 12:33 pm

I will add a C example for the dictionary at some point - but this will have to wait until the new year.

-Patrick

AdamS

Re: C code for Dictionary client

Postby AdamS » Wed Dec 23, 2009 4:12 pm

Had some spare time so decided to write one up that follows more or less the same structure as our dictionary example for python. Should show general process of working with the dictionary. It should be included in the standard C code samples form now on.

Code is below:

Code: Select all

#include <stdio.h>
#include <phidget21.h>

CPhidgetDictionaryHandle dict = 0;
CPhidgetDictionaryListenerHandle keyListener = 0;
const char *keyPattern = ".*";


int KeyChangedHandler(CPhidgetDictionaryHandle dict, void *userPtr, const char *key, const char *value, CPhidgetDictionary_keyChangeReason reason)
{
   const char *reasonString;

   if(reason == PHIDGET_DICTIONARY_VALUE_CHANGED)
   {
      reasonString = "Value Changed";
   }
   else if(reason == PHIDGET_DICTIONARY_ENTRY_ADDED)
   {
      reasonString = "Entry Added";
   }
   else if(reason == PHIDGET_DICTIONARY_ENTRY_REMOVING)
   {
      reasonString = "Entry Removed";
   }
   else if(reason == PHIDGET_DICTIONARY_CURRENT_VALUE)
   {
      reasonString = "Current Value";
   }

   printf("%s>> Key: %s -- Value: %s\n", reasonString, key, value);

   return 0;
}
int DictionaryErrorHandler(CPhidgetDictionaryHandle dict, void *userPtr, int errorCode, const char *errorString)
{
   printf("Dictionary Error>> %i: %s\n", errorCode, errorString);
   return 0;
}
int ServerConnectedHandler(CPhidgetHandle phid, void *userPtr)
{
   printf("Server Connection Established.\n");
   CPhidgetDictionary_set_OnKeyChange_Handler(dict, &keyListener, keyPattern, KeyChangedHandler, NULL);
   return 0;
}
int ServerDisconnectedHandler(CPhidgetHandle phid, void *userPtr)
{
   printf("Server Disconnected!\n");
   CPhidgetDictionary_remove_OnKeyChange_Handler(keyListener);
   keyListener = 0;
   return 0;
}
int dictionary_simple()
{
   int result, serverStatus, port;
   const char *address;

   result = CPhidgetDictionary_create(&dict);

   result = CPhidget_set_OnServerConnect_Handler((CPhidgetHandle)dict, ServerConnectedHandler, NULL);
   result = CPhidget_set_OnServerDisconnect_Handler((CPhidgetHandle)dict, ServerDisconnectedHandler, NULL);
   result = CPhidgetDictionary_set_OnError_Handler(dict, DictionaryErrorHandler, NULL);

   result = CPhidgetDictionary_openRemoteIP(dict, "adam.local", 5001, NULL);

   do
   {
      result = CPhidgetDictionary_getServerStatus(dict, &serverStatus);
   }while(serverStatus = PHIDGET_NOTATTACHED);


   printf("Attached to server\n");

   CPhidgetDictionary_getServerAddress(dict, &address, &port);

   printf("Address: %s -- Port: %i \n", address, port);

   //Set some keys

   printf("Add some keys....\n");

   sleep(1);

   CPhidgetDictionary_addKey(dict, "TEST1", "1234", 1);

   CPhidgetDictionary_addKey(dict, "TEST2", "5678", 1);

   CPhidgetDictionary_addKey(dict, "TEST3", "9012", 0);

   CPhidgetDictionary_addKey(dict, "TEST4", "3456", 1);

   sleep(2);

   printf("Delete the 4th key....\n");

   sleep(1);

   CPhidgetDictionary_removeKey(dict, "TEST4");

   sleep(2);

   printf("Press any key to end\n");

   getchar();

   result = CPhidgetDictionary_close(dict);
   result = CPhidgetDictionary_delete(dict);
   return 0;
}

int main(int argc, char* argv[])
{
   dictionary_simple();
   return 0;
}

Nds

Re: C code for Dictionary client

Postby Nds » Wed Dec 23, 2009 6:55 pm

Thanks Patrick and Adams for the support.

Have a nice holiday and an happy new year

Best regards
Nds


Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 1 guest