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;
}