Hello,

could anyone please help me with this.
I have a hardware connected to my PC via serial port. This hardware is sending me data (in HEX format). Now i would like this data save to a .txt file. Please help me.

Here is my code so far...if anybody is willing to help, please change the colour of your code :)
P.S.: It is written in Slovene (hope there won't be any problems)

------------------------------------------

#include <windows.h>
#include <stdio.h>
#include "ComPort.h"

char _tmp_buffer[256];

const char* ByteToString(char c) {
    for(int i=0;i<8;i++) {
        if((c>>i)&1) _tmp_buffer[7-i]='1';
        else _tmp_buffer[7-i]='0';
    }
    _tmp_buffer[8]=0;
    return _tmp_buffer;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    AllocConsole();
    printf("Program se zaganja.\n");
    
    
    ComPort Vrata("COM5", 2400, 8, false, 1);
    
    
    char znak;
    int stevec;
    stevec = 0;
    unsigned int a,b;
    unsigned int c1,c2;
    int c=0;
    int kanal;
   

        znak = 0;
        
        while (znak < 0x7F)
        {
        Vrata.GetCharacter(znak);
        }
        stevec++;
        kanal = 1;
        a=znak;
        while (stevec<200)
        {
        Vrata.GetCharacter(znak);
        b=znak;
        stevec++;
        
        
        c = b & 127;
        for(int i=0; i< 5 ; i++)
            {
                if((a >> i) & 1)
                {
                    c = c | (1 << 7+i);    
                } 
            }
            c1 = c / 256;
            c2 = c & 255;     

            printf("Rezultat: A:%x(%s)", a, ByteToString(a)); 
            printf("  -  B:%x(%s)\n", b, ByteToString(b));
            printf("Odkodiranje: C1:%x(%s)", c1, ByteToString(c1));
            printf("  -  C2:%x(%s) Kanal: %d\n", c2, ByteToString(c2), kanal);   

            
        Vrata.GetCharacter(znak);
        a=znak;
        stevec++;
        if (znak<0xC0)
        {
             kanal++;
        }
        else 
             kanal = 1;
       
}

        if(Vrata.GetCharacter(znak)) 
        {

            Vrata.SendCharacter(a);
            Vrata.SendCharacter(b);
            Vrata.SendCharacter(c1);            
            Vrata.SendCharacter(c2);
        }


    printf("Program se koncuje.\n");
    system("pause");
    FreeConsole();
    return 0;
}

>> Now i would like this data save to a .txt file.
Just use normal file i/o. If you wrote the code you posted then that should not be a problem for you. On the otherhand if you didn't write it then see fopen(), fprintf() and fclose() functions.

Hmmm...I was thinking sth in this way

/* fopen example */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)
  {
    fputs ("fopen example",pFile);
    fclose (pFile);
  }
  return 0;
}

Problem is...there is no fprintf() :S?
And I don't know where to insert it :S

{
FILE * pFile;
pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fputs ("fopen example",pFile);
fclose (pFile);
}
return 0;
}

Problem is...there is no fprintf() :S?
And I don't know where to insert it :S

Look for what fprintf is suppose to do. If you do, you will see that this example uses fputs instead. (Not a very good idea)
Since I'm in a good mood: Here's a link with an example using fprintf.

Niek

You have to interpret the data arriving at com port and then write them back to file, writing directly might give garbage value.

You have to interpret the data arriving at com port and then write them back to file.

Ok, and how do I do that? (I'm a nOOb in programming:icon_cry: )

This article has been dead for over six months. Start a new discussion instead.