예제 #1
0
int openArduino(char* portname, char** error)
{
RUNLOG("#### arduino.js # start openArduino...\n");
	DCB dcb = {0};
	COMMTIMEOUTS timeout;

	if(handle != NULL){
		closeArduino();
	}

	handle = CreateFile(portname
		, GENERIC_READ | GENERIC_WRITE
		, 0
		, NULL
		, OPEN_EXISTING
		, FILE_ATTRIBUTE_NORMAL
		, NULL
	);
	if(handle == INVALID_HANDLE_VALUE){
		return -1;
	}

	GetCommState(handle, &dcb);
	dcb.BaudRate = CBR_9600;
	dcb.ByteSize = 8;
	dcb.Parity   = NOPARITY;
	dcb.StopBits = ONESTOPBIT;
	timeout.ReadIntervalTimeout         = 10;
	timeout.ReadTotalTimeoutMultiplier  = 1;
	timeout.ReadTotalTimeoutConstant    = 10;
	timeout.WriteTotalTimeoutMultiplier = 1;
	timeout.WriteTotalTimeoutConstant   = 10;
	if(SetCommState(handle, &dcb) == FALSE){
		return -1;
	}
	if(SetCommTimeouts(handle, &timeout) == FALSE){
		return -1;
	}
	Sleep(2000);

	//wait until Arduino finish the setup
	char buffer[BUFFER_SIZE];
	memset(buffer , '\0' , sizeof(buffer));
	response(buffer);

RUNLOG("#### arduino.js # return openArduino...\n");
	return 0;
}
예제 #2
0
int openArduino(char* portname, char** error)
{
  if (device_file_descriptor != -1) {
    closeArduino();
  }
  fprintf(stderr,"enter openArduino\n"); //KK

  //    NSLog(/*@*/"openArduino:[%s]",portname);

  const char *bsdPath = portname;
  struct termios gOriginalTTYAttrs;
  speed_t baudRate = 9600;
  unsigned long mics = 3;

  int	serialFileDescriptor = open(bsdPath, O_RDWR | O_NOCTTY | O_NDELAY );
  device_file_descriptor = serialFileDescriptor;
  // TIOCEXCL causes blocking of non-root processes on this serial-port
  if ( ioctl(serialFileDescriptor, TIOCEXCL) == -1) {
    *error = "Error: couldn't obtain lock on serial port";
    NSLog(/*@*/"%s", *error);
    return -1;
  }
  if ( fcntl(serialFileDescriptor, F_SETFL, 0) == -1) { 
    // clear the O_NONBLOCK flag; all calls from here on out are blocking for non-root processes
    *error = "Error: couldn't obtain lock on serial port";
    NSLog(/*@*/"%s", *error);
    return -1;
  }
  // Get the current options and save them so we can restore the default settings later.
  if ( tcgetattr(serialFileDescriptor, &gOriginalTTYAttrs) == -1) { 
    *error = "Error: couldn't get serial attributes";
    NSLog(/*@*/"%s", *error);
    return -1;
  }
  // copy the old termios settings into the current
  //   you want to do this so that you get all the control characters assigned
  struct termios options = gOriginalTTYAttrs;
  /*
     cfmakeraw(&options) is equivilent to:
     options->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
     options->c_oflag &= ~OPOST;
     options->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
     options->c_cflag &= ~(CSIZE | PARENB);
     options->c_cflag |= CS8;
     */
  cfmakeraw(&options);

  // set tty attributes (raw-mode in this case)
  if ( tcsetattr(serialFileDescriptor, TCSANOW, &options) == -1) {
    *error = "Error: couldn't get serial attributes";
    NSLog(/*@*/"%s", *error);
    return -1;
  }
  // Set baud rate (any arbitrary baud rate can be set this way)
  /***********************************************/
  //if ( ioctl(serialFileDescriptor, IOSSIOSPEED, &baudRate) == -1) { 
  //    *error = "Error: Baud Rate out of bounds";
  //    NSLog(/*@*/"%s", *error);
  //    return -1;
  //}
  // by KK
  if( cfsetspeed(&options, baudRate) == -1 ) {
    NSLog("Error: Baud Rate out of bounds","");
    return -1;
  }
  /***********************************************/
  // Set the receive latency (a.k.a. don't wait to buffer data)
  //if ( ioctl(serialFileDescriptor, IOSSDATALAT, &mics) == -1) { 
  //    *error = "Error: coudln't set serial latency";
  //    NSLog(/*@*/"%s", *error);
  //    return -1;
  //}

  //wait until Arduino finish the setup
  char buffer[BUFFER_SIZE];
  memset(buffer , '\0' , sizeof(buffer));
  response(buffer);

  return 0;
}