Read th Serial data from the port

I'm trying to write the data(Serial port data) in text file. But i want the output write like.......
desire output:

2343 3434 2344 2345 3552
2785 8945 7864 3458 7688...
......
but the program output getting like
12344
34356
35666
..
..
how to change the read function
program code:

void read()
{
uint32_t data[4];
DWORD bytesRead;
Readfile(hcomm, &data,sizeof(data),&bytesRead,Null);
File*fp;
fp=("c:\\data.txt","a+");
for(int i=0;i<5 ;i++)
{
fwrite(&data[i],sizeof(&data),1,f);
}
fprintf(fp,"\n");
}
Last edited on
If you really want to be helped, you should post a compilable code (using code tags) and what data you received to serial port.
Your code doesn't compile because:
1) missing #includes
2) Null and File don't exist
3) the variable f in line fwrite(&data[i],sizeof(&data),1,f); doesn't exist
4) the function Readfile don't exist
5) the variable hcomm don't exist
6) the line fp=("c:\\data.txt","a+"); should be fp=fopen("c:\\data.txt","a+");

Then, at first sight, I see some problems:
1) you ignored the return value of Readfile
2) you ignored the bytesRead value
3) the line for(int i=0;i<5 ;i++) should be for(int i=0;i<4 ;i++)
4) the line fwrite(&data[i],sizeof(&data),1,f); should be fwrite(&data[i],sizeof(data[i]),1,fp);
ok, i have a small doubt , when the Serial port data is come randomly like
1
2
3
4
5
;..
then we have read the data and write into txt.file , the txt.file data also come like this
1
2
3
4
5...
if any way we can write the data 4 column like the example ?
example :
1 2 3 4
5 6 7 8...
please help me.
it is a text file, so it doesn't have columns.
You could represent column with tabs, for example, replacing your code
1
2
3
4
for(int i=0;i<5 ;i++)
{
fwrite(&data[i],sizeof(&data),1,f);
}
with
1
2
3
4
5
fprintf(fp, "%u", data[0]);
for(int i = 1; i < 4; ++i)
{
	fprintf(fp, "\t%u", data[i]);
}

Otherwise, post a compilable code, so we can fix it.
Topic archived. No new replies allowed.