Jump to content

C# automatically add bricklets


CD108

Recommended Posts

Guys,

 

I'm looking for a way to be able to add bricklets without coding for each one individually. For example i want to add a database row in a table that allows me to put in the bricklet info and have the value updated in that row. Currently i have to add a callback in the code for each sensor which is a pain when i want to add more sensors.

 

Is there an easy way to do this so that the sensor network can be expanded without changing the code in the application?

Link zu diesem Kommentar
Share on other sites

Use the enumeration callback of the IPConnection.

 

2.0 example:

    public void main(...)
    {
        //...
        ipcon.Enumerate += EnumerateCB;
    }

    static void EnumerateCB(IPConnection sender,
                            string uid, string connectedUid, char position,
                            short[] hardwareVersion, short[] firmwareVersion,
                            int deviceIdentifier, short enumerationType)
    {
        if(enumerationType == IPConnection.ENUMERATION_TYPE_DISCONNECTED)
        {
           return; //we don't care for disconnected devices
        }
        if(deviceIdentifier == 216)  //Temperature-Bricklet, as of http://www.tinkerforge.com/doc/Software/Device_Identifier.html#device-identifier
        {
           var bricklet = new BrickletTemperature(uid, sender);
           bricklet.SetTemperatureCallbackPeriod(100);
           bricklet.Temperature += TemperatureCB;
        }
    }

    private static void TemperatureReached(BrickletTemperature sender, short temperature)
    {
        //update your database with sender.uid and temperature
    }

 

This example contains mostly code from my mind, so there might be typos.

Also: I saw that there is no Accessor for the UID on the Device-class. So my comment about "use sender.UID" is wrong, but I think this needs to be changed :D (Pull Request in preparation ^^)

Link zu diesem Kommentar
Share on other sites

OK, i have got this to work using

static void Temp1CB(BrickletTemperature sender, short temperature)
        {
            string uid;
            string connecteduid;
            char position;
            byte[] hardwareversion;
            byte[] firmwareversion;
            int deviceidentifier;
            sender.GetIdentity(out uid,out connecteduid,out position,out hardwareversion,out firmwareversion,out deviceidentifier);
            
         //code to update database with uid and temperature
        }

 

Is there simpler way to get the uid without all the other information? As you pointed out there doesn't seem to be a sender.uid

 

Thanks for the quick help!

Link zu diesem Kommentar
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...