예제 #1
0
/*
@note			Instead of passing function pointers, it may make more sense to
				have the caller set those elements after calling xbee_dev_init.
				Unless they are necessary during the init process, there's no need
				to clutter up the function call with extra parameters, especially if
				we need functions to set the RTS pin or read the CTS pin.

@note			Consider having a field in the structure to indicate when it has been
				properly initialized, and have various functions check that field
				before continuing.
*/
_xbee_device_debug
int xbee_dev_init( xbee_dev_t *xbee, const xbee_serial_t *serport,
											xbee_is_awake_fn is_awake, xbee_reset_fn reset)
{
	int error;

	if (! xbee)
	{
		return -EINVAL;
	}

	#ifdef XBEE_PLATFORM_INIT
		XBEE_PLATFORM_INIT();
	#endif

	// try communicating with the XBee module
	// set xbee to all zeros, then
	// set up serial port and attempt communications with module
	memset( xbee, 0, sizeof( xbee_dev_t));

	// configuration for serial XBee
	xbee->is_awake = is_awake;	// function to read XBee's "ON" pin
	if (reset)
	{
		xbee->reset = reset;		// function to assert XBee's reset pin
		reset( xbee, 0);			// take XBee out of reset state
	}

	xbee->serport = *serport;
	error = xbee_ser_open( &xbee->serport, serport->baudrate);
	if (! error)
	{
		error = xbee_ser_flowcontrol( &xbee->serport, 1);
	}

	xbee->flags = XBEE_DEV_FLAG_USE_FLOWCONTROL;

	#ifdef XBEE_DEVICE_ENABLE_ATMODE
		// fill in default values for GT, CT and CC registers
		xbee->guard_time = 1000;
		xbee->escape_char = '+';
		xbee->idle_timeout = 100;
	#endif

	return error;
}
int xbee_ser_set_rts( xbee_serial_t *serial, int asserted)
{
	BOOL success;

	XBEE_SER_CHECK( serial);

	// disable flow control so our manual setting will stick
	xbee_ser_flowcontrol( serial, 0);

	success = EscapeCommFunction( serial->hCom, asserted ? SETRTS : CLRRTS);

	#ifdef XBEE_SERIAL_VERBOSE
		if (success == 0)
		{
			printf( "%s: EscapeCommFunction error %lu\n", __FUNCTION__,
					GetLastError());
		}
	#endif

	return success ? 0 : -EIO;
}
예제 #3
0
int xbee_ser_set_rts( xbee_serial_t *serial, int asserted)
{
	int status;

	XBEE_SER_CHECK( serial);

	// disable flow control so our manual setting will stick
	xbee_ser_flowcontrol( serial, 0);

	if (ioctl( serial->fd, TIOCMGET, &status) == -1)
	{
		#ifdef XBEE_SERIAL_VERBOSE
			printf( "%s: ioctl %s failed (errno=%d)\n", __FUNCTION__,
				"TIOCMGET", errno);
		#endif
		return -errno;
	}

	if (asserted)
	{
		status |= TIOCM_RTS;
	}
	else
	{
		status &= ~TIOCM_RTS;
	}

	if (ioctl( serial->fd, TIOCMSET, &status) == -1)
	{
		#ifdef XBEE_SERIAL_VERBOSE
			printf( "%s: ioctl %s failed (errno=%d)\n", __FUNCTION__,
				"TIOCMSET", errno);
		#endif
		return -errno;
	}

	return 0;
}