Controlling the Serial port

Bernd Walter ticso at cicely12.cicely.de
Wed Jul 7 15:51:31 PDT 2004


On Tue, Jul 06, 2004 at 10:53:23PM -0400, Greg Hormann wrote:
> 
> I'm working to develop a communications program to control a piece of
> vendor supplied hardware. (They only provide a control program for
> Windows.) I think I'm close. The device uses a RS-232 -> RS-485 convert
> which I belive is powered by either the RTS or DTR line of the serial
> port.  How can I force these two lines to say powered in FreeBSD inside a
> C program thus providing power to the converter?  I only need to
> write to the device.

So you are doing half duplex rs485 and need to control the transmitter.
Be aware that the code uses variables in a way which is generally a
bad idea - I was into uC programming when I did this...

static int fd;
static int tciotmp;

inline void
txon() {

	ioctl(fd, TIOCMGET, &tciotmp);
	tciotmp &= ~TIOCM_RTS;
	ioctl(fd, TIOCMSET, &tciotmp);
	tciotmp |= TIOCM_RTS;
}

inline void
txoff() {

	tcdrain(fd); // wait until all data is send
	ioctl(fd, TIOCMSET, &tciotmp);
}

The problem is timing as we need to have the transmitter disabled
bevor the connected device responds to our request.
There is no garanty that this is allways fast enough from userland.
It can also be a good idea to clear the receive buffer after enabling
the transmitter and bevor sending anything to wast stall data.

-- 
B.Walter                   BWCT                http://www.bwct.de
bernd at bwct.de                                  info at bwct.de



More information about the freebsd-hackers mailing list