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!!
10 replies to this topic
#1
Posted 24 May 2010 - 03:56 AM
|
|
|
#2
Posted 24 May 2010 - 04:09 AM
Function fopen and fputc help you
#3
Posted 24 May 2010 - 04:17 AM
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
Posted 24 May 2010 - 04:26 AM
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);
}
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
Posted 24 May 2010 - 04:39 AM
why did you use char *InBuffer = "Это просто тест";?? what does it mean?
#6
Posted 24 May 2010 - 04:42 AM
sorry, confused the language ^^
#7
Posted 24 May 2010 - 04:46 AM
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!
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
Posted 24 May 2010 - 04:56 AM
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);
{
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
Posted 24 May 2010 - 05:09 AM
if(BytesRead) means if bytes are being read from the comport do...
should i remove InBuffer[BytesRead]=0; or just put it somwhere else?
should i remove InBuffer[BytesRead]=0; or just put it somwhere else?
#10
Posted 24 May 2010 - 05:23 AM
I said all. Think myself, if want to learn programming.
#11
Posted 24 May 2010 - 05:54 AM
thanks anyway. Any one else can help pls?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









