ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have two programs that read the Serial Port buffer and write the data in two text files respectively.
I use fopen() and fclose(), passing pointers, as parameters to access the Serial Port in each program.
When I execute the first program, the data is written into the first text file, which is normal. However, when I execute the second program, the data is written into the first text file again. If I execute the second program a second time, the data is then written into the second text file.
I am running Mandrake and programming in C. Does anybody know what I could be doing wrong?
You could be using the same variable to address the filename... Try naming the filename variable differently (if that sollves your problem) or try clearing before reusing it.
I have different pointer variables addressing the filename (namely "temperature" and "humidity"). But the pointers to the Serial Port are of the same name in both programs. Maybe that could be the cause of the problem. I'll check it out. Thanks!
I originally wanted to do that but I know people don't quite like debugging other people's code.
Three pointers are declared in each of the two programs. One program reads a temperature value from the serial port buffer. The other program reads a humidity value.
The two programs are identical except in the second program, the first pointer is declared as
Code:
FILE *humidity;
Pointer temperature points to a text document where the data is written into.
Pointer port_read points to the serial port used when reading.
Pointer port_write points to the serial port used when writing.
OK, I renamed the pointers in the two programs so that no two variables names match. However, the problem still exists. For some reason, the first piece of data read is always written into the file that was accessed last.
Here is a description of what happens when I execute the programs.
1) When I execute measure_temperature.exe, a blank file called "temperature.txt" is created.
2) When I execute it a second time, only then is the data written into the text file.
3) When I execute measure_humidity.exe, a blank file called "humidity.txt" is created but the data is written into "temperature.txt".
This is the code of measure_temperature.exe in it's entirety.
Code:
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
unsigned int counter=0;
unsigned char data=0;
FILE *temperature; //Pointer to "temperature" of type file.
FILE *port_read_temperature; //Pointer to "port" of type file.
FILE *port_write_temperature; //Pointer to "port" of type file.
//Open the Serial Port (COM1).
port_write_temperature=fopen("/dev/ttyS0","w");
//Error Handling.
if(port_write_temperature==NULL)
{
perror("fopen"); //Print Error.
exit(1); //Exit Program.
}
fprintf(port_write_temperature,"T"); //Send Character "T" to the Serial Port.
fclose(port_write_temperature);
//Open a DAT file to write temperature values into.
temperature=fopen("temperature.txt","w");
//Open the Serial Port (COM1).
port_read_temperature=fopen("/dev/ttyS0","r");
fscanf(port_read_temperature,"%c",&data);
//While 'Y' is not received.
while(data!='Y')
{
fprintf(temperature,"%c",data); //Print Data to DAT file.
fscanf(port_read_temperature,"%c",&data);
}
fclose(port_read_temperature); //Close Serial Port.
fclose(temperature); //Close DAT file.
return 0;
}
By use of a newline character, I presume that you mean inserting a "\n" in the code. I have tried that but it does not seem to make a difference. I am guessing it is jumping to a newline in the text file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.