473,503 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing file on a serial device in ANSI C

4 New Member
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
5 5343
horace1
1,510 Recognized Expert Top Contributor
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
lucky001
4 New Member
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
horace1
1,510 Recognized Expert Top Contributor
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
lucky001
4 New Member
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
horace1
1,510 Recognized Expert Top Contributor
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

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

Similar topics

4
9061
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
2
12063
by: Tyaan | last post by:
Hi.. I'm a perl noob need to know how to write a script to parse a file containing one to four of the following blocks of text? I then want to print the results in a format showing the memory size...
21
2955
by: The Man With The Harmonica | last post by:
Hi All, What kind of commands can I use to control the serial and parallel port of my pc. I want to use my laptop instead of microcontrollers and write my software in ANSI C to be used for...
13
6829
by: jay.dow | last post by:
I want to write to the pins of an RS232 without using the serial protocol. The use would be every pin could act to complete a circuit in customized hardware. I could use python to communicate...
4
11028
by: Lonifasiko | last post by:
Hi, I've been able to communicate using HyperTerminal with my device via serial port COM1. I just send a command and device switches on. I just need that to start playing with it. This way, I...
1
3000
by: kameswara | last post by:
Hi folks , Can anyone help me out the problem regarding writing and reading data to/from a serial device like ttyS0 in linux . Writing is being done but unable to read from it . could u plea...
2
7007
by: Paul Sijben | last post by:
To automate/ease configuration in my app I am trying to find out to which serial port a certain bluetooth device is connected. With pybluez I can find out which bluetooth devices I have, but it...
2
7815
by: Nasif | last post by:
Currently I am writing a program which sends and receives messages through serial port to a device. I am using C# and Microsoft Visual studio 2005 for windows program. But my problem is when i try...
6
4629
by: brianrpsgt1 | last post by:
I am new to scripting. I am trying to read the settings from a serial device using Python. I have been able to successfully connect to the device and change baud rate settings, ect... with...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.