コード例 #1
0
ファイル: comport.c プロジェクト: rbaldwin7/rhocode
/* Open the specified serial port, save old communication parameters, and   *
 * set initial communication parameters (default parameters are appropriate *
 * for the Barrett Hand).                                                   *
 * Returns: E_OKAY   if successful.                                         *
 *          E_OPEN   if the port could not be opened.                       *
 *          E_INUSE  if the port is already open by someone else.           *
 *          E_SYSTEM if any of several system calls failed.                 */
int ComOpen(ComPort_t *Port , const char *DevName) {
      int                    Stat;

    /* If already open, close the old one first. */
    ComClose(Port);

    Stat = E_OKAY;

    // Allocate timer and set SIGALRM processing for timeout detection. 
  /*  if (   ( (Port->Tmr = timer_create(CLOCK_REALTIME, NULL, 0)) >= 0)
	   && (signal(SIGALRM, AlrmTrap) != SIG_ERR                  ) ) {
      Stat = E_OKAY;
    } else {
      Stat = E_SYSTEM;
    }*/
    //

    /* Open the specified serial port. */
    if ((Stat == E_OKAY) && ( (Port->Fd = open(DevName, O_RDWR | O_NOCTTY )) < 0)) {
       Stat = E_OPEN;
    } 
 
    /* Get device info and check open_count to see if device already in     *
     * use.                                                                 */
    /*    if ((Stat == E_OKAY) && (dev_info(Port->Fd, &DevInfo) < 0))
        Stat = E_SYSTEM;
   
    if ((Stat == E_OKAY) && (DevInfo.open_count != 1))    Stat = E_INUSE;
    */
    /* Save old communication parameters and set new ones. */
    if ((Stat == E_OKAY) && (tcgetattr(Port->Fd, &(Port->OldTios)) < 0)) {
      Stat = E_SYSTEM;
    }

    if (Stat != E_OKAY) {
        /* An error occurred above. */
        if (Port->Fd >= 0) {  /* If device was opened, just close it. */
            close(Port->Fd);
            Port->Fd = -1;
        }
	if (Port->Tmr >= 0) { // If timer was allocated, deallocate it. 
	  //timer_delete(Port->Tmr);
            Port->Tmr = (timer_t) -1;
        }
        signal(SIGALRM, SIG_DFL);
    } else {
        /* No errors so far, so set communication parameters.  If error,    *
         * (try to) restore original parameters and close the device.       */
        Stat = ComInit(Port);
        if (Stat != E_OKAY) ComClose(Port);
    }
    //printf("ComOpen ret: %d, Port->Fd=%d\n", Stat, Port->Fd);
    
    return(Stat);
}
コード例 #2
0
ファイル: ComAdjust.cpp プロジェクト: awzhang/MyWork
int CComAdjust::Release()
{
#if USE_COMBUS == 0
	ComClose();		// 串口关闭之后,读串口函数将返回错误,读线程退出
#endif

	pthread_mutex_destroy(&m_MutexWrite);

	return 0;
}
コード例 #3
0
ファイル: yx_IrdModKaiTian.cpp プロジェクト: awzhang/MyWork
int CKTIrdMod::Release()
{
	if( m_pthreadRecv )
	{
		m_bEscRecv = true;
		usleep(100000);
		
		if( WaitForEvent(ThreadExit, &m_pthreadRecv, 3000) )
		{
			pthread_cancel(m_pthreadRecv);
			usleep(100000);
		}

		m_pthreadRecv = 0;
	}
	m_bEscRecv = false;
	
	if( m_pthreadWork )
	{
		m_bEscWork = true;
		usleep(100000);
		
		if( WaitForEvent(ThreadExit, &m_pthreadWork, 3000) )
		{
			pthread_cancel(m_pthreadWork);
			usleep(100000);
		}

		m_pthreadWork = 0;
	}
	m_bEscWork = false;
		
	ComClose();

	return 0;
}
コード例 #4
0
ファイル: HSDEMO2.C プロジェクト: mcyril/ravel-ftn
int hslink_input(user_data_rec *udta,           /* current user data record */
                 int argc,                      /* argument count */
                 char *argv[])                  /* argument values */
{
        /* allocate application workspace if needed */
        if (udta->substt == 0)
        {
                udta->appws = (workspace_rec*)mem_alloc(sizeof(workspace_rec));
                if (udta->appws == 0)
                {
                        cprintf("Cannot allocate workspace for user %d!\r\n",usrnum);
                        return 0;
                }
        }

        /* current workspace to application workspace for this user */
        current_hsws = udta->appws;

        /* process current substate */
        switch (udta->substt)
        {
        case 0:
                if (top_init())
                {
                        udta->substt = 0;
                        break;
                }

                set_defaults();

                if (argc == 1)
                {
                        usage("No command line given\r\n","");
                        udta->substt = 0;
                        break;
                }

                if (process_options(argc,argv))
                {
                        usage("No command line given\r\n","");
                        udta->substt = 0;
                        break;
                }

                ComOpen();

                WS.Option.ComSpeed = ComGetSpeed();
                if (!WS.Option.EffSpeed)
                        WS.Option.EffSpeed = WS.Option.ComSpeed;

                /* we're now ready for SlowHandshake to work */
                WS.IoLevel = 0;

                /* allocate up to 10k for file buffers, but no more */
                WS.buffer_sizes = mem_avail()-1000>10240: 10240:mem_avail()-1000;

                /* display opening screen */
                prepare_display();
                process_filespecs(argc,argv);

                /* verify hardware handshake status */
                if (!ComGetCts() && !WS.Option.ForceCts)
                {
                        cprintf("CTS signal missing!  Please use -HC or -FC option.\r\n");
                        WS.Option.CtsHandshake = 0;
                }

                udta->substt = 1;
                break;

        case 1:
                /* wait for ready handshake with remote */
                service_receive();
                udta->substt = wait_for_ready()? 1:2;
                break;

        case 2:
                /* select first file in batch and begin transmit */
                udta->curnode = WS.first_send;
                udta->substt = 3;
                break;

        case 3:
                /* end of batch? change to state 4 */
                if (udta->curnode == NULL)
                {
                        udta->substt = 4;
                        PSEND("%d file%s transmitted.\r\n",WS.files_sent,
                                                           WS.files_sent==1?"":"s");
                        break;
                }

                service_receive();
                if (transmit_file(udta->curnode->name))
                        break;          /* remain in state 3 */

                /* advance to next file in the batch */
                udta->curnode = udta->curnode->next;
                break;

        case 4:
                /* wait for remaining receive activity to terminate */
                service_receive();
                udta->substt = finish_receive()? 4:5;
                break;

        case 5:
                /* close down link */
                udta->substt = terminate_link()? 5:6;
                break;

        case 6:
                /* process exit codes */
                if (ComCarrierLost())
                        set_cancel_link(CANCEL_CARRIER_LOST);
                if ((WS.files_received+WS.files_sent) ==0)
                        set_cancel_link(CANCEL_NO_FILES);

                ComClose();
                close_display();
                cprintf("HS/Link finished! (t:%d r:%d)",WS.files_sent,WS.files_received);
                cprintf("  Exit code = %d\r\n",(int)WS.cancel_link);
                udta->substt = 0;
                break;
        }

        /* if substt is 0 we are done and need to free the workspace */
        if (udta->substt == 0)
        {
                current_hsws = 0;
                mem_free(udta->appws);
                udta->appws = 0;
        }

        /* otherwise we need more calls- return non 0 */
        return udta->substt;
}
コード例 #5
0
ファイル: comport.c プロジェクト: rbaldwin7/rhocode
/* This function is the C equivalent of the C++ destructor.  It should be   *
 * called when the program is completely finished with the ComPort_t        *
 * structure.                                                               */
void ComDestroy(ComPort_t *Port) {
    ComClose(Port);
}