int main(int argc, char** argv) { mraa_uart_context uart; uart = mraa_uart_init(0); mraa_uart_set_baudrate(uart, 9600); if (uart == NULL) { fprintf(stderr, "UART failed to setup\n"); printf("UART fialed"); return EXIT_FAILURE; } char buffer[] = "Hello Mraa!"; // mraa_uart_write(uart, buffer, sizeof(buffer)); printf("buffer: "); char* current; while(1){ printf("%c",buffer[1]); mraa_uart_read(uart, buffer, 9); printf("buff:%c %c %c %c %c %c %c %c %c\n",buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8] ); sleep(1); } // mraa_uart_stop(uart); mraa_deinit(); return EXIT_SUCCESS; }
mraa_uart_context uart_setup() { mraa_uart_context uart0 = mraa_uart_init(0); mraa_uart_set_baudrate(uart0, 9600); mraa_uart_set_mode(uart0, 8, MRAA_UART_PARITY_NONE, 1); return uart0; }
/** * Uart Constructor, takes a pin number which will map directly to the * linux uart number, this 'enables' the uart, nothing more * * @param uart the index of the uart set to use */ Uart(int uart) { m_uart = mraa_uart_init(uart); if (m_uart == NULL) { throw std::invalid_argument("Error initialising UART"); } }
int main(int argc, char** argv) { mraa_uart_context uart; uart = mraa_uart_init(0); if (uart == NULL) { fprintf(stdout, "UART failed to setup\n"); } mraa_deinit(); return 0; }
int main() { printf("Arbotox Commander XBee Test!\n"); // Install signal handler to allow us to do some cleanup... struct sigaction sigIntHandler; sigIntHandler.sa_handler = SignalHandler; sigemptyset(&sigIntHandler.sa_mask); sigIntHandler.sa_flags = 0; sigaction(SIGINT, &sigIntHandler, NULL); mraa_uart_context uart; mraa_init(); uart = mraa_uart_init(0); if (uart == NULL) { printf("MRAA UART failed to setup\n"); } // Lets try to open the XBee device... command.begin(szDevice, B38400); printf("After Begin!\n"); // loop simply echo what we receive from xbee to terminal // Now lets try to get data from the commander. while (fRunning) { if (command.ReadMsgs()) { // We have data. see if anything has changed before if ((command.rightV != rightV) || (command.rightH != rightH) || (command.leftV != leftV) || (command.leftH != leftH) || (command.buttons != buttons) || (command.ext != ext)) { // Something changed so print it out rightV = command.rightV; rightH = command.rightH; leftV = command.leftV; leftH = command.leftH; buttons = command.buttons; ext = command.ext; printf("%x %x - %d %d %d %d\n", buttons, ext, rightV, rightH, leftV, leftH); } } usleep(100); } return 0; }
// uart init ecezo_context ecezo_uart_init(unsigned int uart, unsigned int baudrate) { // make sure MRAA is initialized int mraa_rv; if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) { printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv); return NULL; } ecezo_context dev = (ecezo_context)malloc(sizeof(struct _ecezo_context)); if (!dev) return NULL; // zero out context memset((void *)dev, 0, sizeof(struct _ecezo_context)); // initialize the MRAA contexts // uart, default should be 8N1 if (!(dev->uart = mraa_uart_init(uart))) { printf("%s: mraa_uart_init() failed.\n", __FUNCTION__); ecezo_close(dev); return NULL; } if (mraa_uart_set_baudrate(dev->uart, baudrate)) { printf("%s: mraa_uart_set_baudrate() failed.\n", __FUNCTION__); ecezo_close(dev); return NULL; } mraa_uart_set_flowcontrol(dev->uart, false, false); if (generic_init(dev)) { printf("%s: generic_init() failed.\n", __FUNCTION__); ecezo_close(dev); return NULL; } return dev; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////// Modem related functions ////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int init_modem(){ mraa_init(); if( (uart = mraa_uart_init(0)) == NULL ){ writeToFile(src, "error INITIATING UART\n"); exit(UART_ERROR); } /*set uart boud rate [bps]*/ if ( mraa_uart_set_baudrate(uart, UART_BOUD_RATE)!=MRAA_SUCCESS) { writeToFile(src, "error seting baudrate\n"); exit(UART_ERROR); } if ( mraa_uart_set_mode(uart, 8,MRAA_UART_PARITY_NONE , 1)!=MRAA_SUCCESS) { writeToFile(src, "error seting mode\n"); } mraa_uart_set_flowcontrol(uart, 0, 0); writeToFile(src, "init modem\n"); }
// uart init rn2903_context rn2903_init(unsigned int uart, unsigned int baudrate) { rn2903_context dev; if (!(dev = _rn2903_preinit())) return NULL; // initialize the MRAA context // uart, default should be 8N1 if (!(dev->uart = mraa_uart_init(uart))) { printf("%s: mraa_uart_init() failed.\n", __FUNCTION__); rn2903_close(dev); return NULL; } return _rn2903_postinit(dev, baudrate); }
void Modem_SIM800::Init() { _enabled = true; this->_serial = mraa_uart_init(0); mraa_uart_set_baudrate(this->_serial, 9600); // TODO: Error handling while(mraa_uart_data_available(this->_serial, 0)) {this->Read();} // clear anything the modem sent on startup // get the auto-bauder started this->WriteLine("ATE0"); this->Read(); // disable echo of commands if (mraa_uart_data_available(this->_serial, 100)) { this->Read(); // read extra echo } this->WriteLine("AT"); this->Read(); this->WriteLine("AT"); this->Read(); this->WriteLine("AT"); this->Read(); while(mraa_uart_data_available(this->_serial, 100)) { char deadBuf[2] = {0}; mraa_uart_read(this->_serial, deadBuf, 1); } // clear responses if(!this->CheckReply("ATE0", "OK")) { // TODO: error handle } Logging::LogMessage(this->GetOperator()); }
mrb_value mrb_mraa_uart_init(mrb_state *mrb, mrb_value self) { mrb_int dev, baud, nargs; mraa_uart_context uart; nargs = mrb_get_args(mrb, "i|i", &dev, &baud); if (nargs < 2) baud = UART_DEFAULT_BAUDRATE; uart = mraa_uart_init(dev); if (uart == NULL) { mrb_raisef(mrb, E_RUNTIME_ERROR, "Failed to initialize DEV:%S.", mrb_fixnum_value(dev)); } mrb_data_init(self, uart, &mrb_mraa_uart_ctx_type); // DATA_TYPE(self) = &mrb_mraa_uart_ctx_type; // DATA_PTR(self) = uart; IV_SET("@read_bufsize", mrb_fixnum_value(UART_DEFAULT_BUFSIZE)); IV_SET("@timeout", mrb_fixnum_value(UART_DEFAULT_TIMEOUT)); IV_SET("@prompt", mrb_str_new_cstr(mrb, UART_DEFAULT_PROMPT)); mrb_funcall(mrb, self, "baudrate=", 1, mrb_fixnum_value(baud)); return self; }
/* 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; }
int main(){ /**********************************************************************/ /**********************************************************************/ /* Begin. */ mraa_uart_context uart; uart = mraa_uart_init(0); mraa_uart_set_baudrate(uart, 115200); char buffer[] = "hhh"; char flush[]="xxxxxxxxxxxx"; char str[] = "HELLO"; if (uart == NULL) { fprintf(stderr, "UART failed to setup\n"); printf("UART failed"); return 1; } printf("firstavail:%d\n",mraa_uart_data_available(uart,0)); while (mraa_uart_data_available(uart,0)) { mraa_uart_read(uart, flush, sizeof(uart)); printf("Flush: %c %c %c %c %c %c %c %c",flush[0], flush[1], flush[2],flush[3],flush[4],flush[5],flush[6],flush[7]); usleep(150); } printf("available: %d",mraa_uart_data_available(uart,0)); char speed_user_input[MAXBUFSIZ]; char turn_user_input[MAXBUFSIZ]; mraa_pwm_context speed_pwm_in1, speed_pwm_in2, turn_pwm; speed_pwm_in1 = mraa_pwm_init(3); speed_pwm_in2 = mraa_pwm_init(5); turn_pwm = mraa_pwm_init(6); if (speed_pwm_in1 == NULL || speed_pwm_in2 == NULL || turn_pwm == NULL) { fprintf(stderr, "Failed to initialized.\n"); return 1; } mraa_pwm_period_us(speed_pwm_in1,870); //1150Hz mraa_pwm_enable(speed_pwm_in1, 1); mraa_pwm_period_us(speed_pwm_in2,870); mraa_pwm_enable(speed_pwm_in2, 1); mraa_pwm_period_ms(turn_pwm,20); mraa_pwm_enable(turn_pwm, 1); mraa_pwm_write(turn_pwm, CENTER); mraa_pwm_write(speed_pwm_in1, 1.0f); mraa_pwm_write(speed_pwm_in2, 1.0f); int n = 5; int spd = 0; char direction = 'C'; char cross_clr = 'B'; float angle = 0.0f; while (1) { //sleep(2); //readCharAry(uart,flush); spd = 50; mraa_uart_read(uart, buffer, 1); if(buffer[0]=='\n') { printf("new line "); mraa_uart_read(uart, buffer, 1); if(buffer[0]=='\n') { printf("new line "); mraa_uart_read(uart, buffer, 1); } } mraa_uart_read(uart, buffer+1, 1); if(buffer[1]=='\n') { buffer[0] = 'h'; buffer[1] = 'h'; } int sign = 0; if(cross_clr == 'M') { speed_control(speed_pwm_in1, speed_pwm_in2, 0.0f); sleep(1); char* nearestBeaconID = (char*)malloc(5); getStrongestBeacon(nearestBeaconID); printf("the nearestBeaconID is: %s\n", nearestBeaconID); sign = 1; angle = CENTER + 0.015f; mraa_pwm_write(turn_pwm, angle); usleep(15000); speed_control(speed_pwm_in1, speed_pwm_in2, 54); printf("speed: %d",spd); usleep(5500000); speed_control(speed_pwm_in1, speed_pwm_in2, 0.0f); //sleep(1); mraa_uart_write(uart, str, 1); //while(!mraa_uart_data_available(uart, 10)){}; mraa_uart_read(uart, buffer, 1); while(buffer[0] == '\n') { mraa_uart_read(uart, buffer, 1); } if(buffer[0] == '\0') direction = 'C'; mraa_uart_read(uart, buffer + 1, 1); cross_clr = buffer[1]; } printf("buff:%c %c %c \n",buffer[0], buffer[1],buffer[2]); if(!sign){ if (direction == 'L') angle = CENTER - 0.005f; if (direction == 'R') angle = CENTER + 0.005f; if (direction == 'C') angle = CENTER;} else { if (direction == 'C') angle = CENTER +0.013f; if (direction == 'L') angle = CENTER +0.005f; if (direction == 'R') angle = CENTER + 0.019f; } mraa_pwm_write(turn_pwm, angle); speed_control(speed_pwm_in1, speed_pwm_in2, spd); printf("speed: %d",spd); usleep(250000); direction = buffer[0]; cross_clr = buffer[1]; } return 0; }
// 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 /**/ }
void serial_init(void) {//mraa_uart_context uart; uart = mraa_uart_init(0); }