Пример #1
0
void serialOpen(Serial *s, char *device, int baudCode, int _verbose) {
	struct termios options;
	int r;

	s->verbose = _verbose;

	// Open the serial port.
	if(s->verbose) printf("Serial: opening serial device %s\n", device);
	//s->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
	s->fd = open(device, O_RDWR | O_NOCTTY);
	if(s->fd == -1) {
		int errsv = errno;
		fprintf(stderr, "Serial: ERROR: Could not open serial port %s\n", device);
		fprintf(stderr, "Serial: ERROR: %s\n", strerror(errsv));
		exit(1);
	}
	
	// Non-blocking reads.
	r = fcntl(s->fd, F_SETFL, FNDELAY);
	assert(r != -1);
	
	// Baud rate.
	serialSetBaud(s, baudCode);	
	
	// Other options:
	r = tcgetattr(s->fd, &options);
	assert(r != -1);
	
	// - 8N1
	options.c_cflag &= ~PARENB;
	options.c_cflag &= ~CSTOPB;
	options.c_cflag &= ~CSIZE;
	options.c_cflag |= CS8;

	// Don't "own" the port.
	options.c_cflag |= CLOCAL;

	// - Allow reads.
	options.c_cflag |= CREAD;

	// - Raw input.
	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

	// - Disable parity checking.
	options.c_iflag &= ~(INPCK | ISTRIP);

	// - No hardware flow control.
	options.c_cflag &= ~CRTSCTS;

	// - No software flow control.
	options.c_iflag &= ~(IXON | IXOFF | IXANY);	

	// - Raw output.
	options.c_oflag &= ~OPOST;
	
	// Actually set the options.
	r = tcsetattr(s->fd,TCSANOW, &options);
	assert(r != -1);

}
Пример #2
0
int
serialSetParameters (SerialDevice *serial, const SerialParameters *parameters) {
  if (!serialSetBaud(serial, parameters->baud)) return 0;
  if (!serialSetDataBits(serial, parameters->dataBits)) return 0;
  if (!serialSetStopBits(serial, parameters->stopBits)) return 0;
  if (!serialSetParity(serial, parameters->parity)) return 0;
  if (!serialSetFlowControl(serial, parameters->flowControl)) return 0;
  return 1;
}
Пример #3
0
static int
serialConfigureBaud (SerialDevice *serial, const char *string) {
  if (string && *string) {
    unsigned int baud;

    if (!serialParseBaud(&baud, string)) return 0;
    if (!serialSetBaud(serial, baud)) return 0;
  }

  return 1;
}
Пример #4
0
//Connect to the Barrett Hand via serial port and start the worker thread
BHand* bhand_connect(char *handport) {
	int res;
	BHand *hand = (BHand*)malloc(sizeof(BHand));
	hand->queueHead = hand->queueTail = 0;
	hand->lastResponseIndex = -1;
	hand->currentCommandIndex = 0;

	//connect to the hand via serial
	if (serialOpen(&(hand->port),handport)==1) {
		printf("bhand_connect: error opening serial port\n");
		goto error;
	}
	serialSetBaud(&(hand->port),38400);

	//create mutex
  res = pthread_mutex_init(&(hand->mutex), NULL);
  if (res) {
		printf("bhand_connect: error creating mutex\n");
		goto error;
	}

	//create worker thread
  res = pthread_create(&(hand->thread), NULL, processCommands, (void*)hand);
  if (res) {
		printf("bhand_connect: error creating thread\n");
		goto error;
	}

	//send empty command (equivalent of pressing enter)
	char response[MAX_FEEDBACK_LEN];
	bhand_sendCommandResponse(hand, "", response);

	printf("hand connected\n");

	// return
	return hand;

error:
	free(hand);
	return NULL;	

}
Пример #5
0
int
serialRestartDevice (SerialDevice *serial, unsigned int baud) {
  SerialLines highLines = 0;
  SerialLines lowLines = 0;
  int usingB0;

#ifdef HAVE_POSIX_THREADS
  SerialFlowControlProc *flowControlProc = serial->pendingFlowControlProc;
#endif /* HAVE_POSIX_THREADS */

  if (serial->stream) {
#if defined(GRUB_RUNTIME)
#else /* clearerr() */
    clearerr(serial->stream);
#endif /* clear error on stdio stream */
  }

  serialClearError(serial);

  if (!serialDiscardOutput(serial)) return 0;

#ifdef HAVE_POSIX_THREADS
  serial->pendingFlowControlProc = NULL;
#endif /* HAVE_POSIX_THREADS */

#ifdef B0
  if (!serialPutSpeed(&serial->pendingAttributes, B0)) return 0;
  usingB0 = 1;
#else /* B0 */
  usingB0 = 0;
#endif /* B0 */

  if (!serialFlushAttributes(serial)) {
    if (!usingB0) return 0;
    if (!serialSetBaud(serial, baud)) return 0;
    if (!serialFlushAttributes(serial)) return 0;
    usingB0 = 0;
  }

  if (!usingB0) {
    SerialLines lines;
    if (!serialReadLines(serial, &lines)) return 0;

    {
      static const SerialLines linesTable[] = {SERIAL_LINE_DTR, SERIAL_LINE_RTS, 0};
      const SerialLines *line = linesTable;

      while (*line) {
        *((lines & *line)? &highLines: &lowLines) |= *line;
        line += 1;
      }
    }

    if (highLines)
      if (!serialWriteLines(serial, 0, highLines|lowLines))
        return 0;
  }

  asyncWait(SERIAL_DEVICE_RESTART_DELAY);
  if (!serialDiscardInput(serial)) return 0;

  if (!usingB0)
    if (!serialWriteLines(serial, highLines, lowLines))
      return 0;

#ifdef HAVE_POSIX_THREADS
  serial->pendingFlowControlProc = flowControlProc;
#endif /* HAVE_POSIX_THREADS */

  if (!serialSetBaud(serial, baud)) return 0;
  if (!serialFlushAttributes(serial)) return 0;
  return 1;
}