Jump to content

UID auslesen in C++


Adi

Recommended Posts

Hallo

 

Ich bin relativ neu in der TF Welt und bastle gerade mit dem RED, Master Brick und einigen Bricklets.

 

Im Moment versuche ich beim start eines Programms auf dem RED die Hardware einzulesen bzw. die UID. Auf der TF Webseite bin ich auf die Funktion GetIdentity bzw. Enumerate Callbacks gestossen. Gerade letzteres sagt mir wenig bis nichts, im Forum habe ich etwas gefunden, jedoch nichts zur Sprache C++.

 

Ich möchte nun die UIDs der Hardware einlesen und mithilfe von den Device Identifier zuordnen. Damit soll eine dynamische Verwendung von verschiedenen Bricklet des selben Typs möglich werden.

 

Die Funktion io16_get_identity klappt allerdings nur, wenn das Bricklet bereits mit der UID erzeugt wurde und nicht vorher, um die UID zu erfahren.

 

Von dem her gehe ich davon aus, dass ich ein Callback verwenden muss...Kann ich damit alle Bricklets finden?

 

Kann mir da jemand helfen und evtl. ein Beispiel dazu geben?

 

Danke & Liebe Grüsse

 

 

Link to comment
Share on other sites

Hier mal meinen Code wie ich es benutze.

Schreiben tue ich mit QT. Dann kann man das Programm sowohl auf Windows als auch auf Linux Rechnern laufen lassen.

 

Ich hoffe es hilft dir etwas weiter.

Mein Programm geht dynamisch mit den Komponenten um.

Ich habe eine Dynamische Liste in der alle gespeichert werden.

Ausserdem ordne ich diese dann der Reihenfolge im Stapel nach.

Es ist auch egal wo sie angesteckt werden.

Zum Beispiel:

Der erste gefundene Quad Relais bekommt bei mir die ID 0 - 3.

Egal an welchem Master er angesteckt wird.

Wenn in der Reihenfolge kein anderer Quad davor kommt, bekommt er die Nummern 0-3. Der nächste dann 4-7 u.s.w.

In meiner Liste ist jede Komponente mit Typ und UID hinterlegt.

Darüber kann ich sie dann finden und ansprechen. Bei Callback zum Beispiel.

 

void Create_REDBrick(const char* device_uid, uint8_t device_identifier, const char *connected_uid,char position, bool is_new)
{
    qDebug() << "Create_REDBrick " << device_uid << " connected_uid " << connected_uid << " position " << position;
    /****************************************************************************/
    /*						Create_REDBrick Bricklet einbinden						*/
    /****************************************************************************/
    Brick* NewBrick = NULL;
    QString DEVICE_UID = QString(device_uid);
    QString CONNECTED_UID   = QString(connected_uid);
    UID = strdup(device_uid);
    if(UID)
    {
//            _strset( UID, 0 );
//            strncpy_s(UID,strlen(device_uid),  device_uid, strlen(device_uid)-1);
        /****************************************************************************/
        /*                             RED Brick erstellen      					*/
        /****************************************************************************/
        if(Mainprogramm)
        {
            if(!Mainprogramm->Check_Brick(DEVICE_UID))
            {
                NewBrick = new Brick("Redbrick",0,0,DEVICE_UID,CONNECTED_UID,(QString)position);
                NewBrick->Redbrick = new RED();
                red_create(NewBrick->Redbrick, device_uid,ipcon);
                Mainprogramm->Bricklist.append(NewBrick);
            }
            else
            {
                NewBrick = Mainprogramm->Get_Brick(DEVICE_UID);
            }
            Mainprogramm->Set_enumeratebit();
        }
    }
}

