예제 #1
0
파일: serpardr.c 프로젝트: kjseefried/pm3
static int SerparOpen(const char *name, const char *arg)
{
    char *sername = NULL;
    char *parname = NULL;

#ifdef DEBUG
    printf("SerparOpen: name %s arg %s\n", name, arg ? arg : "<NULL>");
#endif

#ifdef COMPILING_ON_WINDOWS
    if (IsOpenSerial() || IsOpenParallel()) return -1;
#else
    if (Unix_IsSerialInUse() || Unix_IsParallelInUse()) return -1;
#endif

#ifdef COMPILING_ON_WINDOWS
    if (SerparMatch(name, arg) == -1)
        return -1;
#else
    Unix_IsValidParallelDevice(name,&sername,&parname);
# ifdef DEBUG
    printf("translated %s to serial %s and parallel %s\n",
           name==0 ? "NULL" : name,
           sername==0 ? "NULL" : sername,
           parname==0 ? "NULL" : parname);
# endif
    if (sername==NULL || parname==NULL) return -1;
#endif

    user_options_set = FALSE;

    /* interpret and store the arguments */
    if (arg != NULL)
    {
        unsigned int target_baud_rate;

        target_baud_rate = (unsigned int)strtoul(arg, NULL, 10);

        if (target_baud_rate > 0)
        {
#ifdef DEBUG
            printf("user selected baud rate %u\n", target_baud_rate);
#endif
            process_baud_rate(target_baud_rate);
        }
#ifdef DEBUG
        else
            printf("could not understand baud rate %s\n", arg);
#endif
    }

#ifdef COMPILING_ON_WINDOWS
    {
        /*
         * The serial port number is in name[0] followed by
         * the parallel port number in name[1]
         */

        int sport = name[0] - '0';
        int pport = name[1] - '0';

        if (OpenParallel(pport) != COM_OK)
            return -1;

        if (OpenSerial(sport, FALSE) != COM_OK)
        {
            CloseParallel();
            return -1;
        }
    }
#else
    Unix_OpenParallel(parname);
    Unix_OpenSerial(sername);
#endif

    serpar_reset();

#if defined(__unix) || defined(__CYGWIN32__)
    Unix_ioctlNonBlocking();
#endif

    Angel_RxEngineInit(&config, &rxstate);

    return 0;
}
static int SerialOpen(const char *name, const char *arg)
{
    const char *port_name = name;

#ifdef DEBUG
    printf("SerialOpen: name %s arg %s\n", name, arg ? arg : "<NULL>");
#endif

#ifdef COMPILING_ON_WINDOWS
    if (IsOpenSerial()) return -1;
#else
    if (Unix_IsSerialInUse()) return -1;
#endif

#ifdef COMPILING_ON_WINDOWS
    if (SerialMatch(name, arg) != adp_ok)
        return adp_failed;
#else
    port_name = Unix_MatchValidSerialDevice(port_name);
# ifdef DEBUG
    printf("translated port to %s\n", port_name == 0 ? "NULL" : port_name);
# endif
    if (port_name == 0) return adp_failed;
#endif

    user_options_set = FALSE;

    /* interpret and store the arguments */
    if ( arg != NULL )
    {
        unsigned int target_baud_rate;
        target_baud_rate = (unsigned int)strtoul(arg, NULL, 10);
        if (target_baud_rate > 0)
        {
#ifdef DEBUG
            printf( "user selected baud rate %u\n", target_baud_rate );
#endif
            process_baud_rate( target_baud_rate );
        }
#ifdef DEBUG
        else
           printf( "could not understand baud rate %s\n", arg );
#endif
    }
    else if (baud_rate > 0)
    {
      /* If the user specified a baud rate on the command line "-b" or via
         the "set remotebaud" command then try to use that one */
      process_baud_rate( baud_rate );
    }

#ifdef COMPILING_ON_WINDOWS
    {
        int port = IsValidDevice(name);
        if (OpenSerial(port, FALSE) != COM_OK)
            return -1;
    }
#else
    if (Unix_OpenSerial(port_name) < 0)
      return -1;
#endif

    serial_reset();

#if defined(__unix) || defined(__CYGWIN__)
    Unix_ioctlNonBlocking();
#endif

    Angel_RxEngineInit(&config, &rxstate);
    /*
     * DANGER!: passing in NULL as the packet is ok for now as it is just
     * IGNOREd but this may well change
     */
    Angel_TxEngineInit(&config, NULL, &wstate.txstate); 
    return 0;
}