I'm new here, but I've spent the last couple hours searching your archives, and this llooks like a really terrific forum!!!!
I am working on a project that has my computer communicating with a microcontroller over an RS-232 connection. I've got the program on the microcontroller working flawlessly with minicom at 9600 8N1. I should say that I am programming in C on an OSX/BSD unix machine.
i've just spent the day trying to write a C program to communicate read and write to the serial port, so I don't have to use minicom, and I and pretty much baffeled.
The program on the microcontroller send out a short text string, and then will listen on the serial port and repeat back any thing you type on it... character by character... forever.
So, I've been following the instruction in Serial Programming Guide for POSIX Operating Systems
and here is the code that I've written... though it is mainly copied from the text...
Anyway, the problem I keep running into is that the read() returns the error "Resources temprarily unavailable." The write() seems to be doing fine, and I would expect that I would be able to read the 4 bytes that I just sent with the write(), but no.
#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 */
* 'open_port()' - Open serial port 1.
* Returns the file descriptor on success or -1 on error.
int fd; // File descriptor
fd = open_port();
// Read the configureation of the port
struct termios options;
tcgetattr( fd, &options );
/* SEt Baud Rate */
cfsetispeed( &options, B9600 );
cfsetospeed( &options, B9600 );
//I don't know what this is exactly
options.c_cflag |= ( CLOCAL | CREAD );
// Set the Charactor size
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
// Set parity - No Parity (8N1)
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Disable Hardware flowcontrol
// options.c_cflag &= ~CNEW_RTSCTS; -- not supported
// Enable Raw Input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// Disable Software Flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
// Chose raw (not processed) output
options.c_oflag &= ~OPOST;
if ( tcsetattr( fd, TCSANOW, &options ) == -1 )
printf ("Error with tcsetattr = %s\n", strerror ( errno ) );
printf ( "%s\n", "tcsetattr succeed" );
fcntl(fd, F_SETFL, FNDELAY);
// Write some stuff !!!
n = write(fd, "ATZ\r", 4);
if (n < 0)
fputs("write() of 4 bytes failed!\n", stderr);
printf ("Write succeed n = %i\n", n );
for ( n = 0 ; n < 1000 ; n++)
n = read( fd, buf, 1 );
if ( n == -1 )
printf ( "Error = %s\n", strerror( errno ) );
printf ( "Number of bytes to be read = %i\n", n );
printf ( "Buf = %s\n", buf );
close( fd );
int fd; /* File descriptor for the port */
fd = open("/dev/tty.KeySerial1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
* Could not open the port.
perror("open_port: Unable to open /dev/ttyS0 - ");
fcntl(fd, F_SETFL, FNDELAY);
printf ( "In Open port fd = %i\n", fd);
I don't really know what this means or what to do about it. I'm pretty stuck. I've shut down minicom, and rebooted the system so I know (I think) that minicom doesn't have any hold on the port.
Any suggestions would be much apprecated.