User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 5
Reputation: Pačo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Pačo Pačo is offline Offline
Newbie Poster

COMport C++ problem

  #1  
Nov 6th, 2007
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)

------------------------------------------
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include "ComPort.h"
  4.  
  5. char _tmp_buffer[256];
  6.  
  7. const char* ByteToString(char c) {
  8. for(int i=0;i<8;i++) {
  9. if((c>>i)&1) _tmp_buffer[7-i]='1';
  10. else _tmp_buffer[7-i]='0';
  11. }
  12. _tmp_buffer[8]=0;
  13. return _tmp_buffer;
  14. }
  15.  
  16. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
  17. AllocConsole();
  18. printf("Program se zaganja.\n");
  19.  
  20.  
  21. ComPort Vrata("COM5", 2400, 8, false, 1);
  22.  
  23.  
  24. char znak;
  25. int stevec;
  26. stevec = 0;
  27. unsigned int a,b;
  28. unsigned int c1,c2;
  29. int c=0;
  30. int kanal;
  31.  
  32.  
  33. znak = 0;
  34.  
  35. while (znak < 0x7F)
  36. {
  37. Vrata.GetCharacter(znak);
  38. }
  39. stevec++;
  40. kanal = 1;
  41. a=znak;
  42. while (stevec<200)
  43. {
  44. Vrata.GetCharacter(znak);
  45. b=znak;
  46. stevec++;
  47.  
  48.  
  49. c = b & 127;
  50. for(int i=0; i< 5 ; i++)
  51. {
  52. if((a >> i) & 1)
  53. {
  54. c = c | (1 << 7+i);
  55. }
  56. }
  57. c1 = c / 256;
  58. c2 = c & 255;
  59.  
  60. printf("Rezultat: A:%x(%s)", a, ByteToString(a));
  61. printf(" - B:%x(%s)\n", b, ByteToString(b));
  62. printf("Odkodiranje: C1:%x(%s)", c1, ByteToString(c1));
  63. printf(" - C2:%x(%s) Kanal: %d\n", c2, ByteToString(c2), kanal);
  64.  
  65.  
  66. Vrata.GetCharacter(znak);
  67. a=znak;
  68. stevec++;
  69. if (znak<0xC0)
  70. {
  71. kanal++;
  72. }
  73. else
  74. kanal = 1;
  75.  
  76. }
  77.  
  78. if(Vrata.GetCharacter(znak))
  79. {
  80.  
  81. Vrata.SendCharacter(a);
  82. Vrata.SendCharacter(b);
  83. Vrata.SendCharacter(c1);
  84. Vrata.SendCharacter(c2);
  85. }
  86.  
  87.  
  88. printf("Program se koncuje.\n");
  89. system("pause");
  90. FreeConsole();
  91. return 0;
  92. }
Last edited by Ancient Dragon : Nov 6th, 2007 at 8:19 am. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: COMport C++ problem

  #2  
Nov 6th, 2007
>> 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.
Last edited by Ancient Dragon : Nov 6th, 2007 at 8:26 am.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: Pačo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Pačo Pačo is offline Offline
Newbie Poster

Re: COMport C++ problem

  #3  
Nov 7th, 2007
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() ?
And I don't know where to insert it
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,825
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 191
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: COMport C++ problem

  #4  
Nov 7th, 2007
Originally Posted by Pačo View Post
{
FILE * pFile;
pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fputs ("fopen example",pFile);
fclose (pFile);
}
return 0;
}
Problem is...there is no fprintf() ?
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
Reply With Quote  
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,012
Reputation: ithelp will become famous soon enough ithelp will become famous soon enough 
Rep Power: 6
Solved Threads: 68
ithelp ithelp is offline Offline
Veteran Poster

Re: COMport C++ problem

  #5  
Nov 7th, 2007
You have to interpret the data arriving at com port and then write them back to file, writing directly might give garbage value.
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: Pačo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Pačo Pačo is offline Offline
Newbie Poster

Re: COMport C++ problem

  #6  
Nov 12th, 2007
Originally Posted by ithelp View Post
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 )
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC