mrb_value mrb_mraa_uart_get_dev_path(mrb_state *mrb, mrb_value self) { mraa_uart_context uart; const char *path; uart = (mraa_uart_context)mrb_data_get_ptr(mrb, self, &mrb_mraa_uart_ctx_type); path = mraa_uart_get_dev_path(uart); return mrb_str_new_cstr(mrb, path); }
/* Initialize and open UART communication * Return uart handle if successful * baudrate is set to 115200 */ int uart_init(void) { mraa_uart_context uart_dev = NULL; char *uart_dev_path = NULL; if ((uart_dev = mraa_uart_init(0)) == NULL) { printf("ERROR : UART\t mraa uart init failed\n"); return -1; } if ((uart_dev_path = mraa_uart_get_dev_path(uart_dev)) == NULL) { printf("ERROR : UART\t mraa uart path does not exist"); return -1; } if((uart = open(uart_dev_path, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) { printf("ERROR : UART\t opening serial device"); return -1; } set_interface_attribs(uart, B115200, 0); set_blocking(uart, 0); printf("UART successfully initialized on %s\n", uart_dev_path); tcflush(uart, TCIOFLUSH); return 0; }
/** * Get string with tty device path within Linux * For example. Could point to "/dev/ttyS0" * * @return char pointer of device path */ std::string getDevicePath() { std::string ret_val(mraa_uart_get_dev_path(m_uart)); return ret_val; }
// Serial 1 port setup void setupSerial1( int baud ) { /* */ mraa_uart_context uart1 = mraa_uart_init( 0 ); char *devPort = mraa_uart_get_dev_path( uart1 ); // "/dev/ttyMFD1"; // mraa_uart_get_dev_path( uart1 ); printf( " setupSerial1 at start with dev path: %s\n", devPort ); // sets up uart1, connected to Arduino BB pins 0 & 1 serialFDOut = -1; // Say that it is not open... serialFDIn = -1; fPtrOut = NULL; fPtrIn = NULL; // Try to open File Descriptor // char devPort[] = "/dev/ttyMFD1"; printf( " Serial port prepare to open %s\n", devPort ); // int serialFDOut = open( devPort, O_RDWR| O_NONBLOCK | O_NDELAY ); int serialFDOut = open( devPort, O_RDWR| O_NONBLOCK | O_NOCTTY | O_SYNC ); // Error check if ( serialFDOut < 0 ) { printf( " Error opening %s: %d, %s\n", devPort, errno, strerror( errno ) ); return; } printf( " Serial port %s opened successfully\n", devPort ); if ( ( fPtrOut = fdopen( serialFDOut, "r+" ) ) == NULL ) { printf( " Error opening file associated with %s: %d, %s\n", devPort, errno, strerror( errno ) ); close( serialFDOut ); return; } printf( " Serial port access file %s opened successfully\n", devPort ); // For normal files _pfileIn will simply be _pfileOut likewise for file descriptors fPtrIn = fPtrOut; serialFDIn = serialFDOut; setvbuf( fPtrOut, NULL, _IONBF, BUFSIZ ); fflush( fPtrOut ); // Start port config struct termios tty; memset(&tty, 0, sizeof tty ); // Error Handling if ( tcgetattr ( serialFDOut, &tty ) != 0 ) { printf( " Error from tcgetattr: %d, %s\n", errno, strerror (errno) ); fclose( fPtrOut ); close( serialFDOut ); return; } speed_t inSpeed = cfgetispeed( &tty ); speed_t outSpeed = cfgetispeed( &tty ); printf( " Existing port speed, in: %d, out: %d\n", inSpeed, outSpeed ); // Setting other Port Stuff tty.c_cflag &= ~PARENB; // Make 8n1 tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CSIZE; tty.c_cflag &= ~CRTSCTS; // no flow control tty.c_cflag |= CS8 | HUPCL; // 8 bits, enable lower control lines on close - hang up tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines tty.c_lflag = 0; // no signaling chars, no echo, no canonical processing // tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw tty.c_oflag = 0; // no remapping, no delays // tty.c_oflag &= ~OPOST; // make raw tty.c_iflag = 0; // turn off s/w flow ctrl // tty.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl tty.c_cc[VMIN] = 0; // read doesn't block tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout // Set Baud Rate if ( 0 != baud ) { if ( cfsetspeed (&tty, baud ) != 0) { printf( " Error from cfsetspeed: %d, %s\n", errno, strerror (errno) ); return; } } // Flush Port, then applies attributes if ( tcflush( serialFDOut, TCIFLUSH ) != 0) { printf( " Error from tcflush: %d, %s\n", errno, strerror (errno) ); return; } if ( tcsetattr ( serialFDOut, TCSAFLUSH, &tty ) != 0) { printf( " Error from tcsetattr: %d, %s\n", errno, strerror (errno) ); return; } // enable input & output transmission if ( tcflow(serialFDOut, TCOON | TCION) != 0) { printf( " Error from tcflow: %d, %s\n", errno, strerror (errno) ); return; } // purge buffer { char buf[1024]; int n; do { usleep( 5000 ); // 5ms n = read( serialFDOut, buf, sizeof( buf ) ); } while ( n > 0 ); } fcntl( serialFDOut, F_SETFL, 0 ); // disable blocking /**/ }