By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,908 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,908 IT Pros & Developers. It's quick & easy.

Writing file on a serial device in ANSI C

P: 4
Hi

I need to write a file using ANSI C:-

From a computer having unix
To a computer attached on a serial port (this computer has Window CE)

I am able to make a connection with computer having Window CE with open function

e.g.

int fd;

fd = open("/dev/ttyS0", O_RDWR | O_NDELAY);


Now on this opened serial port I need to open a file and write some data on it and then save it on computer connected to serial port.

I checked there is a WriteFile function in probably in C#, I am looking for same in ANSI C because it allows to provide handle and filename as argument.

Please consider I am new to C.
Any help will be appreciated

Regards
Mar 20 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
are you reading and writing the serial ports OK?

for file I/O have a read thru this tutorial
http://www.cprogramming.com/tutorial/lesson10.html
Mar 20 '07 #2

P: 4
Thanks for your reply.

Yes, I can see that my computer having Windows CE (it has a utility that monitors the serial ports) shows me it is connected to computer having unix. After running the following code:

================================================== ======

#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int fd; /* File descriptor for the port */
int file_name_size; /* File name Size */
int port_status;
int write_status;
FILE *fp;

/*
* 'open_port()' - Open serial port 1.
* Returns the file descriptor on success or -1 on error.
*/
int open_port()
{
fd = open("/dev/ttya13", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{

/* Could not open the port. */
perror("open_port: Unable to open /dev/ttya13 - ");
}
else
{
printf("Opened Port --> ttya13\n");
/* fcntl(fd, F_SETFD, 0); */
fp = fopen("aaa.dat", "w");
fprintf(fp, "\nSample Code\n\n");
/* write_status = fwrite(fp, 4, 1, fd); */
printf("value of write_status %d \n", write_status);
fflush(fp);
fclose(fp);
if (write_status < 0)
fputs("write() of 4 bytes failed!\n", stderr);
}
return (fd);
}

main()
{
port_status = open_port();
printf("The value of port_status is %d \n", port_status);
close(fd);
}

================================================== =======

Now I need to know, how can I open and create this "aaa.dat" file on device connected to my serial port. Right now, it is creating this data file in my current directly.

I am missing a part that can connect my file to serial port file descriptor.
Mar 20 '07 #3

Expert 100+
P: 1,510
as the serial port usually deals with a byte at time you would open the file, read it character by character transmitting each character over the serial line. You can do a similar thing to read from a serial port to a file.
Mar 21 '07 #4

P: 4
Ok - but suppose I need to open a file on device named "aaa.dat" and want to write a character in the file.

The real chellenge is to open a file on device connected on serial port.

Could you please guide me how?

Thanks
Mar 21 '07 #5

Expert 100+
P: 1,510
Ok - but suppose I need to open a file on device named "aaa.dat" and want to write a character in the file.

The real chellenge is to open a file on device connected on serial port.

Could you please guide me how?

Thanks
these days most devices I connect to serial ports are embedded controllers and data acquisition systems, e.g. they collected data at set rates or on certain events and transmit it to the host PC and the host may send commands to the device, etc.
Rereading your posts you have two PCs connected together and I assume you want to enter a command on one PC to open a file on the other and receive the contents?
This sounds like a simple FTP protocol and it would be worth reading thru the protocol to see its command structure
http://www.w3.org/Protocols/rfc959/2_Overview.html

A simple example would be one PC may send the command OPEN filename.txt, the other PC would decode the command, open the file and send it character by character, etc. You need to design a protocol so both ends can talk and understand each other.
Mar 21 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.