Arduino Mega not resetting via serial [solved]

Edit: DTR actually wasn't being set low like it's supposed to be when the connection is closed. I fixed it by setting the baud rate to 0 (this also causes DTR to go low) on the connection before closing it.

I'm working on a robot for a competition, we have an Arduino Mega 1280 connected to a small computer running Ubuntu Server 12.04. we are able to reset the Arduino by opening the serial monitor in the Arduino IDE but not via our main program. We've looked at the IDE source and haven't been able to find what it does differently from ours, any help would be greatly appreciated.

Here is the code we are using to open a serial connection to the arduino.

void set_baud_rate(int baud_rate)
{
	struct termios options;
	int i;
	
	/*
	 * Get the current options for the port...
	 */
	tcgetattr(fd, &options);

	print_options(&options);
	/*
	 * Set the baud rates to 115200...
	 */

	cfsetispeed(&options, baud_rate);
	cfsetospeed(&options, baud_rate);

	/*
	 * Enable the receiver and set local mode...
	 */
	 
	options.c_cflag &= ~CSTOPB;
	options.c_cflag &= ~(IXON|IXOFF);
	#ifdef CRTSCTS
	options.c_cflag &= ~CRTSCTS;
	#else
	options.c_cflag &= ~CNEW_RTSCTS;
	#endif
	
	options.c_cflag &= ~PARENB;

	options.c_cflag &= ~(ICANON | ECHO | ECHOE | ISIG);
	
	options.c_cflag &= ~(ISTRIP | IGNCR | INLCR | ICRNL
	#ifdef IUCLC
		| IUCLC
	#endif
	);
	
	options.c_cflag &= ~(OPOST
	#ifdef ONLCR
		| ONLCR
	#endif
	#ifdef OCRNL
		| OCRNL
	#endif
	#ifdef ONOCR
		| ONOCR
	#endif
	#ifdef ONLRET
		| ONLRET
	#endif
	);

	options.c_iflag &= ~IXON;
	options.c_iflag &= ~ICRNL;
	options.c_iflag |= INPCK;

	options.c_oflag &= ~ONLCR;
	options.c_oflag &= ~OPOST;

	options.c_iflag = INPCK;

	options.c_cflag |= CS8;
	options.c_cflag |= CLOCAL;
	options.c_cflag |= CREAD;
	options.c_cflag |= baud_rate;

	options.c_lflag = 0;

	if (tcsetattr(fd, TCSANOW, &options) < 0)
	{
		printf("Unable to configure serial port\n");
	}

	print_options(&options);
}

void close_port(void)
{
	close(fd);
}

int open_port(void)
{
	int error;
	fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1)
	{	//Could not open the port.
		printf("open_port: Unable to open %s - ", SERIAL_PORT);
		return fd;
	}
	fcntl(fd, F_SETFL, 0);
	set_baud_rate(B9600);

	error = close(fd);

	if (error == -1)
	{
		printf("I failed at life %d times\n", errno);
		return -1;
	}
	else
	{
		printf("Everything is fine\n");
	}

	sleep(1);

	fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1)
	{	//Could not open the port.
		printf("open_port: Unable to open %s - ", SERIAL_PORT);
		return fd;
	}
	fcntl(fd, F_SETFL, 0);
	set_baud_rate(B115200);

	sleep(4);
	
	printf("Done opening serial port\n");

	return (fd);
}

Well I can't help you with the Linux stuff, but the arduino board's auto-reset function is activated by having the serial port assert the 'DTR' control signal. Does your opening of a serial port perform that step?

Lefty

It should however it seems not to be reliably doing it.