PDA

View Full Version : Serial port programs



tarunuppal
16th April 2008, 02:02 PM
Hi
Can some one give me some simple programs to write some bytes of data to serial port.
thanks & regards,
Tarun

rbo83
16th April 2008, 02:10 PM
Depending on whether you want to dial or just send data, you can write directly to the /dev/ttySx serial port (x is a port number) Check the wvdial manual page (man wvdial) to test dialing through one of thee ports

tarunuppal
16th April 2008, 04:16 PM
Hi
Thanks for reply.
But iam looking for is a c program sample.
I have a c based simulation program running on my fedora pc and wish to send some data out from the pc. The data is put in an array.
thanks & regards
Tarun Uppal

bryancole
16th April 2008, 04:44 PM
Pity you need a C-program. In python you can do:

from serial import Serial
dev = Serial(port=1, baudrate=9600)
dev.write("here are some bytes")
dev.close()

In C, you just open the /dev/ttyS* file, write some bytes to it (or read some) and close it when you're done. Controlling the port parameters is a PITA though. You can find lots of instructions if you google for it. Try http://www.easysw.com/~mike/serial/serial.html

If I were you, I'd take the core simulation and build it as a shared library. You can then trivially call in to this from python (using python's ctypes module) passing in/out data as C datatypes/structures or large arrays, then do all the IO / GUI / Control stuff from python (where it's way easier than C). If this approach is of any interest, I can detail it further but using python might not be an option for you.

stevea
17th April 2008, 04:24 AM
Ther are a couple unaddressed questions here. Does this program control the tty (set the baud rate & such) or is it just output.

The C code sequence for the data I/O is like ...
FILE *fp;
fp = fopen("/dev/ttyS1", "rw");
fprintf(fp, "this is a test of serial port 1\n");
fclose(fp);
Requires stdio.h and perhaps others. If yo uneed to modify baud rate and such examine the termios call ("man termios").
--
Another point - do yo ureally want to make this a serial-line only program ? You can redirect the I/o of any program like this:
progname </dev/ttyS1 >/dev/ttyS1
for example:
echo "this is test of port1" >/dev/ttyS1
also writes to the serial port. Perhaps you want to make a generic program to read/write to stdin, stdout and then redirect the input/output when yo ulaunch the program.
==
If I can ask, why a serial port ? Why not network I/O ?

tarunuppal
17th April 2008, 05:08 AM
Hi,
Thanks for the suggestions. They did help me.
This is a real time multiple aircraft simulation as a part of my MS thesis . In fact, i am looking for some real time serial port driver for RTAI(RT Linux) for this application. D you have any idea about that?
thanks & regards,
Tarun Uppal