Excerpt from network.c

The only C code in the server software is the code that handles the low-level initialisation of the RS-232/485 port.

This is a snippet of that code showing the RTS and MARK/SPACE toggling routines.



/* force the RTS line low
 * passed the file descriptor for the RS-485 line
 */
void
dropRTS(int fd) {

  int lineData;

  ioctl(fd, TIOCMGET, &lineData);
  lineData &= ~TIOCM_RTS;
  tcdrain(fd);			/* send the data out and then drop the line */

  ioctl(fd, TIOCMSET, &lineData);
} /* dropRTS */


/* force the RTS line high
 * passed the file descriptor for the RS-485 line
 */
void
raiseRTS(int fd) {

  int lineData;

  ioctl(fd, TIOCMGET, &lineData);
  lineData |= TIOCM_RTS;
  ioctl(fd, TIOCMSET, &lineData);
} /* raiseRTS */


/* set the parity to MARK
 * this is controlling the LCR (line control register)
 * 0,1   : Number of data bits (00 = 5, 01 = 6, 10 = 7, 11 = 8)
 * 2     : Number of stop bits (0 = 1, 1 = 2)
 * 3,4,5 : Parity (000 = none, 001 = odd, 011 = even, 101 = mark, 111 = space)
 * 6     : Break control (0 = off, 1 = on)
 * 7     : DLAB (divisor latch access bit)
 */
void
markParity(int fd, struct termios tValue) {

  tValue.c_cflag |= CMSPAR;
  tValue.c_cflag |= PARODD;

  tcsetattr(fd, TCSADRAIN, &tValue);
} /* markParity */


/* set the parity to SPACE */
void
spaceParity(int fd, struct termios tValue) {

  tValue.c_cflag |= CMSPAR;
  tValue.c_cflag &= ~PARODD;

  tcsetattr(fd, TCSADRAIN, &tValue);
} /* spaceParity */


/* write an address byte to the network
 * the sequence is: MARK, write, SPACE
 * returns 1 on success, 0 on failure
 */
int
writeAddress(int fd, char addr) {

  int status = 0;		/* the file descriptor write return status */
  struct termios termVal;	/* current termios setting */

  tcgetattr(fd, &termVal);	/* grab the termios setting */
  markParity(fd, termVal);	/* set the parity to MARK */
  usleep(MARK_DELAY);		/* do a small (microsecond) pause */
  
  /* now write out the address */
  if (write(fd, &addr, 1) == 1) {
    status = 1;
  }
  
  usleep(MARK_DELAY);
  spaceParity(fd, termVal);	/* set the parity to SPACE for the data... */
  usleep(MARK_DELAY);

  return(status);
} /* writeAddress */


/* write a byte of data out to the network
 * returns 1 on success, 0 on failure
 */
int
writeData(int fd, char data) {

  int status = 0;		/* 0 = fail, 1 = okay */

  if (write(fd, &data, 1) == 1) {     /* now write out the data byte */
    status = 1;
  }

  return(status);
} /* writeData */


[Index] [Prev]

Updated: