1. 程式人生 > >android串列埠通訊例項

android串列埠通訊例項

1、init程式碼
/***************************************************************
** fun:   init uart(/dev/ttymxc1);
** in:    
** out:   fd sucess, -1 false;
** init_mcuuart
***************************************************************/
static int  init_mcuuart(void)
{
    int fd,var;
    portinfo pPort_info;
    int err;

    dbg(DBG_DEBUG," init_mcuuart in");
    
    //clear message buf
    memset(&pPort_info,0,sizeof(portinfo));

    //alloc sent and receive buf
    pSentMessagePackage = malloc(sizeof(msgpkg));
    pReceiveMessagePackage = malloc(sizeof(msgpkg));

    fd=open_mcuport();
    
    pSentMessagePackage->fd= fd;
    pReceiveMessagePackage->fd= fd;
    
    if(fd == -1)
    {
        LOGE("init_mcuuart open port error!");
      return -1;
    }
    
    pPort_info.baud_rate=COM1MCU_BAUD;
    pPort_info.data_bits=COM1MCU_DATABIT;
    pPort_info.flow_ctrl=COM1MCU_CTRL;
    pPort_info.stop_bit=COM1MCU_STOPBIT;
    pPort_info.parity=COM1MCU_PARITY;
    pPort_info.port_fd=fd;

    //pthread_mutex_lock(&pPort_info.portlock);
    var = set_portLocked(&pPort_info);
    //pthread_mutex_unlock(&pPort_info.portlock);
    
    if(var < 0)
    {
        LOGE("set_portLocked error!");
        return -1;
    }

    //handshake message
    //messagePackage(&PowerOnHandShakeCmd,NULL);
    
    //messagePackage(&TestCmd,"************com1mcu.c mode for test*********");

    //uart send message thread
    sem_init(&pSentMessagePackage->uart_begin, 0, 0);
    sem_init(&pSentMessagePackage->uart_end, 0, 0);
    pSentMessagePackage->uart_inited = true;
    err = pthread_create(&pSentMessagePackage->thread_id, NULL, &uartUploadData, (void *)pSentMessagePackage);

    if(err != 0)
        LOGE("init_mcuuart pthread_create pSentMessagePackage error %s!",strerror(err));

    //uart receive message thread and analyze it
    //sem_init(&pReceiveMessagePackage->uart_begin, 0, 0);
    sem_init(&pReceiveMessagePackage->uart_end, 0, 0);
    pReceiveMessagePackage->uart_inited = true;
    err = pthread_create(&pReceiveMessagePackage->thread_id, NULL, &uartDownloadData, (void *)pReceiveMessagePackage);

    if(err != 0)
        LOGE("init_mcuuart pthread_create pReceiveMessagePackage error %s!",strerror(err));

    return 0;               
}
2、傳送資料回撥函式
/***************************************************************
** fun:   uart send handle
** in:    arg pSentMessagePackage
**out:   
** uartUploadData
****************************************************************/
static void * uartUploadData(void * arg)
{
    pMsgPkg upData = (pMsgPkg)arg;

    while(1)
    {
        sem_wait(&upData->uart_begin);
        if(!upData->uart_inited)
        {
            sem_post(&upData->uart_end);
            break;
        }
        //No message to upload
        if(upData->messageCnt <= 0)
            sem_wait(&upData->uart_begin);

        upData->SYNCCode = SYNCDATA1;    

#if 1        
        if(!CRCCheck((uuint8*)upData,Send))    //CRC
        {
            LOGE("uartUploadData CRC Error!");
            sem_wait(&upData->uart_begin);
        }
#endif        

        //sent message len = SYNCCodeLen(2)+CmdCnt(1)+CmdLen(1)+messageLen(N)+CRCLen(1)
        send_mcuuart(upData->fd,upData,upData->messageLen+5);

        sem_post(&upData->uart_end);

        upData->messageCnt = 0;
        upData->messageLen = 0;
    }

    return true;
}
3、接收資料回撥函式
/***************************************************************
** fun:   uart receive handle
** in:    arg pReceiveMessagePackage
** out:   
** uartDownloadData
****************************************************************/
static void * uartDownloadData(void * arg)
{
    uuint8 buffer[RECMSGONCELEN];
    pMsgPkg pmsg = (pMsgPkg)arg;
    int len,i;

    while(1)
    {    
        if(!pmsg->uart_inited)
        {
            sem_post(&pmsg->uart_end);
            break;
        }
        
        recv_mcuuart(pmsg->fd,buffer,&len,RECMSGONCELEN,RECMSGTIMEOUT);

        if(len > 0)
        {            
            //copy the receive data to the big buf
            for(i=0;i<len;i++)
            {
                if(pmsg->pRecPoint >= RECMSGBUFLEN)
                    pmsg->pRecPoint = 0;
                
                receiveBuf[pmsg->pRecPoint] = buffer[i];
                pmsg->pRecPoint++;
            }
            
            memset(buffer,0,RECMSGONCELEN);
        }

        LOGI("pAnalyzePoint=%d,pRecPoint=%d",pmsg->pAnalyzePoint,pmsg->pRecPoint);

        //have new message and prev message have handout to app, analyze from the analyze Point
        if((pmsg->pAnalyzePoint != pmsg->pRecPoint)&&(!pmsg->handOutFlag))
            analyzeMsgPackage(pmsg, &(receiveBuf[pmsg->pAnalyzePoint]));
    }

    return true;    
}