Reading and writing to com port and saving data in Text file

P

Thread Starter

Path2success

Language:-c
Platform:- Linux (Ubuntu10.04)
Reading and writing to modem serial port. i.e com port. for reading taking data from com port and saving it in text file.

I am receiving and sending the fax to the multitech modem using AT commands. but i want the solution for how do i read data from com port and store it in text file as fax data. and how do i send the AT commands to the modem? i.e to write the AT commands to com port. and i want to do this in C language that also in Linux (Ubuntu 10.04) platform. any help will be appreciated.
 
J

James Ingraham

Generally, reading text from serial port to file is trivially easy in *nix OSes. Here's a command that would work for Ubuntu (and many others):

cat < /dev/ser1 > myfile

This is redirection at its finest. There's no work to do in C.

However, I'm a little confused by your question, particularly this part:
"I am receiving and sending the fax to the multitech modem using AT commands"

If you are in fact receiving and sending a fax, the above will not give you anything useful. A fax is not text. So the command I gave you will work, but when you try to look at the file it will look like garbage. So you need to clarify: is the person on the other end of this connection sending ASCII text, or are they in fact sending a fax?

"how do i send the AT commands to the modem? i.e to write the AT commands to com port."

Two ways, the trivial and the slightly less trivial.

1) The trivial way is to use standard "printf" (or puts or putc...) in your C program. Then when you run it use redirection:
myprog > /dev/ser1

2) Only slightly less troublesome is to use fopen to get the serial port, and fprintf (or fputs...) to write to the modem. (Warning: I'm typing this here, rather than giving you code that I know compiles and works, so be careful with it.)<pre>
FILE *ser1;
int hangup=1;
ser1 = fopen("/dev/ser1", "w");
if (ser1==NULL){
// deal with failure
}
fprintf(ser1, "ATH%d\n", hangup);</pre>
Hope that helps.

-James Ingraham
Sage Automation, Inc.
 
C

curt wuollet

A good way would be to find a simple terminal emulator and read the source. There are many for linux and I think all are Open Source.

Regards
cww
 
P

path2success

i have opened the .tiff file with fopen() in read mode. and have taken buffer of size 1024. and reading the tiff file with fread & taking the data in buffer and sending it to comport. but i dont know what i am doing is rt or wrong coz. m not getting proper reply from modem after sending this data. and i also want to know sending one page of data is how many bytes. m also calculating size of tiff file and sending data by buffering it in 1024 size buffer till eof.please tell anybody have any solution to do this in c.
 
Top