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; }
upm_result_t rn2903_set_baudrate(const rn2903_context dev, unsigned int baudrate) { assert(dev != NULL); if (dev->debug) printf("%s: Setting baudrate to %d\n", __FUNCTION__, baudrate); if (mraa_uart_set_baudrate(dev->uart, baudrate)) { printf("%s: mraa_uart_set_baudrate() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } dev->baudrate = baudrate; if (!rn2903_autobaud(dev, RN2903_AUTOBAUD_RETRIES)) { printf("%s: rn2903_autobaud detection failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } return UPM_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; }
mraa_uart_context mraa_uart_init_raw(const char* path) { mraa_uart_context dev = mraa_uart_init_internal(plat == NULL ? NULL : plat->adv_func); if (dev == NULL) { syslog(LOG_ERR, "uart: Failed to allocate memory for context"); return NULL; } dev->path = path; if (!dev->path) { syslog(LOG_ERR, "uart: device path undefined, open failed"); free(dev); return NULL; } // now open the device if ((dev->fd = open(dev->path, O_RDWR)) == -1) { syslog(LOG_ERR, "uart: open() failed"); free(dev); return NULL; } // now setup the tty and the selected baud rate struct termios termio; // get current modes if (tcgetattr(dev->fd, &termio)) { syslog(LOG_ERR, "uart: tcgetattr() failed"); close(dev->fd); free(dev); return NULL; } // setup for a 'raw' mode. 8N1, no echo or special character // handling, such as flow control or line editing semantics. // cfmakeraw is not POSIX! cfmakeraw(&termio); if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) { syslog(LOG_ERR, "uart: tcsetattr() failed after cfmakeraw()"); close(dev->fd); free(dev); return NULL; } if (mraa_uart_set_baudrate(dev, 9600) != MRAA_SUCCESS) { close(dev->fd); free(dev); return NULL; } return dev; }
mrb_value mrb_mraa_uart_set_baudrate(mrb_state *mrb, mrb_value self) { mraa_uart_context uart; mraa_result_t result; mrb_int baud; mrb_get_args(mrb, "i", &baud); uart = (mraa_uart_context)mrb_data_get_ptr(mrb, self, &mrb_mraa_uart_ctx_type); result = mraa_uart_set_baudrate(uart, baud); if (result != MRAA_SUCCESS) { mrb_raise(mrb, E_RUNTIME_ERROR, "Could not set baudrate"); } IV_SET("@baudrate", mrb_fixnum_value(baud)); return self; }
int main(){ load_device_tree("ADAFRUIT-UART4"); mraa_uart_context bbb; bbb = mraa_uart_init_raw("/dev/ttyO4"); mraa_uart_set_baudrate(bbb, 38400); mraa_uart_set_mode(bbb, 8,MRAA_UART_PARITY_NONE , 1); char buf[31] = "~4121|p123.324321|n053.989876$"; buf[30] = '\0'; while (1) { mraa_uart_write(bbb, buf, 30); usleep(10000); } 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"); }
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()); }
/** * Set the baudrate. * Takes an int and will attempt to decide what baudrate is * to be used on the UART hardware. * * @param baud unsigned int of baudrate i.e. 9600 * @return Result of operation */ Result setBaudRate(unsigned int baud) { return (Result) mraa_uart_set_baudrate(m_uart, baud); }
/** * n_direction_flag: 0 from edison to beaglebone * 1 from beaglebone to edison * check https://github.com/peidong/drone/blob/master/Edison/main/edison-bbb-communication-code.md for commands */ int communication_with_beaglebone_uart(int nflag_direction, struct T_drone *pT_drone, int nflag_receive_success){ /** * check if uart available */ while (pT_drone->nflag_enable_uart != 1){ usleep(1300); } pT_drone->nflag_enable_uart = 0; mraa_uart_context beaglebone_uart; if (nflag_direction == 1){ /** * From beaglebone to edison */ beaglebone_uart = mraa_uart_init_raw("/dev/ttyO4"); mraa_uart_set_baudrate(beaglebone_uart, 38400); mraa_uart_set_mode(beaglebone_uart, 8, MRAA_UART_PARITY_NONE , 1); while (mraa_uart_data_available(beaglebone_uart, 10000) != 1){ printf("data not available\n"); /*usleep(10000);*/ } /** * Start receive */ char c_flag[1]; char arrc_buffer[20]; int nflag_find_beginning = 0; int nflag_find_end = 0; int n_index = 0; /** * Read the message array */ while (nflag_find_beginning != 1){ mraa_uart_read(beaglebone_uart, c_flag, 1); if (c_flag[0] == '~'){ nflag_find_beginning = 1; n_index = 0; while (nflag_find_end != 1){ mraa_uart_read(beaglebone_uart, arrc_buffer + n_index, 1); if (arrc_buffer[n_index] == '$'){ arrc_buffer[n_index] = '\0'; nflag_find_end = 1; //break; }else if (arrc_buffer[n_index] == '~'){ nflag_find_end = -1; nflag_find_beginning = 1; n_index = 0; //continue; }else{ n_index++; } } } } /** * Process the message */ int n_command_index = -1; if (arrc_buffer[0] == '0'){ /** * stop */ n_command_index = 0; printf("stop received\n"); }else if (arrc_buffer[0] == '1'){ /** * auto control */ char arrc_command_index[4]; int n_temp_index; for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){ arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index]; } arrc_command_index[3] = '\0'; n_command_index = atoi(arrc_command_index); printf("auto control received: %d\n", n_command_index); }else if (arrc_buffer[0] == '2'){ /** * manual control command */ char arrc_command_index[4]; int n_temp_index; for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){ arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index]; } arrc_command_index[3] = '\0'; n_command_index = atoi(arrc_command_index); printf("manual control received: %d\n", n_command_index); }else if (arrc_buffer[0] == '3'){ /** * pid tuning */ char arrc_command_index[4]; int n_temp_index; for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){ arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index]; } arrc_command_index[3] = '\0'; n_command_index = atoi(arrc_command_index); printf("pid tuning received: %d\n", n_command_index); char arrc_pid_value[9]; for (n_temp_index = 0; n_temp_index <= 7; n_temp_index++){ arrc_pid_value[n_temp_index] = arrc_buffer[n_temp_index + 3]; } arrc_buffer[8] = '\0'; if (n_command_index == 301){ pT_drone->d_kp_pitch = atof(arrc_pid_value); }else if (n_command_index == 302){ pT_drone->d_ki_pitch = atof(arrc_pid_value); }else if (n_command_index == 303){ pT_drone->d_kd_pitch = atof(arrc_pid_value); }else if (n_command_index == 304){ pT_drone->d_kp_roll = atof(arrc_pid_value); }else if (n_command_index == 305){ pT_drone->d_ki_roll = atof(arrc_pid_value); }else if (n_command_index == 306){ pT_drone->d_kd_roll = atof(arrc_pid_value); }else if (n_command_index == 307){ pT_drone->d_kp_yaw = atof(arrc_pid_value); }else if (n_command_index == 308){ pT_drone->d_kd_yaw = atof(arrc_pid_value); }else if (n_command_index == 309){ pT_drone->d_ki_yaw = atof(arrc_pid_value); }else if (n_command_index == 310){ pT_drone->d_kp_second_pitch = atof(arrc_pid_value); }else if (n_command_index == 311){ pT_drone->d_kd_second_pitch = atof(arrc_pid_value); }else if (n_command_index == 312){ pT_drone->d_kp_second_roll = atof(arrc_pid_value); }else if (n_command_index == 313){ pT_drone->d_kd_second_roll = atof(arrc_pid_value); }else if (n_command_index == 314){ pT_drone->d_kp_second_yaw = atof(arrc_pid_value); }else if (n_command_index == 315){ pT_drone->d_kd_second_yaw = atof(arrc_pid_value); } printf("pid tuning value: %lf\n", atof(arrc_pid_value)); }else if (arrc_buffer[0] == '4'){ /** * feedback */ printf("feedback received\n"); } }else if (nflag_direction == 0){ /** * From edison to beaglebone */ } pT_drone->nflag_enable_uart = 1; 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; }
int main() { int sampleCount = 0; int sampleRate = 0; uint64_t rateTimer; uint64_t displayTimer; uint64_t now; // Using RTIMULib here allows it to use the .ini file generated by RTIMULibDemo. // Or, you can create the .ini in some other directory by using: // RTIMUSettings *settings = new RTIMUSettings("<directory path>", "RTIMULib"); // where <directory path> is the path to where the .ini file is to be loaded/saved RTIMUSettings *settings = new RTIMUSettings("RTIMULib"); RTIMU *imu = RTIMU::createIMU(settings); if ((imu == NULL) || (imu->IMUType() == RTIMU_TYPE_NULL)) { printf("No IMU found\n"); exit(1); } // This is an opportunity to manually override any settings before the call IMUInit // set up IMU imu->IMUInit(); // this is a convenient place to change fusion parameters imu->setSlerpPower(0.02); imu->setGyroEnable(true); imu->setAccelEnable(true); imu->setCompassEnable(true); // Set up serial out UART ports mraa_uart_context uart; //uart = mraa_uart_init(0); uart=mraa_uart_init_raw("/dev/ttyGS0"); mraa_uart_set_baudrate(uart, 9600); if (uart == NULL) { fprintf(stderr, "UART failed to setup\n"); return 0; } char buffer[20]={}; // To hold output while (1) { while (imu->IMURead()) { RTIMU_DATA imuData = imu->getIMUData(); //sleep(1); printf("%s\r", RTMath::displayDegrees("Gyro", imuData.fusionPose) ); //printf("\nx %f ",imuData.gyro.x()*(180.0/3.14)); //printf("\ny %f ",imuData.gyro.y()*(180.0/3.14)); //printf("\nz %f\n",imuData.gyro.z()*(180.0/3.14)); sprintf(buffer,"%4f\n",imuData.fusionPose.x()*(180.0/3.14)); mraa_uart_write(uart, buffer, sizeof(buffer)); printf("\n\n"); //imuData. fflush(stdout); //displayTimer = now; } } mraa_uart_stop(uart); mraa_deinit(); }
mraa_uart_context mraa_uart_init_raw(const char* path) { mraa_result_t status = MRAA_SUCCESS; mraa_uart_context dev = NULL; if (!path) { syslog(LOG_ERR, "uart: device path undefined"); status = MRAA_ERROR_INVALID_PARAMETER; goto init_raw_cleanup; } dev = mraa_uart_init_internal(plat == NULL ? NULL : plat->adv_func); if (dev == NULL) { syslog(LOG_ERR, "uart: Failed to allocate memory for context"); status = MRAA_ERROR_NO_RESOURCES; goto init_raw_cleanup; } dev->path = (char*) calloc(strlen(path)+1, sizeof(char)); if (dev->path == NULL) { syslog(LOG_ERR, "uart: Failed to allocate memory for path"); status = MRAA_ERROR_NO_RESOURCES; goto init_raw_cleanup; } strncpy((char *) dev->path, path, strlen(path)); if (IS_FUNC_DEFINED(dev, uart_init_raw_replace)) { status = dev->advance_func->uart_init_raw_replace(dev, path); if (status == MRAA_SUCCESS) { return dev; } else { goto init_raw_cleanup; } } // now open the device if ((dev->fd = open(dev->path, O_RDWR)) == -1) { syslog(LOG_ERR, "uart: open(%s) failed: %s", path, strerror(errno)); status = MRAA_ERROR_INVALID_RESOURCE; goto init_raw_cleanup; } // now setup the tty and the selected baud rate struct termios termio; // get current modes if (tcgetattr(dev->fd, &termio)) { syslog(LOG_ERR, "uart: tcgetattr(%s) failed: %s", path, strerror(errno)); status = MRAA_ERROR_INVALID_RESOURCE; goto init_raw_cleanup; } // setup for a 'raw' mode. 8N1, no echo or special character // handling, such as flow control or line editing semantics. // cfmakeraw is not POSIX! cfmakeraw(&termio); if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) { syslog(LOG_ERR, "uart: tcsetattr(%s) failed after cfmakeraw(): %s", path, strerror(errno)); status = MRAA_ERROR_INVALID_RESOURCE; goto init_raw_cleanup; } if (mraa_uart_set_baudrate(dev, 9600) != MRAA_SUCCESS) { status = MRAA_ERROR_INVALID_RESOURCE; goto init_raw_cleanup; } init_raw_cleanup: if (status != MRAA_SUCCESS) { if (dev != NULL) { if (dev->fd >= 0) { close(dev->fd); } if (dev->path != NULL) { free((void *) dev->path); } free(dev); } return NULL; } return dev; }
mraa_result_t mraa_mock_uart_init_raw_replace(mraa_uart_context dev, const char* path) { // The only thing we have to do from the original uart_init_raw() return mraa_uart_set_baudrate(dev, 9600); }
int main() { mraa_uart_context gps; load_device_tree("ADAFRUIT-UART1"); gps = mraa_uart_init_raw("/dev/ttyO1"); mraa_uart_set_baudrate(gps, 9600); char buf[1000]; char search[7]; gprmc_t readGPS; // char *p = buf; while(1){//gprmc_t readGPS; int i=0; mraa_uart_read(gps, search, 1); if(search[0] == '$'){ for(i=1; i<7;i++){ mraa_uart_read(gps, search+i, 1); } if(strstr(search, "$GPRMC,")){ for(i=0; i<100;i++){ mraa_uart_read(gps, buf+i, 1); if(buf[i] == '\n'){ buf[i]='\0'; break; } } // printf("%s\n", buf); nmea_parse_gprmc(buf, &readGPS); gps_convert_deg_to_dec(&(readGPS.latitude), readGPS.lat, &(readGPS.longitude), readGPS.lon); printf("%d\n", readGPS.state); printf("%f\n", readGPS.latitude); printf("%f\n", readGPS.longitude); } // mraa_uart_read(gps, buf+1, 1); // mraa_uart_read(gps, buf+2, 1); // mraa_uart_read(gps, buf+3, 1); // mraa_uart_read(gps, buf+4, 1); // mraa_uart_read(gps, buf+5, 1); // mraa_uart_read(gps, buf+6, 1); // mraa_uart_read(gps, buf+7, 1); // mraa_uart_read(gps, buf+8, 1); // mraa_uart_read(gps, buf+9, 1); // mraa_uart_read(gps, buf+10, 1); // mraa_uart_read(gps, buf+11, 1); // mraa_uart_read(gps, buf+12, 1); // mraa_uart_read(gps, buf+13, 1); // mraa_uart_read(gps, buf+14, 1); // mraa_uart_read(gps, buf+15, 1); // mraa_uart_read(gps, buf+16, 1); // printf("%s\n",buf); // if(buf[0]=='$') // { // printf("%s\n", buf); //buf = strchr(buf, ',')+1; //printf("%s\n", buf); // } // if (strchr(buf, '$GPRMC')!=NULL){ // //buf = strchr(buf, ',')+1; // //printf("%s\n", buf); // } //buf = strchr(buf, ',')+1; //printf("%s\n", buf); //mraa_uart_read(gps, buf, 1); // mraa_uart_read(gps, buf+1, 1); // mraa_uart_read(gps, buf+2, 1); // mraa_uart_read(gps, buf+3, 1); // mraa_uart_read(gps, buf+4, 1); // mraa_uart_read(gps, buf+5, 1); // mraa_uart_read(gps, buf+6, 1); // mraa_uart_read(gps, buf+7, 1); // mraa_uart_read(gps, buf+8, 1); // mraa_uart_read(gps, buf+9, 1); // mraa_uart_read(gps, buf+10, 1); // mraa_uart_read(gps, buf+11, 1); // mraa_uart_read(gps, buf+12, 1); // mraa_uart_read(gps, buf+13, 1); // mraa_uart_read(gps, buf+14, 1); // mraa_uart_read(gps, buf+15, 1); // mraa_uart_read(gps, buf+16, 1); // mraa_uart_read(gps, buf+17, 1); //int i=0; //for(i = 0; i<100) // if(nmea_get_message_type(buf)==NMEA_GPRMC){ // nmea_parse_gprmc(buf, &readGPS); // printf("%d\n", readGPS.speed);} // } } } }