void Create_Master(const char* device_uid, uint8_t device_identifier, const char *connected_uid,char position, bool is_new)
{
    qDebug() << "Create_Master " << device_uid << " connected_uid " << connected_uid << " position " << position;
    /****************************************************************************/
    /*							Master Brick einbinden							*/
    /****************************************************************************/
    Brick*  NewBrick    = NULL;
    QString DEVICE_UID  = QString(device_uid);
    QString CONNECTED_UID   = QString(connected_uid);
    UID = strdup(device_uid);
    if(UID)
    {
        /****************************************************************************/
        /*							Master Brick erstellen							*/
        /****************************************************************************/
        if(Mainprogramm)
        {
            if(!Mainprogramm->Check_Brick(DEVICE_UID))
            {
                NewBrick = new Brick("Master",0,0,DEVICE_UID,CONNECTED_UID,(QString)position);
                NewBrick->master = new Master();
                master_create(NewBrick->master, device_uid,ipcon);
                Mainprogramm->Bricklist.append(NewBrick);
            }
            else
            {
                NewBrick = Mainprogramm->Get_Brick(DEVICE_UID);
            }
            if(NewBrick)
            {
                uint16_t voltage    = 0;
                uint16_t current    = 0;
                uint16_t usbvoltage = 0;
                master_get_stack_voltage(NewBrick->master, &voltage);
                master_get_stack_current(NewBrick->master, &current);
                master_get_usb_voltage(  NewBrick->master, &usbvoltage);
                Mainprogramm->Set_Stackvoltage(voltage/1000);
                Mainprogramm->Set_Stack_current(current/1000);
                /****************************************************************************/
                /*						Master Brick Callback erstellen						*/
                /****************************************************************************/
                master_register_callback(NewBrick->master,MASTER_CALLBACK_STACK_VOLTAGE , (void*)cb_stack_voltage,NewBrick->master);
                master_register_callback(NewBrick->master,MASTER_CALLBACK_USB_VOLTAGE 	, (void*)cb_stack_voltage,NewBrick->master);
                master_register_callback(NewBrick->master,MASTER_CALLBACK_STACK_CURRENT	, (void*)cb_stack_current,NewBrick->master);

            }
            Mainprogramm->Set_enumeratebit();
        }
    }
}

