コード例 #1
0
int main(int argc, char *argv[]){

  char *temp;// = "/dev/ttyMI1";
  //int baud_rate;// = 9600;

  FILE *cfPtr;
  int i, max_cmd, error_query = 0;
  struct timeval timeout = {3, 0};//3 seconds
  int retval;
  fd_set rd_serial_data_fds;

  char buffer[BUFFER], file_name[50], msgs[MAX_MSGS][BUFFER];
  int len = 0, fd = -1;

  temp = getenv("DEVICE");
  if(temp != NULL){
    parse_device(temp);
  }

#if 0
  temp = getenv("BAUD_RATE");
  if(temp != NULL){
    baud_rate = atoi(temp);
  } else {        
    baud_rate = DEFAULT_BAUD;
  }
  for(i=0; baud_rate != speed_table[i].x_speed && speed_table[i].x_speed > 0; i++){}
  if(speed_table[i].x_speed == 0){
    printf("Read baud rate = %d using getenv() is invalid, default is used\n", baud_rate);
    baud_rate = DEFAULT_BAUD;
  }

  if((fd = start_serial(device, baud_rate)) < 0){
    printf("Application failed while opening or setting the attributes of a serial device: device = %s, baud_rate = %d\n", device, baud_rate);
    close(fd);
    return 0;
  }
#endif

  memset(buffer, 0x00, BUFFER); max_cmd = 0;
  if(argc == 1){
    printf("\nTo get started enter -h as an argument to display help commands: i.e. %s -h (then press enter)\n", argv[0]);
    strcpy(msgs[0], "*IDN?");//if no arguments then requet device id
    max_cmd = 1;
  }else if(!strcmp(argv[1], "help") || !strcmp(argv[1], "-h")){
    print_help(argv[0]);
    return 0;

  } else if (!strcmp(argv[1], "config")){
    if(argv[2] != NULL){
      strcpy(file_name, argv[2]);
    } else {
      printf("The configuration file name is not specified\n");
      return 0;
    }
    if((cfPtr = fopen(file_name, "r")) == NULL){
      fprintf(stderr, "error: config file fopen(%s, ): %s\n", file_name, strerror(errno));
      return -1;
    }
    printf("Reading commands from the configuration file defined as %s\n", file_name);
    while(fgets(buffer, BUFFER, cfPtr) != NULL){
      //printf("String read: %s\n",buffer);
      if(buffer[0] != '#'){//not a comment
        if(max_cmd == MAX_MSGS){
          printf("The number of commands read are greater than the defined possible No. = %d\n", MAX_MSGS);
          break;
        }
        strcpy(msgs[max_cmd], buffer);//, strlen(buffer)-1);
        msgs[max_cmd][strlen(buffer)-1] = '\0';//remove '\n'
        max_cmd++;
      }
      memset(buffer, 0x00, strlen(buffer));
    }

    fclose(cfPtr);//finished reading from a file
  }else if(argv[1][0] == '-'){
    //printf("reading char commands ...\n");
    max_cmd = read_char_commands(argc, argv, msgs);
    //printf("Finished reading, max_cmd = %d\n", max_cmd);
  }else {
    //printf("reading full commands ...\n");
    max_cmd = read_full_commands(argc, argv, msgs);
    //printf("Finished reading, max_cmd = %d\n", max_cmd);
  }

  for(i = 0; (baud_rate > speed_table[i].x_speed) && (speed_table[i].x_speed != 0); i++);

  if(speed_table[i].x_speed == 0){
    printf("baud rate = %d is invalid, the default is used\n", baud_rate);
    baud_rate = DEFAULT_BAUD;
  } else if (baud_rate != speed_table[i].x_speed){
    printf("warning: approximating requested speed of %d to %d\n", baud_rate, speed_table[i].x_speed);
    baud_rate = speed_table[i].x_speed;
  }

#if 0
  printf("\ndevice is %s, and the baud_rate is %d\n\n", device, baud_rate);
  for(i=0; i < max_cmd; i++){printf("command: msgs[%d] = %s\n", i, msgs[i]);}
  return 0;
#endif

  if((fd = start_serial(device, baud_rate)) < 0){
    printf("Failed during opening and setting the attributes of a serial device: \
        device = %s, baud_rate = %d\n", device, baud_rate);
    close(fd);
    //free_msgs_buffer(msgs);
    return -1;
  }
コード例 #2
0
ファイル: pk2serial.c プロジェクト: rthille/pk2serial-1
int main(int argc, char **argv) {
    struct usb_bus *busses;

    int c;
    opterr = 0;
    while ((c = getopt(argc, argv, "rptevhb:l:")) != -1) {
        switch(c) {
        case 'h': usage(); exit(0);
        case 'p': power_target = 1; break;
        case 'r': reset_target = 1; break;
        case 'v': verbose++; break;
        case 'e': exit_on_eof = 1; break;
        case 't': pty_mode = 1; break;
        case 'b': 
            baud_rate = strtol(optarg, NULL, 10);
            LIMIT(baud_rate, <, 10);     // arbitrary limit at 1 byte / second.
            LIMIT(baud_rate, >, 60000);  // tested with loopback
            break;

        case 'l': 
            latency_bytes = strtol(optarg, NULL, 10);
            LIMIT(latency_bytes, <, 1);
            LIMIT(latency_bytes, >, 60);
            break;
        case '?':
            if ((optopt == 'b') || (optopt == 'l'))
                fprintf (stderr, "Option -%c requires an argument.\r\n", 
                         optopt);
            else if (isprint (optopt))
                fprintf (stderr, "Unknown option `-%c'.\r\n", optopt);
            else
                fprintf (stderr,
                         "Unknown option character `\\x%x'.\r\n",
                         optopt);
        default:
            usage_exit();
        }
    }

    LOG("baud rate = %d\r\n", baud_rate);

    
    signal(SIGINT, handler);

    usb_init();
    usb_find_busses();
    usb_find_devices();
    
    busses = usb_get_busses();

    struct usb_bus *bus;
    
    for (bus = busses; bus; bus = bus->next) {
        struct usb_device *dev;
        
        for (dev = bus->devices; dev; dev = dev->next) {

            /* Find first PK2 */
            if ((dev->descriptor.idVendor  == vendor)
                && (dev->descriptor.idProduct == product)) {

                if (!(handle = usb_open(dev))) {
                    RAISE_ERROR("Can't open device.\r\n");
                }
                /* FIXME: Detach if necessary 
                   usb_get_driver_np
                   usb_detach_kernel_driver_np
                 */
                if (-1 == usb_set_configuration(handle, 2)) { // Try vendor config (not HID!)
                    RAISE_ERROR("Can't set vendor config.\r\n");
                }
                if (-1 == usb_claim_interface(handle, 0)) {
                    RAISE_ERROR("Can't claim interface.\r\n");
                }
                int pty_master_fd;
                int pty_slave_fd;
                if (pty_mode == 1) {
                    char dev[100];
                    if (openpty(&pty_master_fd, &pty_slave_fd, dev, NULL, NULL) == -1) {
                        RAISE_ERROR("Cannot allocate PTY.\r\n");
                    }
                    input_fd = pty_master_fd;
                    output_fd = pty_master_fd;
                    fprintf(stderr, "Opened PTY on %s\r\n", dev);
                }
                start_serial();
                if (pty_mode == 1) {
                    close(pty_master_fd);
                    close(pty_slave_fd);
                }
                
                if (-1 == usb_release_interface(handle, 0)) {
                    RAISE_ERROR("Can't release interface.\r\n");
                }
                if (-1 == usb_close(handle)) {
                    RAISE_ERROR("Can't close device.\r\n");
                }
            }
        }
    }
    return 0;
    
}