Problem bei Ausgabe mit fprintf



  • Hallo,

    ich habe folgende HEX-Bytefolge "68 00 00 68" vorliegen.

    Wie muss ich also das fprintf benutzen, damit die serielle Schnittstelle versteht, dass da eine HEX-Bytefolge kommt und nicht eine Zeichenkette.

    Analog dazu, wie benutze ich die fscanf Funktion, um den empfangenen Datenstrom wieder in eine HEX-Bytefolge umzuwandeln.

    Wäre echt dankbar, wenn ihr mir helfen könntet.

    MfG

    MTC

    P.S.: Ich weiss, dass ich das Problem schon unter Rubrik Linux/Unix gepostet habe, aber ich hab dort keine Antwort bekommen und deswegen probier ich es hier nocheinmal !!



  • hi

    würde erstmal ein unsigned char[4] benutzen den ein unsigned char ist ein Byte groß(hoffe ich mal). Darin kannst du an deine Bytes speichern und mit fprintf an deinen Stream( serielle Schnittstelle ) schicken.

    unsigned char bla[4];
    bla[0] = 0x68;
    bla[1] = 0x00;
    bla[2] = 0x00;
    bla[3] = 0x68;
    
    fprintf( FILE *stream, bla);
    

    Zu fscanf kann ich net viel sagen aber habe da was aus der MSDN gefunden.

    /* FSCANF.C: This program writes formatted
     * data to a file. It then uses fscanf to
     * read the various data back from the file.
     */
    
    #include <stdio.h>
    
    FILE *stream;
    
    void main( void )
    {
       long l;
       float fp;
       char s[81];
       char c;
    
       stream = fopen( "fscanf.out", "w+" );
       if( stream == NULL )
          printf( "The file fscanf.out was not opened\n" );
       else
       {
          fprintf( stream, "%s %ld %f%c", "a-string", 
                   65000, 3.14159, 'x' );
    
          /* Set pointer to beginning of file: */
          fseek( stream, 0L, SEEK_SET );
    
          /* Read data back from file: */
          fscanf( stream, "%s", s );
          fscanf( stream, "%ld", &l );
    
          fscanf( stream, "%f", &fp );
          fscanf( stream, "%c", &c );
    
          /* Output data read: */
          printf( "%s\n", s );
          printf( "%ld\n", l );
          printf( "%f\n", fp );
          printf( "%c\n", c );
    
          fclose( stream );
       }
    }
    
    Output
    
    a-string
    65000
    3.141590
    x
    

    Gruß
    de_Bert


Anmelden zum Antworten