Re: serialPortSample... hang on "read" function
Re: serialPortSample... hang on "read" function
- Subject: Re: serialPortSample... hang on "read" function
- From: Louis Demers <email@hidden>
- Date: Tue, 17 Feb 2009 13:11:38 -0500
On 17-Feb-09, at 12:56 , Sean McBride wrote:
On 2/17/09 6:52 PM, Boris Prohaska said:
Hi Guys... i hope someone can help me out here.
In the serialPortSample.h on line 517 in the InitializeModem();
function there is this classic "read" command. But when no modem is
here or the cable is unplugged, it hangs there for an infinite amount
on time.
My question is rather simple:
Is there a way to find out if the cable is connected or the other
way... to define a timeout, that it will return with an error?
here is code I dug out. I, myself, found it browsing on the web and
adapted it. Provided without warranty.
//-----------------------------------------------------------------------------------------------
int OpenMySerialPort( char * path, int timeout)
{
struct termios options;
// struct termios gOriginalTTYAttrs;
int serialDescriptor;
//// serialDescriptor = open( path, O_RDWR | O_NOCTTY | O_NDELAY );
serialDescriptor = open( path, O_RDWR | O_NOCTTY );
/// NSLog( @"Opening Serial Port %s.\n", path );
if ( serialDescriptor == -1 )
{
/// NSLog( @"Error %d opening serial port %s.\n", errno,
path );
return (-1);
}
do {
/// NSLog( @"No Problem opening serial port.\n" );
if (fcntl( serialDescriptor, F_SETFL, 0 ) == -1)
{
/// NSLog( @"Error clearing O_NDELAY %s - %s(%d).\n",
path , strerror(errno), errno);
break;
}
// Get the current options and save them for later reset
if (tcgetattr(serialDescriptor, &gOriginalTTYAttrs) == -1)
{
/// NSLog( @"Error getting tty attributes %s - %s(%d).
\n", path , strerror(errno), errno);
break;
}
// Set raw input, one second timeout
// These options are documented in the man page for termios
// (in Terminal enter: man termios)
options = gOriginalTTYAttrs;
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[ VMIN ] = 0;
options.c_cc[ VTIME ] = timeout;
options.c_ispeed = B115200;
options.c_ospeed = B115200;
// Set the options
if (tcsetattr(serialDescriptor, TCSANOW, &options) == -1)
{
/// NSLog( @"Error setting tty attributes %s - %s(%d).
\n", path , strerror(errno), errno);
break;
}
return serialDescriptor;
} while (0);
// Failure path
close (serialDescriptor);
return -1;
} // end of OpenModemPort
Have you seen the AMSerialPort class?
<http://www.harmless.de/cocoa-code.php>
I think it will make your life much easier.
--
____________________________________________________________
Sean McBride, B. Eng email@hidden
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
Louis Demers eng.
www.obzerv.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden