2013-03-05 45 views
0

您好,我正在使用AM1808 ARM9微處理器。 我有一個接口的GSM dongle.I想使GSM dongle連接以及所有的數據傳輸以及接收在後臺使用pthreads。Pthreads在嵌入式ARM中不起作用

當我試圖在後臺連接加密狗時,它不斷閃爍綠燈,我無法連接到服務器。

我找不到問題。

這是我的datacard初始化代碼以及通信例程。 我正在初始化主線程中的線程。

int InitializeDataCard(void) 
    { 
      static int connect_ret = 0; 
      pthread_t processing_th; 

      pthread_create(&processing_th, NULL, Datacard_Connection_Thread, &db_mc_object); 
      pthread_detach(processing_th); 
      ShowMessageBox(msgbox_text[136], MB_TASKMODAL); 
      if(connect_ret) 
      { 
     ShowMessageBox(msgbox_text[163], MB_ICONERROR); 
     return -1;`enter code here` 
      } 
      else 
      { 
     return 0; 
      } 
    } 
    int ConnectToServer(void) 
    { 
     int connect_ret = 0; 

     Dprintf(__func__,"Trying to connect ...."); 
     DPUUtilsLib_RetrieveParameter(PID_DATACARD_INFO,(UCHAR *)&datacard_info,sizeof(datacard_info)); 
     connect_ret = DataCardConnect(datacard_info); 
     sleep(3); 
     //g_do_process = 0; 
     Dprintf(__func__,"DataCardConnect ret=%d",connect_ret); 

     return connect_ret; 
    } 

    void * Datacard_Connection_Thread(void *tempdata) 
{ 
    int ret=0,response = -1,enc_ret=0; 
    static int g_gsm_response = 0; 
    dpu_csv_file_param_t fileparam; 
    dpu_db_export_search_params_t tempparams; 
    dpu_db_milk_collection_t *livedata,*updatedata; 
    dpu_db_user_master_t CreatedBy,UpdatedBy; 
    dpu_db_society_master_t soc_info; 
    char filename[50]={0}; 

    livedata = (dpu_db_milk_collection_t *)tempdata; 

    //Connect to the Network 
     create_connection : 
    ret = ConnectToServer(); 

    //connected successfully 
    if(!ret) 
    { 
      //Get the SocietyCode from the Database 

     if(g_data_available) 
     { 
      g_data_available=0; 
      soc_info.SocietyId = g_society_list[g_selected_society].SocietyId; 
        DPUDBLib_search_society_master(&soc_info); 

      strncpy(livedata->SocietyCode,soc_info.SocietyCode,5); 

      Dprintf(__func__,"%04d\n %5.2f\n %5.2f\n %5.2f\n %5.2f\n %5.2f\n %5.2f\n %7.2f\n %7.2f\n %03d\n %c\n %d\n %5.2d\n %s\n %d", 
       livedata->MemberCode,livedata->Fat,livedata->LRCLR,livedata->SNF,livedata->Solid, 
       livedata->FatKG,livedata->SNFKG,livedata->Rate,livedata->Amount, 
       livedata->CanNumber,livedata->EntryMode,livedata->MC_RateChartId,livedata->Water,livedata->SocietyCode,__LINE__); 

      sprintf(tempparams.FileName,"%s/%s",DATA_TEMP,MT_MILKLIVEFILE); 

      memcpy(fileparam.FilePath,tempparams.FileName,sizeof(tempparams.FileName)); 

      fileparam.Type = DPU_CSV_EXPORT; 
      fileparam.FileType = DPU_CSV_MILK_LIVE_DATA_FILE; 
      //open a csv file 
      DPUCSVLib_open_csv_file(&fileparam); 

      memset(&CreatedBy,0,sizeof(dpu_db_user_master_t));  
      memset(&UpdatedBy,0,sizeof(dpu_db_user_master_t));  

      strncpy(CreatedBy.Username,TempUser,5); 

      //write the live data into the file 
      DPUCSVLib_write_csv_file(&fileparam,livedata,&CreatedBy,&UpdatedBy); 

      //encrypt the file 
      enc_ret = DPUEncryptFile(tempparams.FileName,filename);   

      if(!enc_ret) 
      { 
       //send file request to server for the accepting the data 
       response = SendFileRequest(g_gsm_response,filename,9); 

       if(!response) 
       { 
        //receive the response of the server 
        response = GetResponceFromServer(g_gsm_response,&response,9); 

        if(response || g_gsm_response) response = -1; 
        else 
        { 
         //If record is successfully sent to the server then update the FlagGSM flag of the record as well as 
         //Update the database 
         g_update_record = 1; 
         livedata->FlagGSM = 1; 
         updatedata->MilkCollectionId = livedata->MilkCollectionId; 
         DPUDBLib_search_milk_collection_entry_by_record(&updatedata); 
         DPUDBLib_edit_milk_collection_entry(&updatedata,&livedata); 
         g_update_record = 0; 
        } 

       } 
       //remove the temp file generated 
       remove(filename); 
      } 
     } 
    } 
    else 
    { 
      //if connection is not successfully done then try to reconnect the server 
    ShowMessageBox(msgbox_text[156], MB_ICONERROR); 
      goto create_connection; 
    } 

} 
+4

你有沒有試過做一個簡單的例子,試圖產生一個線程,例如閃爍指示燈? – sonicwave 2013-03-05 09:19:47

+1

你做了什麼調試?你發現了什麼?是否輸入了線程代碼,如果有的話,它會填滿什麼行? – 2013-03-05 09:40:25

+0

是的,我已經通過調試它進入線程。 – 2013-03-05 10:20:31

回答

0

我認爲這裏存在一個基本的錯誤。通過在InitializeDataCard中聲明靜態變量connect_ret,這並不意味着它將是在ConnectToServer中聲明的相同變量。因此,第一個函數將始終具有相同的行爲...

我認爲你需要一個全局變量(即沒有在函數中定義)並可能有某種同步,因爲當你創建線程時,可能它不會立即執行,所以即使你有一個全局變量,你也不能檢查它,直到你知道它已被正確設置。

+0

謝謝。我得到了解決方案。 – 2013-03-09 09:27:44

+0

我發現一個問題如下。因爲我需要繼續我的進程天氣datacard連接或不。當我嘗試發送數據,即使datacard未連接在這種情況下,我的機器將重新啓動意味着我無法退出線程。然後我用Thread_join()函數,而不是Thread_detach()然後我的目的得到解決,但我的系統變得緩慢,因爲它是不可接受的。 – 2013-03-09 09:28:41

+0

請檢查我的代碼如下,void LiveCollectionDataSend(dpu_db_milk_collection_t * MCdata) { dpu_db_milk_collection_t * tempdata = malloc(sizeof(dpu_db_milk_collection_t)); pthread_t processing_th; int temp; int * ret_val; memcpy(tempdata,MCdata,sizeof(dpu_db_milk_collection_t)); pthread_create(&processing_th,NULL,LiveDataTransferThread,tempdata); // pthread_join(processing_th,(void **)&ret_val); pthread_exit((void **)&ret_val); } – 2013-03-09 09:33:42