int dwgpsnmea_init (struct misc_config_s *pconfig, int debug) { //dwgps_info_t info; #if __WIN32__ HANDLE read_gps_th; #else pthread_t read_gps_tid; int e; #endif s_debug = debug; if (s_debug >= 2) { text_color_set(DW_COLOR_DEBUG); dw_printf ("dwgpsnmea_init()\n"); } if (strlen(pconfig->gpsnmea_port) == 0) { /* Nothing to do. Leave initial fix value for not init. */ return (0); } /* * Open serial port connection. * 4800 baud is standard for GPS. * Should add an option to allow changing someday. */ s_gpsnmea_port_fd = serial_port_open (pconfig->gpsnmea_port, 4800); if (s_gpsnmea_port_fd != MYFDERROR) { #if __WIN32__ read_gps_th = (HANDLE)_beginthreadex (NULL, 0, read_gpsnmea_thread, (void*)(long)s_gpsnmea_port_fd, 0, NULL); if (read_gps_th == NULL) { text_color_set(DW_COLOR_ERROR); dw_printf ("Could not create GPS NMEA listening thread.\n"); return (-1); } #else int e; e = pthread_create (&read_gps_tid, NULL, read_gpsnmea_thread, (void*)(long)s_gpsnmea_port_fd); if (e != 0) { text_color_set(DW_COLOR_ERROR); perror("Could not create GPS NMEA listening thread."); return (-1); } #endif } else { text_color_set(DW_COLOR_ERROR); dw_printf ("Could not open serial port %s for GPS receiver.\n", pconfig->gpsnmea_port); return (-1); } /* success */ return (1); } /* end dwgpsnmea_init */
int main (int argc, char *argv[]) { struct timespec ts_curr; signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); int fd= 0; fd = serial_port_open(device_tty); if (fd<0) { printf("cannot open %s\n",device_tty); return 0; } else { printf("%s is open with the file descriptor = %d\n",device_tty,fd); } unsigned char * buf = malloc(160); memset(buf, 0, 160); size_t len = 138, r=0; while (start_sampling(fd)<0) { printf("?"); } volatile int s_idx=0; long long last_time, delta_time, curr_time; while (!stop_main) { r = read(fd, buf, len); if(r>0) { if(buf[0] == (unsigned char)0x5) { // do not print the same index twice if (s_idx != get_unsigned_short(buf[4],buf[5])) { // update curr_time on new Frame clock_gettime(CLOCK_REALTIME, &ts_curr); curr_time = ts_curr.tv_sec * 1000 + ts_curr.tv_nsec / 1000000; // update the last printed index s_idx = get_unsigned_short(buf[4],buf[5]); print_data(buf,r, curr_time); delta_time = curr_time - last_time; last_time = curr_time; } else { // TODO warn that a frame has the last frame's index // printf("warning: last_idx: %d \tcurr_idx: %d\n",s_idx, get_unsigned_short(buf[4],buf[5])); } } else { print_received_buffer(buf,r); } } //sleep(1); } while(stop_sampling(fd)<0) { printf("!"); } if(fd>=0) { printf("closing the file descriptor for %s\n",device_tty); close(fd); } free(buf); return 0; }
void serialPortStart(void){ //enable system to write and read serial port (/dev/ttyO2) serial_port = serial_port_open(); #if 1 //Create a thread that handle received commands. pthread_create(&message_handle_thread, NULL, (void * (*)(void *))serialMessageTask, (void*)NULL); #endif if (serial_port != -1) { // Assign a handler to close the serial port on Ctrl+C. signal (SIGINT, (void*)sigint_handler); //serial_port_write("USB virtual serial port test program\r\n",serial_port); //serial_port_write("To control the usr0 LED, type led1on or led1off and press <Enter>\r\n",serial_port); //serial_port_write("To end the remote application, type closeapp and press <Enter> \r\n",serial_port); //printf("Waiting to receive commands...\n"); printf("Press Ctrl+C to exit the program.\n"); } }
int main(void) { // this example assumes that a EDL21 meter sending SML messages via a // serial device. Adjust as needed. char *device = "/dev/cu.usbserial"; int fd = serial_port_open(device); if (fd > 0) { // listen on the serial device, this call is blocking. sml_transport_listen(fd, &transport_receiver); close(fd); } return 0; }
int main( int argc, char *argv[] ) { int ret = EXIT_FAILURE; serial_port_t port; struct model *model = malloc(sizeof(struct model)); struct activity *activity = malloc(sizeof(struct activity)); model->num_theta_region = CONF_NUM_THETA_REGION; char chosen_username[NAME_LEN]; char chosen_activity[NAME_LEN]; if (prompt_activity_name(chosen_activity)) goto ERROR; if (prompt_user_name(chosen_username)) goto ERROR; strncpy(activity->name, chosen_activity, NAME_LEN); strncpy(activity->user, chosen_username, NAME_LEN); activity->name[strlen(chosen_activity)] = '\0'; activity->user[strlen(chosen_username)] = '\0'; if (init_model(model)) goto ERROR; if (init_activity(activity, model)) goto ERROR; if (check_file_exists(FILE_CONF_CALIBRATION)) goto ERROR; if (serial_port_open(CONF_SERIAL_PORT, &port)) goto ERROR; if (serial_port_configure(port)) goto ERROR; if (real_time_listen(port, activity, model)) goto ERROR; file_write_activity_code(activity->files[FILE_ACTIVITY_CODE], activity, model); ret = EXIT_SUCCESS; ERROR: serial_port_close(port); free_activity(activity); free_model(model); return ret; }
int at_serial_port_execute_command(serial_port_t *port, at_serial_port_command_t *at_command) { int ret = 0; serial_port_open(port); if (!serial_port_is_open(port)) { piksi_log(LOG_ERR, "Unable to open port: %s", port->port_name); return -1; } int n = (int)write(port->fd, at_command->command, strlen(at_command->command)); n += (int)write(port->fd, "\r", 1); if (n != (int)strlen(at_command->command) + 1) { piksi_log(LOG_ERR, "Unable to write command '%s' to port '%s'", at_command->command, port->port_name); ret = -1; goto cleanup; } at_serial_port_wait_command_response(port, at_command); if (!at_command->complete) { piksi_log(LOG_ERR, "Unable to resolve result of command '%s' to port '%s'. %d characters returned", at_command->command, port->port_name, at_command->result_length); ret = -1; goto cleanup; } cleanup: serial_port_close(port); return ret; }
static bool choose_serial_port(struct serial_port *sport, const char *portname) { struct string_set *serial_ports; struct print_context pc; struct pick_context pk; char *line, *end; size_t len; pc.pc_counter = 0; pc.pc_handle = stdout; if (portname != NULL) { serial_ports = NULL; goto open_port; } serial_ports = serial_port_enumerate(); string_set_foreach(serial_ports, print_serial_port, &pc); if (pc.pc_counter == 0) { fprintf(pc.pc_handle, "Sorry, no ports available.\n"); string_set_free(serial_ports); return (false); } pk.pc_counter = 0; pk.pc_name = NULL; pk.pc_pick = 0; for (;;) { fprintf(pc.pc_handle, "Please enter the number corresponding with the port you would like to use.\n"); line = fgetln(stdin, &len); if (line == NULL) continue; line[len - 1] = '\0'; if (line[0] == '\0') continue; pk.pc_pick = strtoul(line, &end, 10); if (*end != '\0') { pk.pc_pick = 0; continue; } if (pk.pc_pick < 1 || pk.pc_pick > pc.pc_counter) { pk.pc_pick = 0; continue; } break; } string_set_foreach(serial_ports, pick_serial_port, &pk); if (pk.pc_name == NULL) { string_set_free(serial_ports); return (false); } portname = pk.pc_name; open_port: fprintf(pc.pc_handle, "Serial port selected: %s\n", portname); if (!serial_port_open(sport, portname)) { if (serial_ports != NULL) string_set_free(serial_ports); return (false); } fprintf(pc.pc_handle, "Serial port opened successfully.\n"); if (serial_ports != NULL) string_set_free(serial_ports); return (true); }