Jump to content
View in the app

A better way to browse. Learn more.

Tinkerunity

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
[[Template core/global/global/poll is throwing an error. This theme may be out of date. Run the support tool in the AdminCP to restore the default theme.]]

Featured Replies

Geschrieben

Hallo,

 

habe ein Projekt bezüglich Optischer Kommunikation. Mein Problem ist, das auf der Senderseite (IO4 Bricklet) die Übertragung nicht funktioniert. Das ganze ist und muss in C geschrieben sein!

Der Compiler erkennt nicht die Funktion um den Port 0 auf High/Low zusetzen. Kann mir jemand da weiter helfen?

Der C-Code an sich müsste passen. Wenn ich anstatt der µC Befehle einfach eine Textausgabe, z.B. "Licht an" machen funktioniert es.

 

Ich hoffe jemand kann mir weiter helfen...

 

Gruß Alex

 

Hier der Code:

 

#include <stdio.h>

#include <time.h>              // Wichtig für Clocks per Sec und Sleep!!!!

#include <unistd.h>

 

#include "ip_connection.h"

#include "bricklet_io4.h"

 

#define HOST "localhost"

#define PORT 4223

#define UID "9VU"              // Change to your UID

 

sendByte (char c){

       

       

        io4_set_configuration(&io, 1<<0, 'o', true);

        usleep(600000);

       

        int i = 0;

                         

        for (i=0; i<8; i++) {

     

                    if (c & (i<<1)) {                         

                    io4_set_configuration(&io, 1<<0, 'o', true);

                    usleep(300000);

                    }

                               

                    else {                                   

                    io4_set_configuration(&io, 1<<0, 'o', false);

                    usleep(300000);                                                             

                    }

        }

       

        io4_set_configuration(&io, 1<<0, 'o', false);

        usleep(600000);

       

 

 

void send(char *c)

{

        while (*c != 0) {     

                sendByte(*c);

                c++;

        }

}

 

                 

int main() {

 

    // Don't use device before ipcon is connected

 

  // Hauptprogramm

 

      // Create IP connection

    IPConnection ipcon;

    ipcon_create(&ipcon);

 

    // Create device object

    IO4 io;

    io4_create(&io, UID, &ipcon);

 

    //Connect to brickd

    if(ipcon_connect(&ipcon, HOST, PORT) < 0) {

        fprintf(stderr, "Could not connect\n");

        exit(1);

    }

           

  send("B");

     

    printf("Press key to exit\n");

    getchar();

    ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally

}

 

 

Geschrieben

Fehlt da nicht der Brick an dem das IO4 angeschlossen ist?

Geschrieben

Fehlt da nicht der Brick an dem das IO4 angeschlossen ist?

Man muss den Brick nicht mit angeben oder einbinden. Nur wenn man Funktionen von diesem verwenden möchte.

Geschrieben
  • Autor

Ich benutze MAC OS, als Programm Netbeans und C Compiler ist gcc

 

Der Compiler kennt bei:

 

io4_set_configuration(&io, 1<<0, 'o', true);

 

das &io nicht.

 

Was mache ich verkehrt? Wenn io4_set_configuration(&io, 1<<0, 'o', true); in der main() steht, dann wird port0 auf Output gestellt und die LED leuchtet. Für meine Funktion kann ich es aber nicht in die main() schreiben.

Der Beispielcode für das IO/4 von der TF Site funktioniert auch problemlos

 

Als Fehler kommt folgendes:

 

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf

"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/led

mkdir -p build/Debug/GNU-MacOSX

rm -f build/Debug/GNU-MacOSX/main.o.d

gcc    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.c

main.c: In function 'sendByte':

main.c:15: error: 'io' undeclared (first use in this function)

main.c:15: error: (Each undeclared identifier is reported only once

main.c:15: error: for each function it appears in.)

make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1

make[1]: *** [.build-conf] Error 2

make: *** [.build-impl] Error 2

 

 

BUILD FEHLGESCHLAGEN. (Ausstiegswert 2,, Zeit total: 254ms)

 

Geschrieben

Naja, der Compiler sagt ja schon was Sache ist:

main.c:15: error: 'io' undeclared (first use in this function)

 

io Ist nicht deklariert in sendByte. Entweder du machst das global oder du übergibst es in der Funktion ;).

Geschrieben
  • Autor

Wenn ich es global mache oder der Funktion übergebe funktioniert das Programm auch nicht.

dann kommt:

 

RUN FINISHED; Segmentation fault: 11, real time: 160ms; user: 0ms; system 0ms

 

Habe es so gemacht:

 

sendByte (char c){

       

    IO4 io;

       

        io4_set_configuration(&io, 1<<0, 'o', true);

        usleep(600000);

       

        int i = 0;

                         

        for (i=0; i<8; i++) {

     

                    if (c & (i<<1)) {                         

                    io4_set_configuration(&io, 1<<0, 'o', true);

                    usleep(300000);

                    }

                               

                    else {                                   

                    io4_set_configuration(&io, 1<<0, 'o', false);

                    usleep(300000);                                                             

                    }

        }

       

        io4_set_configuration(&io, 1<<0, 'o', false);

        usleep(600000);

       

 

 

Der C-Code an sich müsste korrekt sein. Denn wenn ich einfach ein printf befehl mache für "Lampe an/aus" anstatt der io4_set_configuration läuft das Programm durch und gibt mit den Buchstaben bitweise aus

 

Geschrieben

Wenn ich es global mache oder der Funktion übergebe funktioniert das Programm auch nicht.

dann kommt:

 

RUN FINISHED; Segmentation fault: 11, real time: 160ms; user: 0ms; system 0ms

 

So wie du es jetzt gemacht hast ist das "io" weder global noch initialisiert. Das kann nicht gehen.

 

Nimm mal dein Original Programm und verschiebe das "IO4 io;" aus der main nach oben zu den Defines.

Geschrieben
  • Autor

Bekomme den gleichen Fehler, das hatte ich ja schon alles versucht. Deshalb verstehe ich nicht was ich falsch mache....

 

....

#include <stdio.h>

#include <time.h>              // Wichtig für Clocks per Sec und Sleep!!!!

#include <unistd.h>

#include <pthread.h>   

 

#include "ip_connection.h"

#include "bricklet_io4.h"

 

#define HOST "localhost"

#define PORT 4223

#define UID "9VU"              // Change to your UID

 

IPConnection ipcon;

IO4 io;

 

sendByte (char c){ 

 

-----------

Habe die main.c mal als Anhang dazu gemacht

main.c

Geschrieben

Dein angehängtes Programm funktioniert prinzipiell, du bist da aber in eine Falle mit dem Linker geraten.

 

Das Problem ist, dass du eine Funktion namens send hast und es schon in der libc eine Funktion namens send gibt die die IPConnection inter verwendet. Der Linker nimmt jetzt aber deine send Funktion anstatt der aus der libc (weil die gleich heißen). Der Segfault kommt dann daher dass die falsche send Funktion aufgerufen.

 

Die Lösung ist, deine send Funktion in sendString umzubenennen. Zusätzlich machst du am besten alle deine Funktionen (außer main) static, dann sind sie nur innerhalb der main.c sichtbar.

 

Also statt

 

sendByte (char c) { ... }

void send(char *c) { ... } 

 

dann

 

static void sendByte (char c) { ... }

static void sendString(char *c) { ... } 

 

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...

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.