void cb_enumerate(const char *uid, const char *connected_uid, char position, uint8_t hardware_version[3], uint8_t firmware_version[3], uint16_t device_identifier, uint8_t enumeration_type, void *user_data)
{
    qDebug() << QTime::currentTime().toString("hh:mm:ss:zzz   ")  << "cb_enumerate( " << uid << " , " << connected_uid << " , " << position << " , " << hardware_version << " , " << firmware_version << " , " << device_identifier << " , " << enumeration_type << "*user_data)";
    /****************************************************************************/
    /*				Antwort auf	Komponentenabfrage (Bricks,Bricklets) 			*/
    /****************************************************************************/
    int n = 0;
    bool New = false;
    QString DEVICE_UID = QString(uid);
    if(Mainprogramm)
    {
        if(!Mainprogramm->Check_Brick(DEVICE_UID))
        {
            New = true;
        }
    }
    if(enumeration_type == IPCON_ENUMERATION_TYPE_DISCONNECTED)
    {
        Mainprogramm->Remove_Brick(DEVICE_UID);
    }

    if(device_identifier==MASTER_DEVICE_IDENTIFIER)
    {
        if(enumeration_type == IPCON_ENUMERATION_TYPE_AVAILABLE)
        {
            Create_Master(uid,device_identifier, connected_uid, position,true);
            uid_master = uid;
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "Master Brick gefunden";
            printf("Enumeration Type:  %d\n", enumeration_type);
        }
        if(enumeration_type == IPCON_ENUMERATION_TYPE_CONNECTED)
        {
            Create_Master(uid,device_identifier, connected_uid, position,true);
            uid_master = uid;
            printf("Enumeration Type:  %d\n", enumeration_type);
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "Master Brick verbunden";
        }
        if(enumeration_type == IPCON_ENUMERATION_TYPE_DISCONNECTED)
        {
            printf("Enumeration Type:  %d\n", enumeration_type);
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "Master Brick disconnected";
            Brick* OldBrick = Mainprogramm->Get_Brick(DEVICE_UID);
            if(OldBrick)
            {
                Mainprogramm->Remove_Brick(DEVICE_UID);
                master_destroy(OldBrick->master);
                delete OldBrick->master;
                OldBrick->master = NULL;
                Mainprogramm->Remove_Brick(DEVICE_UID);
            }
        }

    if(device_identifier==INDUSTRIAL_QUAD_RELAY_DEVICE_IDENTIFIER)
    {
        if(enumeration_type == IPCON_ENUMERATION_TYPE_AVAILABLE)
        {
            Create_Industrial_Quad_Relay[n](uid,device_identifier, connected_uid, position,true);
            uid_Digital_In = uid;
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "DIGITAL QUAD OUT Brickled gefunden";
            printf("Enumeration Type:  %d\n", enumeration_type);
        }
        if(enumeration_type == IPCON_ENUMERATION_TYPE_CONNECTED)
        {
            Create_Industrial_Quad_Relay(uid,device_identifier, connected_uid, position,true);
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "DIGITAL QUAD OUT Brickled gefunden";
            printf("Enumeration Type:  %d\n", enumeration_type);
        }
        if(enumeration_type == IPCON_ENUMERATION_TYPE_DISCONNECTED)
        {
            printf("Enumeration Type:  %d\n", enumeration_type);
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "DIGITAL QUAD OUT Brickled disconnected";
            Brick* OldBrick = Mainprogramm->Get_Brick(DEVICE_UID);
            if(OldBrick)
            {
                industrial_quad_relay_destroy(OldBrick->Quad_Relay);
                delete OldBrick->Quad_Relay;
                OldBrick->Quad_Relay = NULL;
            }
        }
    }
    }

    if(device_identifier==RED_DEVICE_IDENTIFIER)
    {
        if(enumeration_type == IPCON_ENUMERATION_TYPE_AVAILABLE)
        {
            Create_REDBrick(uid,device_identifier, connected_uid, position,true);
            uid_Redbrick = uid;
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "RED Brick gefunden";
            printf("Enumeration Type:  %d\n", enumeration_type);
        }
        if(enumeration_type == IPCON_ENUMERATION_TYPE_CONNECTED)
        {
            Create_REDBrick(uid,device_identifier, connected_uid, position,true);
            uid_Redbrick = uid;
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "RED Brick gefunden";
            uid_Redbrick = uid;
            printf("Enumeration Type:  %d\n", enumeration_type);
        }
        if(enumeration_type == IPCON_ENUMERATION_TYPE_DISCONNECTED)
        {
            printf("Enumeration Type:  %d\n", enumeration_type);
            qDebug() << "Enumeration Type: " << enumeration_type;
            qDebug() << "RED Brick disconnected";
            Brick* OldBrick = Mainprogramm->Get_Brick(DEVICE_UID);
            if(OldBrick)
            {
                red_destroy(OldBrick->Redbrick);
                delete OldBrick->Redbrick;
                OldBrick->Redbrick = NULL;
                Mainprogramm->Remove_Brick(DEVICE_UID);
            }
        }
    }

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qDebug() << "main(" << argc << " , " << argv << ")";
    QString Puffer;
    int Pos = 0;
    while(Pos < argc)
    {
        Puffer = argv[Pos];
        qDebug() << "Arg " << Pos << " : " << Puffer;
        if(Puffer.contains("IP="))
        {
            TFIP = Puffer.remove("IP=");
        }
        else if(Puffer.contains("PORT="))
        {
            Puffer = Puffer.remove("PORT=");
            PORT = Puffer.toInt();
        }
        Pos++;
    }
    /****************************************************************************/
    /*							Hauptprogramm starten							*/
    /****************************************************************************/
    if(Mainprogramm == NULL)
    {
        qDebug() << "Touchprogram erstellen";
        Mainprogramm = new Touchprogram();
        Mainprogramm->Read_Ini();
        Mainprogramm->Run();
        Mainprogramm->window->update();
        if(Mainprogramm->LOCALHOST)
        {
            TFIP = "localhost";
        }
        else
        {
            TFIP = Mainprogramm->TFIP;
        }
        PORT = Mainprogramm->PORT;
    }

    /****************************************************************************/
    /*							IPConnection erstellen							*/
    /****************************************************************************/

    HOST = (char *)malloc((TFIP.length() + 1) * sizeof(char));
    strcpy(HOST, TFIP.toStdString().c_str());
    qDebug() << "Create IPConnection";
    if(!ipcon)
    {
       ipcon = new IPConnection();
    }
    if(ipcon)
    {
        qDebug() << "Create IPConnection success";
        ipcon_create(ipcon);
        qDebug() << "connect to " << HOST << " Port: " << PORT;
        if(ipcon_connect(ipcon, HOST, PORT) < 0)
        {
            qDebug() << "Could not create connection";
            fprintf(stderr, "Could not create connection\n");
            ipcon_set_auto_reconnect(ipcon,true);
        }
    }
    else
    {
        qDebug() << "Could not create  IPConnection";
    }
    /****************************************************************************/
    /*					Komponenten (Bricks,Bricklets) abfragen					*/
    /****************************************************************************/
    ipcon_register_callback(ipcon,   IPCON_CALLBACK_ENUMERATE,(void *)cb_enumerate   ,NULL);
    ipcon_enumerate(ipcon);

    return a.exec();
}


Link to comment
Share on other sites

Vielen Dank für die schnelle Antwort.

 

Ich habe mir das wohl zu einfach vorgestellt...ich dachte die UIDs der Bricklets und eventuell noch den Steckplatz auszulesen wäre einfacher  :P

Da ich nur einen Master habe, würden diese Infos vollkommen ausreichen.

 

Am Wochenende versuche ich mal Deinen Code etwas genauer zu verstehen..

 

Gruss

Link to comment
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...