Jump to content

Saving from com port to text file help!!

- - - - -

  • Please log in to reply
10 replies to this topic

#1
cs342

cs342

    Newbie

  • Members
  • Pip
  • 6 posts
Hi,
I am working on a project to read data coming in from the comport and then save it to a text file. I have managed to read the data coming from the comport however i am now having trouble with sending it to a text file. can anyone help pls? this is my code so far:

include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>

#define COM1 8
#define SETTINGS ( _COM_9600 | _COM_NOPARITY | _COM_STOP1 | _COM_CHR8)

int main (void)
{
DCB dcbCommPort;
COMMTIMEOUTS CommTimeouts;
DWORD BytesRead;
char InBuffer[100];
int hComm;

hComm = CreateFile( "COM9",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);


if (hComm == INVALID_HANDLE_VALUE)
{
printf("Could not open comm port.\n");
return;
}


dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);

if(!BuildCommDCB("baud=9600 parity=N data=8 stop=1", &dcbCommPort))
{
hComm = NULL;
printf("Cannot build comm DCB.\n");
return;
}

if(!SetCommState(hComm, &dcbCommPort))
{
CloseHandle(hComm);
hComm = NULL;
printf("Cannot set comm state.\n");
return;
}

CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 250;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;

if(!SetCommTimeouts(hComm, &CommTimeouts))
{
CloseHandle(hComm);
hComm = NULL;
printf("Cannot set comm timeouts.\n");
return;
}


int CommFlag = 1;

while(CommFlag)
{

Sleep(1);
ReadFile(hComm, InBuffer, 100, &BytesRead, NULL);

if(BytesRead)
{
InBuffer[BytesRead] = 0;
printf("%s",InBuffer);
}
}
}

I am using a GCC compile on codeblocks in windows vista
THANKS!!

#2
setjmp

setjmp

    Newbie

  • Members
  • PipPip
  • 21 posts
Function fopen and fputc help you

#3
cs342

cs342

    Newbie

  • Members
  • Pip
  • 6 posts

setjmp said:

Function fopen and fputc help you

yes ive tried that but it doesn't work. this is what ive tried:

FILE *fp;
fp=fopen("temp.txt", "w");
fputc(InBuffer, fp);
fclose(fp);

when I open the text file it just has a euro sign in it.

#4
setjmp

setjmp

    Newbie

  • Members
  • PipPip
  • 21 posts
you forgot cycle.

Work example:

#include "iostream"
#include "stdio.h"

int main()
{
char *InBuffer = "It's test";
FILE *fp;
fp=fopen("temp.txt", "w");
for(int t=0; t<=strlen(InBuffer); t++){
fputc((int)InBuffer[t], fp);
}
fclose(fp);
}

#5
cs342

cs342

    Newbie

  • Members
  • Pip
  • 6 posts
why did you use char *InBuffer = "Это просто тест";?? what does it mean?

#6
setjmp

setjmp

    Newbie

  • Members
  • PipPip
  • 21 posts
sorry, confused the language ^^

#7
cs342

cs342

    Newbie

  • Members
  • Pip
  • 6 posts
i tried it like this:
if(BytesRead)
{
InBuffer[BytesRead] = 0;
printf("%s",InBuffer);
char *InBuffer[BytesRead];
FILE *fp;
fp=fopen("temp.txt", "w");
for( t=0; t<=strlen(InBuffer); t++)
{
fputc(InBuffer[t], fp);
}
fclose(fp);
but it still doesnt work!

#8
setjmp

setjmp

    Newbie

  • Members
  • PipPip
  • 21 posts

Quote

if(BytesRead)
{
InBuffer[BytesRead] = 0;
printf("%s",InBuffer);
char *InBuffer[BytesRead];
FILE *fp;
fp=fopen("temp.txt", "w");
for( t=0; t<=strlen(InBuffer); t++)
{
fputc(InBuffer[t], fp);
}
fclose(fp);

BytesRead - what is it? If is it int or char then such a record is not good. Take away if(){}.
InBuffer[BytesRead] = 0; - you using var. befor to its announcement.

#9
cs342

cs342

    Newbie

  • Members
  • Pip
  • 6 posts
if(BytesRead) means if bytes are being read from the comport do...
should i remove InBuffer[BytesRead]=0; or just put it somwhere else?

#10
setjmp

setjmp

    Newbie

  • Members
  • PipPip
  • 21 posts
I said all. Think myself, if want to learn programming.

#11
cs342

cs342

    Newbie

  • Members
  • Pip
  • 6 posts
thanks anyway. Any one else can help pls?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users