Serial Port Configuration



Hi,

I'm trying to configure a serial port to be able to write to with or
without a device attached to it. If there is no device the data gets
dumped. Therefore I want raw I/O and no flow control of any kind, I
assume. Now, I've read the Serial Programming Howto and the Serial
Programming Guide for POSIX and I think I've got it right, but I must
be missing something. I have a small test pogram. After the data is
written to the serial port, tcdrain is called but it never returns,
just hangs there. I do not currently have anything attached to the
serial port and can only assume that nothing is being output (I'm a
little short of hardware currently). I've been hammering my head on
this for more than a day. So, what am I missing here?

#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/serial.h>

#define PORT "/dev/ttyS2"

int main()
{
int fd, status;
char *msg ="Hello world!";
struct termios options;
struct serial_struct z_ser;

/* Open and configure the serial port */
fd = open(PORT, O_RDWR | O_NOCTTY | O_NONBLOCK );

memset( &options, 0, sizeof(options) );
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD | IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_iflag = INPCK | ISTRIP;
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
options.c_line = N_TTY;

if ( tcflush( fd, TCIOFLUSH ) < 0 )
{
printf("prob flush\n");
}

if( tcsetattr( fd, TCSANOW, &options) < 0 )
{
printf("prob set\n");
}

/* Write output */
if ( write( fd ,msg, strlen( msg ) ) < 0 )
{
printf("prob write\n");
}

/* Waiting until the output buffer is empty */
printf( "waiting to drain\n" );

tcdrain( fd ); /* tcdrain(fd) waits forever */

printf( "drained\n" );

/* Close the port */
close( fd );
}


Here's the port settings too (after running program):
speed 9600 baud; rows 0; columns 0; line = 0;
intr = <undef>; quit = <undef>; erase = <undef>; kill = <undef>; eof =
<undef>;
eol = <undef>; eol2 = <undef>; start = <undef>; stop = <undef>; susp =
<undef>;
rprnt = <undef>; werase = <undef>; lnext = <undef>; flush = <undef>;
min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk inpck istrip -inlcr -igncr -icrnl -ixon
-ixoff
-iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0
bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase
-tostop -echoprt
-echoctl -echoke

Thanks,
Ed.

.



Relevant Pages

  • Re: Serial COM ports Dont Always Communicate
    ... send the base station in for testing. ... REAL serial port first, a friends or perhaps if you have another PC. ... some embedded programming, basically because I didn't have the time to do ... module would not program through the converter. ...
    (microsoft.public.windowsxp.general)
  • Re: FreeBSD programming question
    ... but I'm relatively new to programming for *nix (though not new ... HVAC system from any terminal on the network. ... you can selecton the file descriptors for the standard ... > input and the serial port, though remember that STDIN uses buffered IO ...
    (freebsd-questions)
  • Re: Cant go back to original BIOS on P5KC ?
    ... I use the serial port to load programming to Ham radios and police scanners. ... Shortly after the BIOS update the software refused to talk to the radios, saying it could not find anything attached to the port.. ... is *NOT* programming the boot block. ...
    (alt.comp.periphs.mainboard.asus)
  • global vs. local struct var - compiler bug?
    ... I'm using Visual Studio 2005, C++, programming for an HP ... iPaq running Windows Mobile 5. ... I wrote a program that uses a variable declared local to the routine ... tools whlie this is running with this device stuck in the serial port, ...
    (microsoft.public.pocketpc.developer)
  • Re: Timer Circuits or PIC
    ... There are also several other free or cheap programming environments. ... Once you get the serial port hooked up, you'll be able to modify the program and be back testing it in seconds. ... Email me if you wanna discuss further. ... Return address is VALID but some sites block emails with links. ...
    (sci.electronics.basics)