•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,233 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,767 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1348 | Replies: 5
![]() |
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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)
------------------------------------------
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)
------------------------------------------
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon : Nov 6th, 2007 at 8:19 am. Reason: add code tags
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
>> 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.
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.
Last edited by Ancient Dragon : Nov 6th, 2007 at 8:26 am.
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Hmmm...I was thinking sth in this way
Problem is...there is no fprintf()
?
And I don't know where to insert it
/* 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;
}
?And I don't know where to insert it
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,825
Reputation:
Rep Power: 11
Solved Threads: 191
•
•
•
•
Problem is...there is no fprintf(){ FILE * pFile; pFile = fopen ("myfile.txt","w"); if (pFile!=NULL) { fputs ("fopen example",pFile); fclose (pFile); } return 0; }?
And I don't know where to insert it
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
Last edited by niek_e : Nov 7th, 2007 at 4:25 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
do NOT pm me for help, in the best case, you'll get ignored
•
•
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,012
Reputation:
Rep Power: 6
Solved Threads: 68
You have to interpret the data arriving at com port and then write them back to file, writing directly might give garbage value.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Installing Windows 98 On VMware. Floppy problem (Windows 9x / Me)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP / 2003)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Gadgets and Gizmos)
- Connection Problems (Networking Hardware Configuration)
- Encoding (Unicode) problem in IE 6.0 (Web Browsers)
- .htaccess mod_rewrite problem (Linux Servers and Apache)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
- Problem with Windows Update and WinXP (Web Browsers)
Other Threads in the C Forum
- Previous Thread: Converting Any Base to Any Base
- Next Thread: A problem when I employ GNU Scientific Libraries in a Code



Linear Mode